pax_global_header00006660000000000000000000000064126057366640014531gustar00rootroot0000000000000052 comment=d7d21b1008556df728be3993ebdd62abe17f1fcb pep257-0.7.0/000077500000000000000000000000001260573666400125575ustar00rootroot00000000000000pep257-0.7.0/.gitignore000066400000000000000000000006631260573666400145540ustar00rootroot00000000000000*.py[co] # Vim *.swp *.swo # Packages *.egg *.egg-info dist build eggs parts bin var sdist develop-eggs .installed.cfg .cache MANIFEST # Installer logs pip-log.txt # Unit test / coverage reports .coverage .tox nosetests.xml # Translations *.mo # Sphinx docs/_* # Eclipse files .project .pydevproject .settings # PyCharm files .idea # virtualenv venv/ # generated rst docs/snippets/error_code_table.rst # PyCharm files .idea pep257-0.7.0/.travis.yml000066400000000000000000000004351260573666400146720ustar00rootroot00000000000000# Travis (http://travis-ci.org/) is a continuous integration # service for open source projects. This file configures # Travis to install and run "tox" test runner, which is # configured in tox.ini file. sudo: false language: python install: pip install tox --use-mirrors script: tox pep257-0.7.0/LICENSE-MIT000066400000000000000000000020671260573666400142200ustar00rootroot00000000000000Copyright (c) 2012 GreenSteam, Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. pep257-0.7.0/MANIFEST.in000066400000000000000000000000371260573666400143150ustar00rootroot00000000000000include README.rst LICENSE-MIT pep257-0.7.0/README.rst000066400000000000000000000024361260573666400142530ustar00rootroot00000000000000PEP 257 docstring style checker =========================================================== **pep257** is a static analysis tool for checking compliance with Python `PEP 257 `_. The framework for checking docstring style is flexible, and custom checks can be easily added, for example to cover NumPy `docstring conventions `_. **pep257** supports Python 2.6, 2.7, 3.2, 3.3, 3.4, pypy and pypy3. Quick Start ----------- Install ^^^^^^^ .. code:: pip install pep257 Run ^^^ .. code:: $ pep257 test.py test.py:18 in private nested class `meta`: D101: Docstring missing test.py:22 in public method `method`: D102: Docstring missing ... Links ----- .. image:: https://travis-ci.org/GreenSteam/pep257.svg?branch=master :target: https://travis-ci.org/GreenSteam/pep257 .. image:: https://readthedocs.org/projects/pep257/badge/?version=latest :target: https://readthedocs.org/projects/pep257/?badge=latest :alt: Documentation Status * `Read the full documentation here `_. * `Fork pep257 on GitHub `_. * `PyPI project page `_. pep257-0.7.0/docs/000077500000000000000000000000001260573666400135075ustar00rootroot00000000000000pep257-0.7.0/docs/Makefile000066400000000000000000000151521260573666400151530ustar00rootroot00000000000000# Makefile for Sphinx documentation # # You can set these variables from the command line. SPHINXOPTS = SPHINXBUILD = sphinx-build PAPER = BUILDDIR = _build # User-friendly check for sphinx-build ifeq ($(shell which $(SPHINXBUILD) >/dev/null 2>&1; echo $$?), 1) $(error The '$(SPHINXBUILD)' command was not found. Make sure you have Sphinx installed, then set the SPHINXBUILD environment variable to point to the full path of the '$(SPHINXBUILD)' executable. Alternatively you can add the directory with the executable to your PATH. If you don't have Sphinx installed, grab it from http://sphinx-doc.org/) endif # Internal variables. PAPEROPT_a4 = -D latex_paper_size=a4 PAPEROPT_letter = -D latex_paper_size=letter ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . # the i18n builder cannot share the environment and doctrees with the others I18NSPHINXOPTS = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . .PHONY: help clean html dirhtml singlehtml pickle json htmlhelp qthelp devhelp epub latex latexpdf text man changes linkcheck doctest gettext help: @echo "Please use \`make ' where is one of" @echo " html to make standalone HTML files" @echo " dirhtml to make HTML files named index.html in directories" @echo " singlehtml to make a single large HTML file" @echo " pickle to make pickle files" @echo " json to make JSON files" @echo " htmlhelp to make HTML files and a HTML help project" @echo " qthelp to make HTML files and a qthelp project" @echo " devhelp to make HTML files and a Devhelp project" @echo " epub to make an epub" @echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter" @echo " latexpdf to make LaTeX files and run them through pdflatex" @echo " latexpdfja to make LaTeX files and run them through platex/dvipdfmx" @echo " text to make text files" @echo " man to make manual pages" @echo " texinfo to make Texinfo files" @echo " info to make Texinfo files and run them through makeinfo" @echo " gettext to make PO message catalogs" @echo " changes to make an overview of all changed/added/deprecated items" @echo " xml to make Docutils-native XML files" @echo " pseudoxml to make pseudoxml-XML files for display purposes" @echo " linkcheck to check all external links for integrity" @echo " doctest to run all doctests embedded in the documentation (if enabled)" clean: rm -rf $(BUILDDIR)/* html: $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html @echo @echo "Build finished. The HTML pages are in $(BUILDDIR)/html." dirhtml: $(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml @echo @echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml." singlehtml: $(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml @echo @echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml." pickle: $(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle @echo @echo "Build finished; now you can process the pickle files." json: $(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json @echo @echo "Build finished; now you can process the JSON files." htmlhelp: $(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp @echo @echo "Build finished; now you can run HTML Help Workshop with the" \ ".hhp project file in $(BUILDDIR)/htmlhelp." qthelp: $(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp @echo @echo "Build finished; now you can run "qcollectiongenerator" with the" \ ".qhcp project file in $(BUILDDIR)/qthelp, like this:" @echo "# qcollectiongenerator $(BUILDDIR)/qthelp/pep257.qhcp" @echo "To view the help file:" @echo "# assistant -collectionFile $(BUILDDIR)/qthelp/pep257.qhc" devhelp: $(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp @echo @echo "Build finished." @echo "To view the help file:" @echo "# mkdir -p $$HOME/.local/share/devhelp/pep257" @echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/pep257" @echo "# devhelp" epub: $(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub @echo @echo "Build finished. The epub file is in $(BUILDDIR)/epub." latex: $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex @echo @echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex." @echo "Run \`make' in that directory to run these through (pdf)latex" \ "(use \`make latexpdf' here to do that automatically)." latexpdf: $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex @echo "Running LaTeX files through pdflatex..." $(MAKE) -C $(BUILDDIR)/latex all-pdf @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." latexpdfja: $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex @echo "Running LaTeX files through platex and dvipdfmx..." $(MAKE) -C $(BUILDDIR)/latex all-pdf-ja @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." text: $(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text @echo @echo "Build finished. The text files are in $(BUILDDIR)/text." man: $(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man @echo @echo "Build finished. The manual pages are in $(BUILDDIR)/man." texinfo: $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo @echo @echo "Build finished. The Texinfo files are in $(BUILDDIR)/texinfo." @echo "Run \`make' in that directory to run these through makeinfo" \ "(use \`make info' here to do that automatically)." info: $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo @echo "Running Texinfo files through makeinfo..." make -C $(BUILDDIR)/texinfo info @echo "makeinfo finished; the Info files are in $(BUILDDIR)/texinfo." gettext: $(SPHINXBUILD) -b gettext $(I18NSPHINXOPTS) $(BUILDDIR)/locale @echo @echo "Build finished. The message catalogs are in $(BUILDDIR)/locale." changes: $(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes @echo @echo "The overview file is in $(BUILDDIR)/changes." linkcheck: $(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck @echo @echo "Link check complete; look for any errors in the above output " \ "or in $(BUILDDIR)/linkcheck/output.txt." doctest: $(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest @echo "Testing of doctests in the sources finished, look at the " \ "results in $(BUILDDIR)/doctest/output.txt." xml: $(SPHINXBUILD) -b xml $(ALLSPHINXOPTS) $(BUILDDIR)/xml @echo @echo "Build finished. The XML files are in $(BUILDDIR)/xml." pseudoxml: $(SPHINXBUILD) -b pseudoxml $(ALLSPHINXOPTS) $(BUILDDIR)/pseudoxml @echo @echo "Build finished. The pseudo-XML files are in $(BUILDDIR)/pseudoxml." pep257-0.7.0/docs/conf.py000066400000000000000000000207231260573666400150120ustar00rootroot00000000000000# -*- coding: utf-8 -*- # # pep257 documentation build configuration file, created by # sphinx-quickstart on Fri Jan 30 20:30:42 2015. # # This file is execfile()d with the current directory set to its # containing dir. # # Note that not all possible configuration values are present in this # autogenerated file. # # All configuration values have a default; values that are commented out # serve to show the default. import sys import os # If extensions (or modules to document with autodoc) are in another directory, # add these directories to sys.path here. If the directory is relative to the # documentation root, use os.path.abspath to make it absolute, like shown here. sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..')) # -- General configuration ------------------------------------------------ # If your documentation needs a minimal Sphinx version, state it here. #needs_sphinx = '1.0' # Add any Sphinx extension module names here, as strings. They can be # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom # ones. extensions = [ 'sphinx.ext.autodoc', 'sphinx.ext.coverage', 'sphinx.ext.viewcode', 'sphinxcontrib.issuetracker', # autolinks issue numbers (like #78) ] # Add any paths that contain templates here, relative to this directory. templates_path = ['_templates'] # The suffix of source filenames. source_suffix = '.rst' # The encoding of source files. #source_encoding = 'utf-8-sig' # The master toctree document. master_doc = 'index' # General information about the project. project = u'pep257' copyright = u'2015, Vladimir Keleshev' # The version info for the project you're documenting, acts as replacement for # |version| and |release|, also used in various other places throughout the # built documents. # # The short X.Y version. version = '0.5.0' # The full version, including alpha/beta/rc tags. release = '0.5.0' # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. #language = None # There are two options for replacing |today|: either, you set today to some # non-false value, then it is used: #today = '' # Else, today_fmt is used as the format for a strftime call. #today_fmt = '%B %d, %Y' # List of patterns, relative to source directory, that match files and # directories to ignore when looking for source files. exclude_patterns = ['_build'] # The reST default role (used for this markup: `text`) to use for all # documents. #default_role = None # If true, '()' will be appended to :func: etc. cross-reference text. #add_function_parentheses = True # If true, the current module name will be prepended to all description # unit titles (such as .. function::). #add_module_names = True # If true, sectionauthor and moduleauthor directives will be shown in the # output. They are ignored by default. #show_authors = False # The name of the Pygments (syntax highlighting) style to use. pygments_style = 'sphinx' # A list of ignored prefixes for module index sorting. #modindex_common_prefix = [] # If true, keep warnings as "system message" paragraphs in the built documents. #keep_warnings = False # -- Options for HTML output ---------------------------------------------- # The theme to use for HTML and HTML Help pages. See the documentation for # a list of builtin themes. html_theme = 'default' # Theme options are theme-specific and customize the look and feel of a theme # further. For a list of options available for each theme, see the # documentation. #html_theme_options = {} # Add any paths that contain custom themes here, relative to this directory. #html_theme_path = [] # The name for this set of Sphinx documents. If None, it defaults to # " v documentation". #html_title = None # A shorter title for the navigation bar. Default is the same as html_title. #html_short_title = None # The name of an image file (relative to this directory) to place at the top # of the sidebar. #html_logo = None # The name of an image file (within the static path) to use as favicon of the # docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 # pixels large. #html_favicon = None # Add any paths that contain custom static files (such as style sheets) here, # relative to this directory. They are copied after the builtin static files, # so a file named "default.css" will overwrite the builtin "default.css". html_static_path = ['_static'] # Add any extra paths that contain custom files (such as robots.txt or # .htaccess) here, relative to this directory. These files are copied # directly to the root of the documentation. #html_extra_path = [] # If not '', a 'Last updated on:' timestamp is inserted at every page bottom, # using the given strftime format. #html_last_updated_fmt = '%b %d, %Y' # If true, SmartyPants will be used to convert quotes and dashes to # typographically correct entities. #html_use_smartypants = True # Custom sidebar templates, maps document names to template names. #html_sidebars = {} # Additional templates that should be rendered to pages, maps page names to # template names. #html_additional_pages = {} # If false, no module index is generated. #html_domain_indices = True # If false, no index is generated. #html_use_index = True # If true, the index is split into individual pages for each letter. #html_split_index = False # If true, links to the reST sources are added to the pages. #html_show_sourcelink = True # If true, "Created using Sphinx" is shown in the HTML footer. Default is True. #html_show_sphinx = True # If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. #html_show_copyright = True # If true, an OpenSearch description file will be output, and all pages will # contain a tag referring to it. The value of this option must be the # base URL from which the finished HTML is served. #html_use_opensearch = '' # This is the file name suffix for HTML files (e.g. ".xhtml"). #html_file_suffix = None # Output file base name for HTML help builder. htmlhelp_basename = 'pep257doc' # -- Options for LaTeX output --------------------------------------------- latex_elements = { # The paper size ('letterpaper' or 'a4paper'). #'papersize': 'letterpaper', # The font size ('10pt', '11pt' or '12pt'). #'pointsize': '10pt', # Additional stuff for the LaTeX preamble. #'preamble': '', } # Grouping the document tree into LaTeX files. List of tuples # (source start file, target name, title, # author, documentclass [howto, manual, or own class]). latex_documents = [ ('index', 'pep257.tex', u'pep257 Documentation', u'Vladimir Keleshev', 'manual'), ] # The name of an image file (relative to this directory) to place at the top of # the title page. #latex_logo = None # For "manual" documents, if this is true, then toplevel headings are parts, # not chapters. #latex_use_parts = False # If true, show page references after internal links. #latex_show_pagerefs = False # If true, show URL addresses after external links. #latex_show_urls = False # Documents to append as an appendix to all manuals. #latex_appendices = [] # If false, no module index is generated. #latex_domain_indices = True # -- Options for manual page output --------------------------------------- # One entry per manual page. List of tuples # (source start file, name, description, authors, manual section). man_pages = [ ('index', 'pep257', u'pep257 Documentation', [u'Vladimir Keleshev'], 1) ] # If true, show URL addresses after external links. #man_show_urls = False # -- Options for Texinfo output ------------------------------------------- # Grouping the document tree into Texinfo files. List of tuples # (source start file, target name, title, author, # dir menu entry, description, category) texinfo_documents = [ ('index', 'pep257', u'pep257 Documentation', u'Vladimir Keleshev', 'pep257', 'One line description of project.', 'Miscellaneous'), ] # Documents to append as an appendix to all manuals. #texinfo_appendices = [] # If false, no module index is generated. #texinfo_domain_indices = True # How to display URL addresses: 'footnote', 'no', or 'inline'. #texinfo_show_urls = 'footnote' # If true, do not generate a @detailmenu in the "Top" node's menu. #texinfo_no_detailmenu = False # sphinxcontrib.issuetracker settings issuetracker = 'github' issuetracker_project = 'GreenSteam/pep257' def generate_error_code_table(): from pep257 import ErrorRegistry with open(os.path.join('snippets', 'error_code_table.rst'), 'wt') as outf: outf.write(ErrorRegistry.to_rst()) generate_error_code_table() pep257-0.7.0/docs/error_codes.rst000066400000000000000000000006031260573666400165460ustar00rootroot00000000000000Error Codes =========== Grouping -------- .. include:: snippets/error_code_table.rst Default Checks -------------- Not all error codes are checked for by default. The default behavior is to check only error codes that are part of the `PEP257 `_ official convention. All of the above error codes are checked for by default except for D203. pep257-0.7.0/docs/index.rst000066400000000000000000000011211260573666400153430ustar00rootroot00000000000000.. pep257 documentation master file, created by sphinx-quickstart on Fri Jan 30 20:30:42 2015. You can adapt this file completely to your liking, but it should at least contain the root `toctree` directive. pep257's documentation ====================== **pep257** is a static analysis tool for checking compliance with Python `PEP 257 `_. Contents: .. toctree:: :maxdepth: 2 usage error_codes release_notes license .. include:: quickstart.rst Credits ======= Created by Vladimir Keleshev. Maintained by Amir Rachum. pep257-0.7.0/docs/license.rst000066400000000000000000000000551260573666400156630ustar00rootroot00000000000000License ======= .. include:: ../LICENSE-MIT pep257-0.7.0/docs/make.bat000066400000000000000000000150551260573666400151220ustar00rootroot00000000000000@ECHO OFF REM Command file for Sphinx documentation if "%SPHINXBUILD%" == "" ( set SPHINXBUILD=sphinx-build ) set BUILDDIR=_build set ALLSPHINXOPTS=-d %BUILDDIR%/doctrees %SPHINXOPTS% . set I18NSPHINXOPTS=%SPHINXOPTS% . if NOT "%PAPER%" == "" ( set ALLSPHINXOPTS=-D latex_paper_size=%PAPER% %ALLSPHINXOPTS% set I18NSPHINXOPTS=-D latex_paper_size=%PAPER% %I18NSPHINXOPTS% ) if "%1" == "" goto help if "%1" == "help" ( :help echo.Please use `make ^` where ^ is one of echo. html to make standalone HTML files echo. dirhtml to make HTML files named index.html in directories echo. singlehtml to make a single large HTML file echo. pickle to make pickle files echo. json to make JSON files echo. htmlhelp to make HTML files and a HTML help project echo. qthelp to make HTML files and a qthelp project echo. devhelp to make HTML files and a Devhelp project echo. epub to make an epub echo. latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter echo. text to make text files echo. man to make manual pages echo. texinfo to make Texinfo files echo. gettext to make PO message catalogs echo. changes to make an overview over all changed/added/deprecated items echo. xml to make Docutils-native XML files echo. pseudoxml to make pseudoxml-XML files for display purposes echo. linkcheck to check all external links for integrity echo. doctest to run all doctests embedded in the documentation if enabled goto end ) if "%1" == "clean" ( for /d %%i in (%BUILDDIR%\*) do rmdir /q /s %%i del /q /s %BUILDDIR%\* goto end ) %SPHINXBUILD% 2> nul if errorlevel 9009 ( echo. echo.The 'sphinx-build' command was not found. Make sure you have Sphinx echo.installed, then set the SPHINXBUILD environment variable to point echo.to the full path of the 'sphinx-build' executable. Alternatively you echo.may add the Sphinx directory to PATH. echo. echo.If you don't have Sphinx installed, grab it from echo.http://sphinx-doc.org/ exit /b 1 ) if "%1" == "html" ( %SPHINXBUILD% -b html %ALLSPHINXOPTS% %BUILDDIR%/html if errorlevel 1 exit /b 1 echo. echo.Build finished. The HTML pages are in %BUILDDIR%/html. goto end ) if "%1" == "dirhtml" ( %SPHINXBUILD% -b dirhtml %ALLSPHINXOPTS% %BUILDDIR%/dirhtml if errorlevel 1 exit /b 1 echo. echo.Build finished. The HTML pages are in %BUILDDIR%/dirhtml. goto end ) if "%1" == "singlehtml" ( %SPHINXBUILD% -b singlehtml %ALLSPHINXOPTS% %BUILDDIR%/singlehtml if errorlevel 1 exit /b 1 echo. echo.Build finished. The HTML pages are in %BUILDDIR%/singlehtml. goto end ) if "%1" == "pickle" ( %SPHINXBUILD% -b pickle %ALLSPHINXOPTS% %BUILDDIR%/pickle if errorlevel 1 exit /b 1 echo. echo.Build finished; now you can process the pickle files. goto end ) if "%1" == "json" ( %SPHINXBUILD% -b json %ALLSPHINXOPTS% %BUILDDIR%/json if errorlevel 1 exit /b 1 echo. echo.Build finished; now you can process the JSON files. goto end ) if "%1" == "htmlhelp" ( %SPHINXBUILD% -b htmlhelp %ALLSPHINXOPTS% %BUILDDIR%/htmlhelp if errorlevel 1 exit /b 1 echo. echo.Build finished; now you can run HTML Help Workshop with the ^ .hhp project file in %BUILDDIR%/htmlhelp. goto end ) if "%1" == "qthelp" ( %SPHINXBUILD% -b qthelp %ALLSPHINXOPTS% %BUILDDIR%/qthelp if errorlevel 1 exit /b 1 echo. echo.Build finished; now you can run "qcollectiongenerator" with the ^ .qhcp project file in %BUILDDIR%/qthelp, like this: echo.^> qcollectiongenerator %BUILDDIR%\qthelp\pep257.qhcp echo.To view the help file: echo.^> assistant -collectionFile %BUILDDIR%\qthelp\pep257.ghc goto end ) if "%1" == "devhelp" ( %SPHINXBUILD% -b devhelp %ALLSPHINXOPTS% %BUILDDIR%/devhelp if errorlevel 1 exit /b 1 echo. echo.Build finished. goto end ) if "%1" == "epub" ( %SPHINXBUILD% -b epub %ALLSPHINXOPTS% %BUILDDIR%/epub if errorlevel 1 exit /b 1 echo. echo.Build finished. The epub file is in %BUILDDIR%/epub. goto end ) if "%1" == "latex" ( %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex if errorlevel 1 exit /b 1 echo. echo.Build finished; the LaTeX files are in %BUILDDIR%/latex. goto end ) if "%1" == "latexpdf" ( %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex cd %BUILDDIR%/latex make all-pdf cd %BUILDDIR%/.. echo. echo.Build finished; the PDF files are in %BUILDDIR%/latex. goto end ) if "%1" == "latexpdfja" ( %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex cd %BUILDDIR%/latex make all-pdf-ja cd %BUILDDIR%/.. echo. echo.Build finished; the PDF files are in %BUILDDIR%/latex. goto end ) if "%1" == "text" ( %SPHINXBUILD% -b text %ALLSPHINXOPTS% %BUILDDIR%/text if errorlevel 1 exit /b 1 echo. echo.Build finished. The text files are in %BUILDDIR%/text. goto end ) if "%1" == "man" ( %SPHINXBUILD% -b man %ALLSPHINXOPTS% %BUILDDIR%/man if errorlevel 1 exit /b 1 echo. echo.Build finished. The manual pages are in %BUILDDIR%/man. goto end ) if "%1" == "texinfo" ( %SPHINXBUILD% -b texinfo %ALLSPHINXOPTS% %BUILDDIR%/texinfo if errorlevel 1 exit /b 1 echo. echo.Build finished. The Texinfo files are in %BUILDDIR%/texinfo. goto end ) if "%1" == "gettext" ( %SPHINXBUILD% -b gettext %I18NSPHINXOPTS% %BUILDDIR%/locale if errorlevel 1 exit /b 1 echo. echo.Build finished. The message catalogs are in %BUILDDIR%/locale. goto end ) if "%1" == "changes" ( %SPHINXBUILD% -b changes %ALLSPHINXOPTS% %BUILDDIR%/changes if errorlevel 1 exit /b 1 echo. echo.The overview file is in %BUILDDIR%/changes. goto end ) if "%1" == "linkcheck" ( %SPHINXBUILD% -b linkcheck %ALLSPHINXOPTS% %BUILDDIR%/linkcheck if errorlevel 1 exit /b 1 echo. echo.Link check complete; look for any errors in the above output ^ or in %BUILDDIR%/linkcheck/output.txt. goto end ) if "%1" == "doctest" ( %SPHINXBUILD% -b doctest %ALLSPHINXOPTS% %BUILDDIR%/doctest if errorlevel 1 exit /b 1 echo. echo.Testing of doctests in the sources finished, look at the ^ results in %BUILDDIR%/doctest/output.txt. goto end ) if "%1" == "xml" ( %SPHINXBUILD% -b xml %ALLSPHINXOPTS% %BUILDDIR%/xml if errorlevel 1 exit /b 1 echo. echo.Build finished. The XML files are in %BUILDDIR%/xml. goto end ) if "%1" == "pseudoxml" ( %SPHINXBUILD% -b pseudoxml %ALLSPHINXOPTS% %BUILDDIR%/pseudoxml if errorlevel 1 exit /b 1 echo. echo.Build finished. The pseudo-XML files are in %BUILDDIR%/pseudoxml. goto end ) :end pep257-0.7.0/docs/quickstart.rst000066400000000000000000000005231260573666400164330ustar00rootroot00000000000000Quick Start =========== 1. Install .. code:: pip install pep257 2. Run .. code:: $ pep257 test.py test.py:18 in private nested class `meta`: D101: Docstring missing test.py:22 in public method `method`: D102: Docstring missing ... 3. Fix your code :) pep257-0.7.0/docs/release_notes.rst000066400000000000000000000073531260573666400171010ustar00rootroot00000000000000Release Notes ============= 0.7.0 - October 9th, 2015 ------------------------- New Features * Added the D104 error code - "Missing docstring in public package". This new error is turned on by default. Missing docstring in `__init__.py` files which previously resulted in D100 errors ("Missing docstring in public module") will now result in D104 (#105, #127). * Added the D105 error code - "Missing docstring in magic method'. This new error is turned on by default. Missing docstrings in magic method which previously resulted in D102 error ("Missing docstring in public method") will now result in D105. Note that exceptions to this rule are variadic magic methods - specifically `__init__`, `__call__` and `__new__`, which will be considered non-magic and missing docstrings in them will result in D102 (#60, #139). * Support the option to exclude all error codes. Running pep257 with `--select=` (or `select=` in the configuration file) will exclude all errors which could then be added one by one using `add-select`. Useful for projects new to pep257 (#132, #135). * Added check D211: No blank lines allowed before class docstring. This change is a result of a change to the official PEP257 convention. Therefore, D211 will now be checked by default instead of D203, which required a single blank line before a class docstring (#137). * Configuration files are now handled correctly. The closer a configuration file is to a checked file the more it matters. Configuration files no longer support ``explain``, ``source``, ``debug``, ``verbose`` or ``count`` (#133). Bug Fixes * On Python 2.x, D302 ("Use u""" for Unicode docstrings") is not reported if `unicode_literals` is imported from `__future__` (#113, #134). * Fixed a bug where there was no executable for `pep257` on Windows (#73, #136). 0.6.0 - July 20th, 2015 ----------------------- New Features * Added support for more flexible error selections using ``--ignore``, ``--select``, ``--convention``, ``--add-ignore`` and ``--add-select`` (#96, #123). Bug Fixes * Property setter and deleter methods are now treated as private and do not require docstrings separate from the main property method (#69, #107). * Fixed an issue where pep257 did not accept docstrings that are both unicode and raw in Python 2.x (#116, #119). * Fixed an issue where Python 3.x files with Unicode encodings were not read correctly (#118). 0.5.0 - March 14th, 2015 ------------------------ New Features * Added check D210: No whitespaces allowed surrounding docstring text (#95). * Added real documentation rendering using Sphinx (#100, #101). Bug Fixes * Removed log level configuration from module level (#98). * D205 used to check that there was *a* blank line between the one line summary and the description. It now checks that there is *exactly* one blank line between them (#79). * Fixed a bug where ``--match-dir`` was not properly respected (#108, #109). 0.4.1 - January 10th, 2015 -------------------------- Bug Fixes * Getting ``ImportError`` when trying to run pep257 as the installed script (#92, #93). 0.4.0 - January 4th, 2015 ------------------------- .. warning:: A fatal bug was discovered in this version (#92). Please use a newer version. New Features * Added configuration file support (#58, #87). * Added a ``--count`` flag that prints the number of violations found (#86, #89). * Added support for Python 3.4, PyPy and PyPy3 (#81). Bug Fixes * Fixed broken tests (#74). * Fixed parsing various colon and parenthesis combinations in definitions (#82). * Allow for greater flexibility in parsing ``__all__`` (#67). * Fixed handling of one-liner definitions (#77). 0.3.2 - March 11th, 2014 ------------------------ First documented release! pep257-0.7.0/docs/snippets/000077500000000000000000000000001260573666400153545ustar00rootroot00000000000000pep257-0.7.0/docs/snippets/cli.rst000066400000000000000000000050351260573666400166600ustar00rootroot00000000000000.. _cli_usage: Usage ^^^^^ .. code:: Usage: pep257 [options] [...] Options: --version show program's version number and exit -h, --help show this help message and exit -e, --explain show explanation of each error -s, --source show source for each error --select= choose the basic list of checked errors by specifying which errors to check for (with a list of comma- separated error codes). for example: --select=D101,D202 --ignore= choose the basic list of checked errors by specifying which errors to ignore (with a list of comma-separated error codes). for example: --ignore=D101,D202 --convention= choose the basic list of checked errors by specifying an existing convention. Possible conventions: pep257 --add-select= amend the list of errors to check for by specifying more error codes to check. --add-ignore= amend the list of errors to check for by specifying more error codes to ignore. --match= check only files that exactly match regular expression; default is --match='(?!test_).*\.py' which matches files that don't start with 'test_' but end with '.py' --match-dir= search only dirs that exactly match regular expression; default is --match-dir='[^\.].*', which matches all dirs that don't start with a dot -d, --debug print debug information -v, --verbose print status information --count print total number of errors to stdout Return Code ^^^^^^^^^^^ +--------------+--------------------------------------------------------------+ | 0 | Success - no violations | +--------------+--------------------------------------------------------------+ | 1 | Some code violations were found | +--------------+--------------------------------------------------------------+ | 2 | Illegal usage - see error message | +--------------+--------------------------------------------------------------+ pep257-0.7.0/docs/snippets/config.rst000066400000000000000000000035301260573666400173540ustar00rootroot00000000000000``pep257`` supports `ini`-like configuration files. In order for ``pep257`` to use it, it must be named ``setup.cfg``, ``tox.ini`` or ``.pep257`` and have a ``[pep257]`` section. When searching for a configuration file, ``pep257`` looks for one of the file specified above `in that exact order`. If a configuration file was not found, it keeps looking for one up the directory tree until one is found or uses the default configuration. Available Options ################# Not all configuration options are available in the configuration files. Available options are: * ``convention`` * ``select`` * ``ignore`` * ``add_select`` * ``add_ignore`` * ``match`` * ``match_dir`` See the :ref:`cli_usage` section for more information. Inheritance ########### By default, when finding a configuration file, ``pep257`` tries to inherit the parent directory's configuration and merge them to the local ones. The merge process is as follows: * If one of ``select``, ``ignore`` or ``convention`` was specified in the child configuration - Ignores the parent configuration and set the new error codes to check. Othewise, Simply copies the parent checked error codes. * If ``add-ignore`` or ``add-select`` were specified, adds or removes the specified error codes from the checked error codes list. * If ``match`` or ``match-dir`` were specified - use them. Otherwise, use the parent's. In order to disable this (useful for configuration files located in your repo's root), simply add ``inherit=false`` to your configuration file. .. note:: If any of ``select``, ``ignore`` or ``convention`` were specified in the CLI, the configuration files will take no part in choosing which error codes will be checked. ``match`` and ``match-dir`` will still take effect. Example ####### .. code:: [pep257] inherit = false ignore = D100,D203,D405 match = *.py pep257-0.7.0/docs/snippets/install.rst000066400000000000000000000002461260573666400175560ustar00rootroot00000000000000Use `pip `_ or easy_install:: pip install pep257 Alternatively, you can use ``pep257.py`` source file directly--it is self-contained. pep257-0.7.0/docs/usage.rst000066400000000000000000000003451260573666400153470ustar00rootroot00000000000000Usage ===== Installation ------------ .. include:: snippets/install.rst Command Line Interface ---------------------- .. include:: snippets/cli.rst Configuration Files ^^^^^^^^^^^^^^^^^^^ .. include:: snippets/config.rst pep257-0.7.0/requirements/000077500000000000000000000000001260573666400153025ustar00rootroot00000000000000pep257-0.7.0/requirements/docs.txt000066400000000000000000000000321260573666400167660ustar00rootroot00000000000000sphinxcontrib-issuetrackerpep257-0.7.0/requirements/tests.txt000066400000000000000000000000361260573666400172040ustar00rootroot00000000000000pytest==2.7.2 pytest-pep8 mockpep257-0.7.0/setup.cfg000066400000000000000000000000261260573666400143760ustar00rootroot00000000000000[wheel] universal = 1 pep257-0.7.0/setup.py000066400000000000000000000017431260573666400142760ustar00rootroot00000000000000from __future__ import with_statement import os from setuptools import setup with open(os.path.join('src', 'pep257.py')) as f: for line in f: if line.startswith('__version__'): version = eval(line.split('=')[-1]) setup( name='pep257', version=version, description="Python docstring style checker", long_description=open('README.rst').read(), license='MIT', author='Vladimir Keleshev', url='https://github.com/GreenSteam/pep257/', classifiers=[ 'Intended Audience :: Developers', 'Environment :: Console', 'Programming Language :: Python :: 2', 'Programming Language :: Python :: 3', 'Operating System :: OS Independent', 'License :: OSI Approved :: MIT License', ], keywords='PEP 257, pep257, PEP 8, pep8, docstrings', package_dir={'': 'src'}, py_modules=['pep257'], entry_points={ 'console_scripts': [ 'pep257 = pep257:main', ], }, ) pep257-0.7.0/src/000077500000000000000000000000001260573666400133465ustar00rootroot00000000000000pep257-0.7.0/src/__init__.py000066400000000000000000000000001260573666400154450ustar00rootroot00000000000000pep257-0.7.0/src/pep257.py000077500000000000000000001651361260573666400147610ustar00rootroot00000000000000#! /usr/bin/env python """Static analysis tool for checking docstring conventions and style. Implemented checks cover PEP257: http://www.python.org/dev/peps/pep-0257/ Other checks can be added, e.g. NumPy docstring conventions: https://github.com/numpy/numpy/blob/master/doc/HOWTO_DOCUMENT.rst.txt The repository is located at: http://github.com/GreenSteam/pep257 """ from __future__ import with_statement import os import sys import copy import logging import tokenize as tk from itertools import takewhile, dropwhile, chain from re import compile as re import itertools from collections import defaultdict, namedtuple, Set try: # Python 3.x from ConfigParser import RawConfigParser except ImportError: # Python 2.x from configparser import RawConfigParser log = logging.getLogger(__name__) try: from StringIO import StringIO except ImportError: # Python 3.0 and later from io import StringIO try: next except NameError: # Python 2.5 and earlier nothing = object() def next(obj, default=nothing): if default == nothing: return obj.next() else: try: return obj.next() except StopIteration: return default # If possible (python >= 3.2) use tokenize.open to open files, so PEP 263 # encoding markers are interpreted. try: tokenize_open = tk.open except AttributeError: tokenize_open = open __version__ = '0.7.0' __all__ = ('check', 'collect') NO_VIOLATIONS_RETURN_CODE = 0 VIOLATIONS_RETURN_CODE = 1 INVALID_OPTIONS_RETURN_CODE = 2 VARIADIC_MAGIC_METHODS = ('__init__', '__call__', '__new__') def humanize(string): return re(r'(.)([A-Z]+)').sub(r'\1 \2', string).lower() def is_magic(name): return (name.startswith('__') and name.endswith('__') and name not in VARIADIC_MAGIC_METHODS) def is_ascii(string): return all(ord(char) < 128 for char in string) def is_blank(string): return not string.strip() def leading_space(string): return re('\s*').match(string).group() class Value(object): def __init__(self, *args): vars(self).update(zip(self._fields, args)) def __hash__(self): return hash(repr(self)) def __eq__(self, other): return other and vars(self) == vars(other) def __repr__(self): kwargs = ', '.join('{0}={1!r}'.format(field, getattr(self, field)) for field in self._fields) return '{0}({1})'.format(self.__class__.__name__, kwargs) class Definition(Value): _fields = ('name', '_source', 'start', 'end', 'decorators', 'docstring', 'children', 'parent') _human = property(lambda self: humanize(type(self).__name__)) kind = property(lambda self: self._human.split()[-1]) module = property(lambda self: self.parent.module) all = property(lambda self: self.module.all) _slice = property(lambda self: slice(self.start - 1, self.end)) source = property(lambda self: ''.join(self._source[self._slice])) def __iter__(self): return chain([self], *self.children) @property def _publicity(self): return {True: 'public', False: 'private'}[self.is_public] def __str__(self): return 'in %s %s `%s`' % (self._publicity, self._human, self.name) class Module(Definition): _fields = ('name', '_source', 'start', 'end', 'decorators', 'docstring', 'children', 'parent', '_all', 'future_imports') is_public = True _nest = staticmethod(lambda s: {'def': Function, 'class': Class}[s]) module = property(lambda self: self) all = property(lambda self: self._all) def __str__(self): return 'at module level' class Package(Module): """A package is a __init__.py module.""" class Function(Definition): _nest = staticmethod(lambda s: {'def': NestedFunction, 'class': NestedClass}[s]) @property def is_public(self): if self.all is not None: return self.name in self.all else: return not self.name.startswith('_') class NestedFunction(Function): is_public = False class Method(Function): @property def is_public(self): # Check if we are a setter/deleter method, and mark as private if so. for decorator in self.decorators: # Given 'foo', match 'foo.bar' but not 'foobar' or 'sfoo' if re(r"^{0}\.".format(self.name)).match(decorator.name): return False name_is_public = (not self.name.startswith('_') or self.name in VARIADIC_MAGIC_METHODS or is_magic(self.name)) return self.parent.is_public and name_is_public class Class(Definition): _nest = staticmethod(lambda s: {'def': Method, 'class': NestedClass}[s]) is_public = Function.is_public class NestedClass(Class): is_public = False class Decorator(Value): """A decorator for function, method or class.""" _fields = 'name arguments'.split() class TokenKind(int): def __repr__(self): return "tk.{0}".format(tk.tok_name[self]) class Token(Value): _fields = 'kind value start end source'.split() def __init__(self, *args): super(Token, self).__init__(*args) self.kind = TokenKind(self.kind) class TokenStream(object): def __init__(self, filelike): self._generator = tk.generate_tokens(filelike.readline) self.current = Token(*next(self._generator, None)) self.line = self.current.start[0] def move(self): previous = self.current current = next(self._generator, None) self.current = None if current is None else Token(*current) self.line = self.current.start[0] if self.current else self.line return previous def __iter__(self): while True: if self.current is not None: yield self.current else: return self.move() class AllError(Exception): def __init__(self, message): Exception.__init__( self, message + 'That means pep257 cannot decide which definitions are public. ' 'Variable __all__ should be present at most once in each file, ' "in form `__all__ = ('a_public_function', 'APublicClass', ...)`. " 'More info on __all__: http://stackoverflow.com/q/44834/. ') class Parser(object): def __call__(self, filelike, filename): self.source = filelike.readlines() src = ''.join(self.source) self.stream = TokenStream(StringIO(src)) self.filename = filename self.all = None self.future_imports = defaultdict(lambda: False) self._accumulated_decorators = [] return self.parse_module() current = property(lambda self: self.stream.current) line = property(lambda self: self.stream.line) def consume(self, kind): assert self.stream.move().kind == kind def leapfrog(self, kind, value=None): """Skip tokens in the stream until a certain token kind is reached. If `value` is specified, tokens whose values are different will also be skipped. """ while self.current is not None: if (self.current.kind == kind and (value is None or self.current.value == value)): self.consume(kind) return self.stream.move() def parse_docstring(self): """Parse a single docstring and return its value.""" log.debug("parsing docstring, token is %r (%s)", self.current.kind, self.current.value) while self.current.kind in (tk.COMMENT, tk.NEWLINE, tk.NL): self.stream.move() log.debug("parsing docstring, token is %r (%s)", self.current.kind, self.current.value) if self.current.kind == tk.STRING: docstring = self.current.value self.stream.move() return docstring return None def parse_decorators(self): """Called after first @ is found. Parse decorators into self._accumulated_decorators. Continue to do so until encountering the 'def' or 'class' start token. """ name = [] arguments = [] at_arguments = False while self.current is not None: if (self.current.kind == tk.NAME and self.current.value in ['def', 'class']): # Done with decorators - found function or class proper break elif self.current.kind == tk.OP and self.current.value == '@': # New decorator found. Store the decorator accumulated so far: self._accumulated_decorators.append( Decorator(''.join(name), ''.join(arguments))) # Now reset to begin accumulating the new decorator: name = [] arguments = [] at_arguments = False elif self.current.kind == tk.OP and self.current.value == '(': at_arguments = True elif self.current.kind == tk.OP and self.current.value == ')': # Ignore close parenthesis pass elif self.current.kind == tk.NEWLINE or self.current.kind == tk.NL: # Ignore newlines pass else: # Keep accumulating current decorator's name or argument. if not at_arguments: name.append(self.current.value) else: arguments.append(self.current.value) self.stream.move() # Add decorator accumulated so far self._accumulated_decorators.append( Decorator(''.join(name), ''.join(arguments))) def parse_definitions(self, class_, all=False): """Parse multiple defintions and yield them.""" while self.current is not None: log.debug("parsing defintion list, current token is %r (%s)", self.current.kind, self.current.value) if all and self.current.value == '__all__': self.parse_all() elif self.current.kind == tk.OP and self.current.value == '@': self.consume(tk.OP) self.parse_decorators() elif self.current.value in ['def', 'class']: yield self.parse_definition(class_._nest(self.current.value)) elif self.current.kind == tk.INDENT: self.consume(tk.INDENT) for definition in self.parse_definitions(class_): yield definition elif self.current.kind == tk.DEDENT: self.consume(tk.DEDENT) return elif self.current.value == 'from': self.parse_from_import_statement() else: self.stream.move() def parse_all(self): """Parse the __all__ definition in a module.""" assert self.current.value == '__all__' self.consume(tk.NAME) if self.current.value != '=': raise AllError('Could not evaluate contents of __all__. ') self.consume(tk.OP) if self.current.value not in '([': raise AllError('Could not evaluate contents of __all__. ') if self.current.value == '[': msg = ("%s WARNING: __all__ is defined as a list, this means " "pep257 cannot reliably detect contents of the __all__ " "variable, because it can be mutated. Change __all__ to be " "an (immutable) tuple, to remove this warning. Note, " "pep257 uses __all__ to detect which definitions are " "public, to warn if public definitions are missing " "docstrings. If __all__ is a (mutable) list, pep257 cannot " "reliably assume its contents. pep257 will proceed " "assuming __all__ is not mutated.\n" % self.filename) sys.stderr.write(msg) self.consume(tk.OP) self.all = [] all_content = "(" while self.current.kind != tk.OP or self.current.value not in ")]": if self.current.kind in (tk.NL, tk.COMMENT): pass elif (self.current.kind == tk.STRING or self.current.value == ','): all_content += self.current.value else: kind = token.tok_name[self.current.kind] raise AllError('Unexpected token kind in __all__: %s' % kind) self.stream.move() self.consume(tk.OP) all_content += ")" try: self.all = eval(all_content, {}) except BaseException as e: raise AllError('Could not evaluate contents of __all__.' '\bThe value was %s. The exception was:\n%s' % (all_content, e)) def parse_module(self): """Parse a module (and its children) and return a Module object.""" log.debug("parsing module.") start = self.line docstring = self.parse_docstring() children = list(self.parse_definitions(Module, all=True)) assert self.current is None, self.current end = self.line cls = Module if self.filename.endswith('__init__.py'): cls = Package module = cls(self.filename, self.source, start, end, [], docstring, children, None, self.all) for child in module.children: child.parent = module module.future_imports = self.future_imports log.debug("finished parsing module.") return module def parse_definition(self, class_): """Parse a defintion and return its value in a `class_` object.""" start = self.line self.consume(tk.NAME) name = self.current.value log.debug("parsing %s '%s'", class_.__name__, name) self.stream.move() if self.current.kind == tk.OP and self.current.value == '(': parenthesis_level = 0 while True: if self.current.kind == tk.OP: if self.current.value == '(': parenthesis_level += 1 elif self.current.value == ')': parenthesis_level -= 1 if parenthesis_level == 0: break self.stream.move() if self.current.kind != tk.OP or self.current.value != ':': self.leapfrog(tk.OP, value=":") else: self.consume(tk.OP) if self.current.kind in (tk.NEWLINE, tk.COMMENT): self.leapfrog(tk.INDENT) assert self.current.kind != tk.INDENT docstring = self.parse_docstring() decorators = self._accumulated_decorators self._accumulated_decorators = [] log.debug("parsing nested defintions.") children = list(self.parse_definitions(class_)) log.debug("finished parsing nested defintions for '%s'", name) end = self.line - 1 else: # one-liner definition docstring = self.parse_docstring() decorators = [] # TODO children = [] end = self.line self.leapfrog(tk.NEWLINE) definition = class_(name, self.source, start, end, decorators, docstring, children, None) for child in definition.children: child.parent = definition log.debug("finished parsing %s '%s'. Next token is %r (%s)", class_.__name__, name, self.current.kind, self.current.value) return definition def parse_from_import_statement(self): """Parse a 'from x import y' statement. The purpose is to find __future__ statements. """ log.debug('parsing from/import statement.') assert self.current.value == 'from', self.current.value self.stream.move() if self.current.value != '__future__': return self.stream.move() assert self.current.value == 'import', self.current.value self.stream.move() if self.current.value == '(': self.consume(tk.OP) expected_end_kind = tk.OP else: expected_end_kind = tk.NEWLINE while self.current.kind != expected_end_kind: if self.current.kind != tk.NAME: self.stream.move() continue log.debug("parsing import, token is %r (%s)", self.current.kind, self.current.value) log.debug('found future import: %s', self.current.value) self.future_imports[self.current.value] = True self.consume(tk.NAME) log.debug("parsing import, token is %r (%s)", self.current.kind, self.current.value) if self.current.kind == tk.NAME: self.consume(tk.NAME) # as self.consume(tk.NAME) # new name, irrelevant if self.current.value == ',': self.consume(tk.OP) log.debug("parsing import, token is %r (%s)", self.current.kind, self.current.value) class Error(object): """Error in docstring style.""" # should be overridden by inheriting classes code = None short_desc = None context = None # Options that define how errors are printed: explain = False source = False def __init__(self, *parameters): self.parameters = parameters self.definition = None self.explanation = None def set_context(self, definition, explanation): self.definition = definition self.explanation = explanation filename = property(lambda self: self.definition.module.name) line = property(lambda self: self.definition.start) @property def message(self): ret = '%s: %s' % (self.code, self.short_desc) if self.context is not None: ret += ' (' + self.context % self.parameters + ')' return ret @property def lines(self): source = '' lines = self.definition._source[self.definition._slice] offset = self.definition.start lines_stripped = list(reversed(list(dropwhile(is_blank, reversed(lines))))) numbers_width = 0 for n, line in enumerate(lines_stripped): numbers_width = max(numbers_width, n + offset) numbers_width = len(str(numbers_width)) numbers_width = 6 for n, line in enumerate(lines_stripped): source += '%*d: %s' % (numbers_width, n + offset, line) if n > 5: source += ' ...\n' break return source def __str__(self): self.explanation = '\n'.join(l for l in self.explanation.split('\n') if not is_blank(l)) template = '%(filename)s:%(line)s %(definition)s:\n %(message)s' if self.source and self.explain: template += '\n\n%(explanation)s\n\n%(lines)s\n' elif self.source and not self.explain: template += '\n\n%(lines)s\n' elif self.explain and not self.source: template += '\n\n%(explanation)s\n\n' return template % dict((name, getattr(self, name)) for name in ['filename', 'line', 'definition', 'message', 'explanation', 'lines']) __repr__ = __str__ def __lt__(self, other): return (self.filename, self.line) < (other.filename, other.line) class ErrorRegistry(object): groups = [] class ErrorGroup(object): def __init__(self, prefix, name): self.prefix = prefix self.name = name self.errors = [] def create_error(self, error_code, error_desc, error_context=None): # TODO: check prefix class _Error(Error): code = error_code short_desc = error_desc context = error_context self.errors.append(_Error) return _Error @classmethod def create_group(cls, prefix, name): group = cls.ErrorGroup(prefix, name) cls.groups.append(group) return group @classmethod def get_error_codes(cls): for group in cls.groups: for error in group.errors: yield error.code @classmethod def to_rst(cls): sep_line = '+' + 6 * '-' + '+' + '-' * 71 + '+\n' blank_line = '|' + 78 * ' ' + '|\n' table = '' for group in cls.groups: table += sep_line table += blank_line table += '|' + ('**%s**' % group.name).center(78) + '|\n' table += blank_line for error in group.errors: table += sep_line table += ('|' + error.code.center(6) + '| ' + error.short_desc.ljust(70) + '|\n') table += sep_line return table D1xx = ErrorRegistry.create_group('D1', 'Missing Docstrings') D100 = D1xx.create_error('D100', 'Missing docstring in public module') D101 = D1xx.create_error('D101', 'Missing docstring in public class') D102 = D1xx.create_error('D102', 'Missing docstring in public method') D103 = D1xx.create_error('D103', 'Missing docstring in public function') D104 = D1xx.create_error('D104', 'Missing docstring in public package') D105 = D1xx.create_error('D105', 'Missing docstring in magic method') D2xx = ErrorRegistry.create_group('D2', 'Whitespace Issues') D200 = D2xx.create_error('D200', 'One-line docstring should fit on one line ' 'with quotes', 'found %s') D201 = D2xx.create_error('D201', 'No blank lines allowed before function ' 'docstring', 'found %s') D202 = D2xx.create_error('D202', 'No blank lines allowed after function ' 'docstring', 'found %s') D203 = D2xx.create_error('D203', '1 blank line required before class ' 'docstring', 'found %s') D204 = D2xx.create_error('D204', '1 blank line required after class ' 'docstring', 'found %s') D205 = D2xx.create_error('D205', '1 blank line required between summary line ' 'and description', 'found %s') D206 = D2xx.create_error('D206', 'Docstring should be indented with spaces, ' 'not tabs') D207 = D2xx.create_error('D207', 'Docstring is under-indented') D208 = D2xx.create_error('D208', 'Docstring is over-indented') D209 = D2xx.create_error('D209', 'Multi-line docstring closing quotes should ' 'be on a separate line') D210 = D2xx.create_error('D210', 'No whitespaces allowed surrounding ' 'docstring text') D211 = D2xx.create_error('D211', 'No blank lines allowed before class ' 'docstring', 'found %s') D3xx = ErrorRegistry.create_group('D3', 'Quotes Issues') D300 = D3xx.create_error('D300', 'Use """triple double quotes"""', 'found %s-quotes') D301 = D3xx.create_error('D301', 'Use r""" if any backslashes in a docstring') D302 = D3xx.create_error('D302', 'Use u""" for Unicode docstrings') D4xx = ErrorRegistry.create_group('D4', 'Docstring Content Issues') D400 = D4xx.create_error('D400', 'First line should end with a period', 'not %r') D401 = D4xx.create_error('D401', 'First line should be in imperative mood', '%r, not %r') D402 = D4xx.create_error('D402', 'First line should not be the function\'s ' '"signature"') class AttrDict(dict): def __getattr__(self, item): return self[item] conventions = AttrDict({ 'pep257': set(ErrorRegistry.get_error_codes()) - set(['D203']), }) # General configurations for pep257 run. RunConfiguration = namedtuple('RunConfiguration', ('explain', 'source', 'debug', 'verbose', 'count')) class IllegalConfiguration(Exception): """An exception for illegal configurations.""" pass # Check configuration - used by the ConfigurationParser class. CheckConfiguration = namedtuple('CheckConfiguration', ('checked_codes', 'match', 'match_dir')) def check_initialized(method): """Check that the configuration object was initialized.""" def _decorator(self, *args, **kwargs): if self._arguments is None or self._options is None: raise RuntimeError('using an uninitialized configuration') return method(self, *args, **kwargs) return _decorator class ConfigurationParser(object): """Responsible for parsing configuration from files and CLI. There are 2 types of configurations: Run configurations and Check configurations. Run Configurations: ------------------ Responsible for deciding things that are related to the user interface, e.g. verbosity, debug options, etc. All run configurations default to `False` and are decided only by CLI. Check Configurations: -------------------- Configurations that are related to which files and errors will be checked. These are configurable in 2 ways: using the CLI, and using configuration files. Configuration files are nested within the file system, meaning that the closer a configuration file is to a checked file, the more relevant it will be. For instance, imagine this directory structure: A +-- tox.ini: sets `select=D100` +-- B +-- foo.py +-- tox.ini: sets `add-ignore=D100` Then `foo.py` will not be checked for `D100`. The configuration build algorithm is described in `self._get_config`. Note: If any of `BASE_ERROR_SELECTION_OPTIONS` was selected in the CLI, all configuration files will be ignored and each file will be checked for the error codes supplied in the CLI. """ CONFIG_FILE_OPTIONS = ('convention', 'select', 'ignore', 'add-select', 'add-ignore', 'match', 'match-dir') BASE_ERROR_SELECTION_OPTIONS = ('ignore', 'select', 'convention') DEFAULT_MATCH_RE = '(?!test_).*\.py' DEFAULT_MATCH_DIR_RE = '[^\.].*' DEFAULT_CONVENTION = conventions.pep257 PROJECT_CONFIG_FILES = ('setup.cfg', 'tox.ini', '.pep257') def __init__(self): """Create a configuration parser.""" self._cache = {} self._override_by_cli = None self._options = self._arguments = self._run_conf = None self._parser = self._create_option_parser() # ---------------------------- Public Methods ----------------------------- def get_default_run_configuration(self): """Return a `RunConfiguration` object set with default values.""" options, _ = self._parse_args([]) return self._create_run_config(options) def parse(self): """Parse the configuration. If one of `BASE_ERROR_SELECTION_OPTIONS` was selected, overrides all error codes to check and disregards any error code related configurations from the configuration files. """ self._options, self._arguments = self._parse_args() self._arguments = self._arguments or ['.'] if not self._validate_options(self._options): raise IllegalConfiguration() self._run_conf = self._create_run_config(self._options) config = self._create_check_config(self._options, use_dafaults=False) self._override_by_cli = config @check_initialized def get_user_run_configuration(self): """Return the run configuration for pep257.""" return self._run_conf @check_initialized def get_files_to_check(self): """Generate files and error codes to check on each one. Walk dir trees under `self._arguments` and generate yield filnames that `match` under each directory that `match_dir`. The method locates the configuration for each file name and yields a tuple of (filename, [error_codes]). With every discovery of a new configuration file `IllegalConfiguration` might be raised. """ def _get_matches(config): """Return the `match` and `match_dir` functions for `config`.""" match_func = re(config.match + '$').match match_dir_func = re(config.match_dir + '$').match return match_func, match_dir_func for name in self._arguments: if os.path.isdir(name): for root, dirs, filenames in os.walk(name): config = self._get_config(root) match, match_dir = _get_matches(config) # Skip any dirs that do not match match_dir dirs[:] = [dir for dir in dirs if match_dir(dir)] for filename in filenames: if match(filename): full_path = os.path.join(root, filename) yield full_path, list(config.checked_codes) else: config = self._get_config(name) match, _ = _get_matches(config) if match(name): yield name, list(config.checked_codes) # --------------------------- Private Methods ----------------------------- def _get_config(self, node): """Get and cache the run configuration for `node`. If no configuration exists (not local and not for the parend node), returns and caches a default configuration. The algorithm: ------------- * If the current directory's configuration exists in `self._cache` - return it. * If a configuration file does not exist in this directory: * If the directory is not a root directory: * Cache its configuration as this directory's and return it. * Else: * Cache a default configuration and return it. * Else: * Read the configuration file. * If a parent directory exists AND the configuration file allows inheritance: * Read the parent configuration by calling this function with the parent directory as `node`. * Merge the parent configuration with the current one and cache it. * If the user has specified one of `BASE_ERROR_SELECTION_OPTIONS` in the CLI - return the CLI configuration with the configuration match clauses * Set the `--add-select` and `--add-ignore` CLI configurations. """ path = os.path.abspath(node) path = path if os.path.isdir(path) else os.path.dirname(path) if path in self._cache: return self._cache[path] config_file = self._get_config_file_in_folder(path) if config_file is None: parent_dir, tail = os.path.split(path) if tail: # No configuration file, simply take the parent's. config = self._get_config(parent_dir) else: # There's no configuration file and no parent directory. # Use the default configuration or the one given in the CLI. config = self._create_check_config(self._options) else: # There's a config file! Read it and merge if necessary. options, inherit = self._read_configuration_file(config_file) parent_dir, tail = os.path.split(path) if tail and inherit: # There is a parent dir and we should try to merge. parent_config = self._get_config(parent_dir) config = self._merge_configuration(parent_config, options) else: # No need to merge or parent dir does not exist. config = self._create_check_config(options) # Make the CLI always win final_config = {} for attr in CheckConfiguration._fields: cli_val = getattr(self._override_by_cli, attr) conf_val = getattr(config, attr) final_config[attr] = cli_val if cli_val is not None else conf_val config = CheckConfiguration(**final_config) self._set_add_options(config.checked_codes, self._options) self._cache[path] = config return self._cache[path] def _read_configuration_file(self, path): """Try to read and parse `path` as a pep257 configuration file. If the configurations were illegal (checked with `self._validate_options`), raises `IllegalConfiguration`. Returns (options, should_inherit). """ parser = RawConfigParser() options = None should_inherit = True if parser.read(path) and parser.has_section('pep257'): option_list = dict([(o.dest, o.type or o.action) for o in self._parser.option_list]) # First, read the default values new_options, _ = self._parse_args([]) # Second, parse the configuration pep257_section = 'pep257' for opt in parser.options(pep257_section): if opt == 'inherit': should_inherit = parser.getboolean(pep257_section, opt) continue if opt.replace('_', '-') not in self.CONFIG_FILE_OPTIONS: log.warning("Unknown option '{0}' ignored".format(opt)) continue normalized_opt = opt.replace('-', '_') opt_type = option_list[normalized_opt] if opt_type in ('int', 'count'): value = parser.getint(pep257_section, opt) elif opt_type == 'string': value = parser.get(pep257_section, opt) else: assert opt_type in ('store_true', 'store_false') value = parser.getboolean(pep257_section, opt) setattr(new_options, normalized_opt, value) # Third, fix the set-options options = self._fix_set_options(new_options) if options is not None: if not self._validate_options(options): raise IllegalConfiguration('in file: {0}'.format(path)) return options, should_inherit def _merge_configuration(self, parent_config, child_options): """Merge parent config into the child options. The migration process requires an `options` object for the child in order to distinguish between mutually exclusive codes, add-select and add-ignore error codes. """ # Copy the parent error codes so we won't override them error_codes = copy.deepcopy(parent_config.checked_codes) if self._has_exclusive_option(child_options): error_codes = self._get_exclusive_error_codes(child_options) self._set_add_options(error_codes, child_options) match = child_options.match \ if child_options.match is not None else parent_config.match match_dir = child_options.match_dir \ if child_options.match_dir is not None else parent_config.match_dir return CheckConfiguration(checked_codes=error_codes, match=match, match_dir=match_dir) def _parse_args(self, args=None, values=None): """Parse the options using `self._parser` and reformat the options.""" options, arguments = self._parser.parse_args(args, values) return self._fix_set_options(options), arguments @staticmethod def _create_run_config(options): """Create a `RunConfiguration` object from `options`.""" values = dict([(opt, getattr(options, opt)) for opt in RunConfiguration._fields]) return RunConfiguration(**values) @classmethod def _create_check_config(cls, options, use_dafaults=True): """Create a `CheckConfiguration` object from `options`. If `use_dafaults`, any of the match options that are `None` will be replaced with their default value and the default convention will be set for the checked codes. """ match = cls.DEFAULT_MATCH_RE \ if options.match is None and use_dafaults \ else options.match match_dir = cls.DEFAULT_MATCH_DIR_RE \ if options.match_dir is None and use_dafaults \ else options.match_dir checked_codes = None if cls._has_exclusive_option(options) or use_dafaults: checked_codes = cls._get_checked_errors(options) return CheckConfiguration(checked_codes=checked_codes, match=match, match_dir=match_dir) @classmethod def _get_config_file_in_folder(cls, path): """Look for a configuration file in `path`. If exists return it's full path, otherwise None. """ if os.path.isfile(path): path = os.path.dirname(path) for fn in cls.PROJECT_CONFIG_FILES: config = RawConfigParser() full_path = os.path.join(path, fn) if config.read(full_path) and config.has_section('pep257'): return full_path @staticmethod def _get_exclusive_error_codes(options): """Extract the error codes from the selected exclusive option.""" codes = set(ErrorRegistry.get_error_codes()) checked_codes = None if options.ignore is not None: checked_codes = codes - options.ignore elif options.select is not None: checked_codes = options.select elif options.convention is not None: checked_codes = getattr(conventions, options.convention) # To not override the conventions nor the options - copy them. return copy.deepcopy(checked_codes) @staticmethod def _set_add_options(checked_codes, options): """Set `checked_codes` by the `add_ignore` or `add_select` options.""" checked_codes |= options.add_select checked_codes -= options.add_ignore @classmethod def _get_checked_errors(cls, options): """Extract the codes needed to be checked from `options`.""" checked_codes = cls._get_exclusive_error_codes(options) if checked_codes is None: checked_codes = cls.DEFAULT_CONVENTION cls._set_add_options(checked_codes, options) return checked_codes @classmethod def _validate_options(cls, options): """Validate the mutually exclusive options. Return `True` iff only zero or one of `BASE_ERROR_SELECTION_OPTIONS` was selected. """ for opt1, opt2 in \ itertools.permutations(cls.BASE_ERROR_SELECTION_OPTIONS, 2): if getattr(options, opt1) and getattr(options, opt2): log.error('Cannot pass both {0} and {1}. They are ' 'mutually exclusive.'.format(opt1, opt2)) return False if options.convention and options.convention not in conventions: log.error("Illegal convention '{0}'. Possible conventions: {1}" .format(options.convention, ', '.join(conventions.keys()))) return False return True @classmethod def _has_exclusive_option(cls, options): """Return `True` iff one or more exclusive options were selected.""" return any([getattr(options, opt) is not None for opt in cls.BASE_ERROR_SELECTION_OPTIONS]) @staticmethod def _fix_set_options(options): """Alter the set options from None/strings to sets in place.""" optional_set_options = ('ignore', 'select') mandatory_set_options = ('add_ignore', 'add_select') def _get_set(value_str): """Split `value_str` by the delimiter `,` and return a set. Removes any occurrences of '' in the set. """ return set(value_str.split(',')) - set(['']) for opt in optional_set_options: value = getattr(options, opt) if value is not None: setattr(options, opt, _get_set(value)) for opt in mandatory_set_options: value = getattr(options, opt) if value is None: value = '' if not isinstance(value, Set): value = _get_set(value) setattr(options, opt, value) return options @classmethod def _create_option_parser(cls): """Return an option parser to parse the command line arguments.""" from optparse import OptionParser parser = OptionParser(version=__version__, usage='Usage: pep257 [options] [...]') option = parser.add_option # Run configuration options option('-e', '--explain', action='store_true', default=False, help='show explanation of each error') option('-s', '--source', action='store_true', default=False, help='show source for each error') option('-d', '--debug', action='store_true', default=False, help='print debug information') option('-v', '--verbose', action='store_true', default=False, help='print status information') option('--count', action='store_true', default=False, help='print total number of errors to stdout') # Error check options option('--select', metavar='', default=None, help='choose the basic list of checked errors by ' 'specifying which errors to check for (with a list of ' 'comma-separated error codes). ' 'for example: --select=D101,D202') option('--ignore', metavar='', default=None, help='choose the basic list of checked errors by ' 'specifying which errors to ignore (with a list of ' 'comma-separated error codes). ' 'for example: --ignore=D101,D202') option('--convention', metavar='', default=None, help='choose the basic list of checked errors by specifying an ' 'existing convention. Possible conventions: {0}' .format(', '.join(conventions))) option('--add-select', metavar='', default=None, help='amend the list of errors to check for by specifying ' 'more error codes to check.') option('--add-ignore', metavar='', default=None, help='amend the list of errors to check for by specifying ' 'more error codes to ignore.') # Match clauses option('--match', metavar='', default=None, help=("check only files that exactly match regular " "expression; default is --match='{0}' which matches " "files that don't start with 'test_' but end with " "'.py'").format(cls.DEFAULT_MATCH_RE)) option('--match-dir', metavar='', default=None, help=("search only dirs that exactly match regular " "expression; default is --match-dir='{0}', which " "matches all dirs that don't start with " "a dot").format(cls.DEFAULT_MATCH_DIR_RE)) return parser def check(filenames, select=None, ignore=None): """Generate PEP 257 errors that exist in `filenames` iterable. Only returns errors with error-codes defined in `checked_codes` iterable. Example ------- >>> check(['pep257.py'], checked_codes=['D100']) """ if select is not None and ignore is not None: raise IllegalConfiguration('Cannot pass both select and ignore. ' 'They are mutually exclusive.') elif select is not None: checked_codes = select elif ignore is not None: checked_codes = list(set(ErrorRegistry.get_error_codes()) - set(ignore)) else: checked_codes = conventions.pep257 for filename in filenames: log.info('Checking file %s.', filename) try: with tokenize_open(filename) as file: source = file.read() for error in PEP257Checker().check_source(source, filename): code = getattr(error, 'code', None) if code in checked_codes: yield error except (EnvironmentError, AllError): yield sys.exc_info()[1] except tk.TokenError: yield SyntaxError('invalid syntax in file %s' % filename) def setup_stream_handlers(conf): """Setup logging stream handlers according to the options.""" class StdoutFilter(logging.Filter): def filter(self, record): return record.levelno in (logging.DEBUG, logging.INFO) if log.handlers: for handler in log.handlers: log.removeHandler(handler) stdout_handler = logging.StreamHandler(sys.stdout) stdout_handler.setLevel(logging.WARNING) stdout_handler.addFilter(StdoutFilter()) if conf.debug: stdout_handler.setLevel(logging.DEBUG) elif conf.verbose: stdout_handler.setLevel(logging.INFO) else: stdout_handler.setLevel(logging.WARNING) log.addHandler(stdout_handler) stderr_handler = logging.StreamHandler(sys.stderr) stderr_handler.setLevel(logging.WARNING) log.addHandler(stderr_handler) def run_pep257(): log.setLevel(logging.DEBUG) conf = ConfigurationParser() setup_stream_handlers(conf.get_default_run_configuration()) try: conf.parse() except IllegalConfiguration: return INVALID_OPTIONS_RETURN_CODE run_conf = conf.get_user_run_configuration() # Reset the logger according to the command line arguments setup_stream_handlers(run_conf) log.debug("starting pep257 in debug mode.") Error.explain = run_conf.explain Error.source = run_conf.source errors = [] try: for filename, checked_codes in conf.get_files_to_check(): errors.extend(check((filename,), select=checked_codes)) except IllegalConfiguration: # An illegal configuration file was found during file generation. return INVALID_OPTIONS_RETURN_CODE code = NO_VIOLATIONS_RETURN_CODE count = 0 for error in errors: sys.stderr.write('%s\n' % error) code = VIOLATIONS_RETURN_CODE count += 1 if run_conf.count: print(count) return code parse = Parser() def check_for(kind, terminal=False): def decorator(f): f._check_for = kind f._terminal = terminal return f return decorator class PEP257Checker(object): """Checker for PEP 257. D10x: Missing docstrings D20x: Whitespace issues D30x: Docstring formatting D40x: Docstring content issues """ def check_source(self, source, filename): module = parse(StringIO(source), filename) for definition in module: for check in self.checks: terminate = False if isinstance(definition, check._check_for): error = check(None, definition, definition.docstring) errors = error if hasattr(error, '__iter__') else [error] for error in errors: if error is not None: partition = check.__doc__.partition('.\n') message, _, explanation = partition error.set_context(explanation=explanation, definition=definition) yield error if check._terminal: terminate = True break if terminate: break @property def checks(self): all = [check for check in vars(type(self)).values() if hasattr(check, '_check_for')] return sorted(all, key=lambda check: not check._terminal) @check_for(Definition, terminal=True) def check_docstring_missing(self, definition, docstring): """D10{0,1,2,3}: Public definitions should have docstrings. All modules should normally have docstrings. [...] all functions and classes exported by a module should also have docstrings. Public methods (including the __init__ constructor) should also have docstrings. Note: Public (exported) definitions are either those with names listed in __all__ variable (if present), or those that do not start with a single underscore. """ if (not docstring and definition.is_public or docstring and is_blank(eval(docstring))): codes = {Module: D100, Class: D101, NestedClass: D101, Method: (lambda: D105() if is_magic(definition.name) else D102()), Function: D103, NestedFunction: D103, Package: D104} return codes[type(definition)]() @check_for(Definition) def check_one_liners(self, definition, docstring): """D200: One-liner docstrings should fit on one line with quotes. The closing quotes are on the same line as the opening quotes. This looks better for one-liners. """ if docstring: lines = eval(docstring).split('\n') if len(lines) > 1: non_empty_lines = sum(1 for l in lines if not is_blank(l)) if non_empty_lines == 1: return D200(len(lines)) @check_for(Function) def check_no_blank_before(self, function, docstring): # def """D20{1,2}: No blank lines allowed around function/method docstring. There's no blank line either before or after the docstring. """ # NOTE: This does not take comments into account. # NOTE: This does not take into account functions with groups of code. if docstring: before, _, after = function.source.partition(docstring) blanks_before = list(map(is_blank, before.split('\n')[:-1])) blanks_after = list(map(is_blank, after.split('\n')[1:])) blanks_before_count = sum(takewhile(bool, reversed(blanks_before))) blanks_after_count = sum(takewhile(bool, blanks_after)) if blanks_before_count != 0: yield D201(blanks_before_count) if not all(blanks_after) and blanks_after_count != 0: yield D202(blanks_after_count) @check_for(Class) def check_blank_before_after_class(slef, class_, docstring): """D20{3,4}: Class docstring should have 1 blank line around them. Insert a blank line before and after all docstrings (one-line or multi-line) that document a class -- generally speaking, the class's methods are separated from each other by a single blank line, and the docstring needs to be offset from the first method by a blank line; for symmetry, put a blank line between the class header and the docstring. """ # NOTE: this gives false-positive in this case # class Foo: # # """Docstring.""" # # # # comment here # def foo(): pass if docstring: before, _, after = class_.source.partition(docstring) blanks_before = list(map(is_blank, before.split('\n')[:-1])) blanks_after = list(map(is_blank, after.split('\n')[1:])) blanks_before_count = sum(takewhile(bool, reversed(blanks_before))) blanks_after_count = sum(takewhile(bool, blanks_after)) if blanks_before_count != 0: yield D211(blanks_before_count) if blanks_before_count != 1: yield D203(blanks_before_count) if not all(blanks_after) and blanks_after_count != 1: yield D204(blanks_after_count) @check_for(Definition) def check_blank_after_summary(self, definition, docstring): """D205: Put one blank line between summary line and description. Multi-line docstrings consist of a summary line just like a one-line docstring, followed by a blank line, followed by a more elaborate description. The summary line may be used by automatic indexing tools; it is important that it fits on one line and is separated from the rest of the docstring by a blank line. """ if docstring: lines = eval(docstring).strip().split('\n') if len(lines) > 1: post_summary_blanks = list(map(is_blank, lines[1:])) blanks_count = sum(takewhile(bool, post_summary_blanks)) if blanks_count != 1: return D205(blanks_count) @check_for(Definition) def check_indent(self, definition, docstring): """D20{6,7,8}: The entire docstring should be indented same as code. The entire docstring is indented the same as the quotes at its first line. """ if docstring: before_docstring, _, _ = definition.source.partition(docstring) _, _, indent = before_docstring.rpartition('\n') lines = docstring.split('\n') if len(lines) > 1: lines = lines[1:] # First line does not need indent. indents = [leading_space(l) for l in lines if not is_blank(l)] if set(' \t') == set(''.join(indents) + indent): yield D206() if (len(indents) > 1 and min(indents[:-1]) > indent or indents[-1] > indent): yield D208() if min(indents) < indent: yield D207() @check_for(Definition) def check_newline_after_last_paragraph(self, definition, docstring): """D209: Put multi-line docstring closing quotes on separate line. Unless the entire docstring fits on a line, place the closing quotes on a line by themselves. """ if docstring: lines = [l for l in eval(docstring).split('\n') if not is_blank(l)] if len(lines) > 1: if docstring.split("\n")[-1].strip() not in ['"""', "'''"]: return D209() @check_for(Definition) def check_surrounding_whitespaces(self, definition, docstring): """D210: No whitespaces allowed surrounding docstring text.""" if docstring: lines = eval(docstring).split('\n') if lines[0].startswith(' ') or \ len(lines) == 1 and lines[0].endswith(' '): return D210() @check_for(Definition) def check_triple_double_quotes(self, definition, docstring): r'''D300: Use """triple double quotes""". For consistency, always use """triple double quotes""" around docstrings. Use r"""raw triple double quotes""" if you use any backslashes in your docstrings. For Unicode docstrings, use u"""Unicode triple-quoted strings""". Note: Exception to this is made if the docstring contains """ quotes in its body. ''' if docstring and '"""' in eval(docstring) and docstring.startswith( ("'''", "r'''", "u'''", "ur'''")): # Allow ''' quotes if docstring contains """, because otherwise """ # quotes could not be expressed inside docstring. Not in PEP 257. return if docstring and not docstring.startswith( ('"""', 'r"""', 'u"""', 'ur"""')): quotes = "'''" if "'''" in docstring[:4] else "'" return D300(quotes) @check_for(Definition) def check_backslashes(self, definition, docstring): r'''D301: Use r""" if any backslashes in a docstring. Use r"""raw triple double quotes""" if you use any backslashes (\) in your docstrings. ''' # Just check that docstring is raw, check_triple_double_quotes # ensures the correct quotes. if docstring and '\\' in docstring and not docstring.startswith( ('r', 'ur')): return D301() @check_for(Definition) def check_unicode_docstring(self, definition, docstring): r'''D302: Use u""" for docstrings with Unicode. For Unicode docstrings, use u"""Unicode triple-quoted strings""". ''' if definition.module.future_imports['unicode_literals']: return # Just check that docstring is unicode, check_triple_double_quotes # ensures the correct quotes. if docstring and sys.version_info[0] <= 2: if not is_ascii(docstring) and not docstring.startswith( ('u', 'ur')): return D302() @check_for(Definition) def check_ends_with_period(self, definition, docstring): """D400: First line should end with a period. The [first line of a] docstring is a phrase ending in a period. """ if docstring: summary_line = eval(docstring).strip().split('\n')[0] if not summary_line.endswith('.'): return D400(summary_line[-1]) @check_for(Function) def check_imperative_mood(self, function, docstring): # def context """D401: First line should be in imperative mood: 'Do', not 'Does'. [Docstring] prescribes the function or method's effect as a command: ("Do this", "Return that"), not as a description; e.g. don't write "Returns the pathname ...". """ if docstring: stripped = eval(docstring).strip() if stripped: first_word = stripped.split()[0] if first_word.endswith('s') and not first_word.endswith('ss'): return D401(first_word[:-1], first_word) @check_for(Function) def check_no_signature(self, function, docstring): # def context """D402: First line should not be function's or method's "signature". The one-line docstring should NOT be a "signature" reiterating the function/method parameters (which can be obtained by introspection). """ if docstring: first_line = eval(docstring).strip().split('\n')[0] if function.name + '(' in first_line.replace(' ', ''): return D402() # Somewhat hard to determine if return value is mentioned. # @check(Function) def SKIP_check_return_type(self, function, docstring): """D40x: Return value type should be mentioned. [T]he nature of the return value cannot be determined by introspection, so it should be mentioned. """ if docstring and function.returns_value: if 'return' not in docstring.lower(): return Error() def main(): try: sys.exit(run_pep257()) except KeyboardInterrupt: pass if __name__ == '__main__': main() pep257-0.7.0/src/tests/000077500000000000000000000000001260573666400145105ustar00rootroot00000000000000pep257-0.7.0/src/tests/__init__.py000066400000000000000000000000001260573666400166070ustar00rootroot00000000000000pep257-0.7.0/src/tests/test_cases/000077500000000000000000000000001260573666400166455ustar00rootroot00000000000000pep257-0.7.0/src/tests/test_cases/__init__.py000066400000000000000000000000001260573666400207440ustar00rootroot00000000000000pep257-0.7.0/src/tests/test_cases/expected.py000066400000000000000000000007201260573666400210170ustar00rootroot00000000000000class Expectation(object): """Hold expectation for pep257 violations in tests.""" def __init__(self): self.expected = set([]) def expect(self, *args): """Decorator that expects a certain PEP 257 violation.""" def none(_): return None if len(args) == 1: return lambda f: (self.expected.add((f.__name__, args[0])) or none(f()) or f) self.expected.add(args) pep257-0.7.0/src/tests/test_cases/test.py000066400000000000000000000133451260573666400202040ustar00rootroot00000000000000# encoding: utf-8 # No docstring, so we can test D100 import sys from .expected import Expectation expectation = Expectation() expect = expectation.expect expect('class_', 'D101: Missing docstring in public class') class class_: expect('meta', 'D101: Missing docstring in public class') class meta: """""" @expect('D102: Missing docstring in public method') def method(): pass def _ok_since_private(): pass @expect('D102: Missing docstring in public method') def __init__(self=None): pass @expect('D105: Missing docstring in magic method') def __str__(self=None): pass @expect('D102: Missing docstring in public method') def __call__(self=None, x=None, y=None, z=None): pass @expect('D103: Missing docstring in public function') def function(): """ """ def ok_since_nested(): pass @expect('D103: Missing docstring in public function') def nested(): '' @expect('D200: One-line docstring should fit on one line with quotes ' '(found 3)') def asdlkfasd(): """ Wrong. """ @expect('D201: No blank lines allowed before function docstring (found 1)') def leading_space(): """Leading space.""" @expect('D202: No blank lines allowed after function docstring (found 1)') def trailing_space(): """Leading space.""" pass @expect('D201: No blank lines allowed before function docstring (found 1)') @expect('D202: No blank lines allowed after function docstring (found 1)') def trailing_and_leading_space(): """Trailing and leading space.""" pass expect('LeadingSpaceMissing', 'D203: 1 blank line required before class docstring (found 0)') class LeadingSpaceMissing: """Leading space missing.""" expect('WithLeadingSpace', 'D211: No blank lines allowed before class docstring (found 1)') class WithLeadingSpace: """With leading space.""" expect('TrailingSpace', 'D204: 1 blank line required after class docstring (found 0)') expect('TrailingSpace', 'D211: No blank lines allowed before class docstring (found 1)') class TrailingSpace: """TrailingSpace.""" pass expect('LeadingAndTrailingSpaceMissing', 'D203: 1 blank line required before class docstring (found 0)') expect('LeadingAndTrailingSpaceMissing', 'D204: 1 blank line required after class docstring (found 0)') class LeadingAndTrailingSpaceMissing: """Leading and trailing space missing.""" pass @expect('D205: 1 blank line required between summary line and description ' '(found 0)') def multi_line_zero_separating_blanks(): """Summary. Description. """ @expect('D205: 1 blank line required between summary line and description ' '(found 2)') def multi_line_two_separating_blanks(): """Summary. Description. """ def multi_line_one_separating_blanks(): """Summary. Description. """ @expect('D207: Docstring is under-indented') def asdfsdf(): """Summary. Description. """ @expect('D207: Docstring is under-indented') def asdsdfsdffsdf(): """Summary. Description. """ @expect('D208: Docstring is over-indented') def asdfsdsdf24(): """Summary. Description. """ @expect('D208: Docstring is over-indented') def asdfsdsdfsdf24(): """Summary. Description. """ @expect('D208: Docstring is over-indented') def asdfsdfsdsdsdfsdf24(): """Summary. Description. """ @expect('D209: Multi-line docstring closing quotes should be on a separate ' 'line') def asdfljdf24(): """Summary. Description.""" @expect('D210: No whitespaces allowed surrounding docstring text') def endswith(): """Whitespace at the end. """ @expect('D210: No whitespaces allowed surrounding docstring text') def around(): """ Whitespace at everywhere. """ @expect('D210: No whitespaces allowed surrounding docstring text') def multiline(): """ Whitespace at the begining. This is the end. """ @expect('D300: Use """triple double quotes""" (found \'\'\'-quotes)') def lsfklkjllkjl(): r'''Summary.''' @expect('D300: Use """triple double quotes""" (found \'-quotes)') def lalskklkjllkjl(): r'Summary.' @expect('D301: Use r""" if any backslashes in a docstring') def lalsksdewnlkjl(): """Sum\\mary.""" if sys.version_info[0] <= 2: @expect('D302: Use u""" for Unicode docstrings') def lasewnlkjl(): """Юникод.""" @expect("D400: First line should end with a period (not 'y')") def lwnlkjl(): """Summary""" @expect("D401: First line should be in imperative mood ('Return', not " "'Returns')") def liouiwnlkjl(): """Returns foo.""" @expect('D402: First line should not be the function\'s "signature"') def foobar(): """Signature: foobar().""" def new_209(): """First line. More lines. """ pass def old_209(): """One liner. Multi-line comments. OK to have extra blank line """ @expect("D103: Missing docstring in public function") def oneliner_d102(): return @expect("D400: First line should end with a period (not 'r')") def oneliner_withdoc(): """One liner""" @expect("D207: Docstring is under-indented") def docstring_start_in_same_line(): """First Line. Second Line """ def function_with_lambda_arg(x=lambda y: y): """A valid docstring.""" def a_following_valid_function(x): """Check for a bug where the previous function caused an assertion. The assertion was caused in the next function, so this one is necessary. """ def outer_function(): """Do something.""" def inner_function(): """Do inner something.""" return 0 expect(__file__ if __file__[-1] != 'c' else __file__[:-1], 'D100: Missing docstring in public module') pep257-0.7.0/src/tests/test_cases/unicode_literals.py000066400000000000000000000004011260573666400225370ustar00rootroot00000000000000# -*- coding: utf-8 -*- """This is a module.""" from __future__ import unicode_literals from .expected import Expectation expectation = Expectation() expect = expectation.expect def with_unicode_docstring_without_u(): r"""Check unicode: \u2611.""" pep257-0.7.0/src/tests/test_decorators.py000066400000000000000000000154251260573666400202750ustar00rootroot00000000000000"""Unit test for pep257 module decorator handling. Use tox or py.test to run the test suite. """ try: from StringIO import StringIO except ImportError: from io import StringIO import textwrap from .. import pep257 class TestParser: """Check parsing of Python source code.""" def test_parse_class_single_decorator(self): """Class decorator is recorded in class instance.""" code = textwrap.dedent("""\ @first_decorator class Foo: pass """) module = pep257.parse(StringIO(code), 'dummy.py') decorators = module.children[0].decorators assert 1 == len(decorators) assert 'first_decorator' == decorators[0].name assert '' == decorators[0].arguments def test_parse_class_decorators(self): """Class decorators are accumulated together with their arguments.""" code = textwrap.dedent("""\ @first_decorator @second.decorator(argument) @third.multi.line( decorator, key=value, ) class Foo: pass """) module = pep257.parse(StringIO(code), 'dummy.py') defined_class = module.children[0] decorators = defined_class.decorators assert 3 == len(decorators) assert 'first_decorator' == decorators[0].name assert '' == decorators[0].arguments assert 'second.decorator' == decorators[1].name assert 'argument' == decorators[1].arguments assert 'third.multi.line' == decorators[2].name assert 'decorator,key=value,' == decorators[2].arguments def test_parse_class_nested_decorator(self): """Class decorator is recorded even for nested classes.""" code = textwrap.dedent("""\ @parent_decorator class Foo: pass @first_decorator class NestedClass: pass """) module = pep257.parse(StringIO(code), 'dummy.py') nested_class = module.children[0].children[0] decorators = nested_class.decorators assert 1 == len(decorators) assert 'first_decorator' == decorators[0].name assert '' == decorators[0].arguments def test_parse_method_single_decorator(self): """Method decorators are accumulated.""" code = textwrap.dedent("""\ class Foo: @first_decorator def method(self): pass """) module = pep257.parse(StringIO(code), 'dummy.py') defined_class = module.children[0] decorators = defined_class.children[0].decorators assert 1 == len(decorators) assert 'first_decorator' == decorators[0].name assert '' == decorators[0].arguments def test_parse_method_decorators(self): """Multiple method decorators are accumulated along with their args.""" code = textwrap.dedent("""\ class Foo: @first_decorator @second.decorator(argument) @third.multi.line( decorator, key=value, ) def method(self): pass """) module = pep257.parse(StringIO(code), 'dummy.py') defined_class = module.children[0] decorators = defined_class.children[0].decorators assert 3 == len(decorators) assert 'first_decorator' == decorators[0].name assert '' == decorators[0].arguments assert 'second.decorator' == decorators[1].name assert 'argument' == decorators[1].arguments assert 'third.multi.line' == decorators[2].name assert 'decorator,key=value,' == decorators[2].arguments def test_parse_function_decorator(self): """A function decorator is also accumulated.""" code = textwrap.dedent("""\ @first_decorator def some_method(self): pass """) module = pep257.parse(StringIO(code), 'dummy.py') decorators = module.children[0].decorators assert 1 == len(decorators) assert 'first_decorator' == decorators[0].name assert '' == decorators[0].arguments def test_parse_method_nested_decorator(self): """Method decorators are accumulated for nested methods.""" code = textwrap.dedent("""\ class Foo: @parent_decorator def method(self): @first_decorator def nested_method(arg): pass """) module = pep257.parse(StringIO(code), 'dummy.py') defined_class = module.children[0] decorators = defined_class.children[0].children[0].decorators assert 1 == len(decorators) assert 'first_decorator' == decorators[0].name assert '' == decorators[0].arguments class TestMethod: """Unit test for Method class.""" def makeMethod(self, name='someMethodName'): """Return a simple method instance.""" children = [] all = ['ClassName'] source = textwrap.dedent("""\ class ClassName: def %s(self): """ % (name)) module = pep257.Module('module_name', source, 0, 1, [], 'Docstring for module', [], None, all) cls = pep257.Class('ClassName', source, 0, 1, [], 'Docstring for class', children, module, all) return pep257.Method(name, source, 0, 1, [], 'Docstring for method', children, cls, all) def test_is_public_normal(self): """Methods are normally public, even if decorated.""" method = self.makeMethod('methodName') method.decorators = [pep257.Decorator('some_decorator', [])] assert method.is_public def test_is_public_setter(self): """Setter methods are considered private.""" method = self.makeMethod('methodName') method.decorators = [ pep257.Decorator('some_decorator', []), pep257.Decorator('methodName.setter', []), ] assert not method.is_public def test_is_public_deleter(self): """Deleter methods are also considered private.""" method = self.makeMethod('methodName') method.decorators = [ pep257.Decorator('methodName.deleter', []), pep257.Decorator('another_decorator', []), ] assert not method.is_public def test_is_public_trick(self): """Common prefix does not necessarily indicate private.""" method = self.makeMethod("foo") method.decorators = [ pep257.Decorator('foobar', []), pep257.Decorator('foobar.baz', []), ] assert method.is_public pep257-0.7.0/src/tests/test_definitions.py000066400000000000000000000114311260573666400204340ustar00rootroot00000000000000import os from ..pep257 import (StringIO, TokenStream, Parser, Error, check, Module, Class, Method, Function, NestedFunction, ErrorRegistry) _ = type('', (), dict(__repr__=lambda *a: '_', __eq__=lambda *a: True))() parse = Parser() source = ''' """Module.""" __all__ = ('a', 'b' 'c',) def function(): "Function." def nested_1(): """Nested.""" if True: def nested_2(): pass class class_(object): """Class.""" def method_1(self): """Method.""" def method_2(self): def nested_3(self): """Nested.""" ''' source_alt = ''' __all__ = ['a', 'b' 'c',] ''' source_alt_nl_at_bracket = ''' __all__ = [ # Inconvenient comment. 'a', 'b' 'c',] ''' source_unicode_literals = ''' from __future__ import unicode_literals ''' source_multiple_future_imports = ''' from __future__ import (nested_scopes as ns, unicode_literals) ''' def test_parser(): dunder_all = ('a', 'bc') module = parse(StringIO(source), 'file.py') assert len(list(module)) == 8 assert Module('file.py', _, 1, len(source.split('\n')), _, '"""Module."""', _, _, dunder_all, {}) == \ module function, class_ = module.children assert Function('function', _, _, _, _, '"Function."', _, module) == function assert Class('class_', _, _, _, _, '"""Class."""', _, module) == class_ nested_1, nested_2 = function.children assert NestedFunction('nested_1', _, _, _, _, '"""Nested."""', _, function) == nested_1 assert NestedFunction('nested_2', _, _, _, _, None, _, function) == nested_2 assert nested_1.is_public is False method_1, method_2 = class_.children assert method_1.parent == method_2.parent == class_ assert Method('method_1', _, _, _, _, '"""Method."""', _, class_) == method_1 assert Method('method_2', _, _, _, _, None, _, class_) == method_2 nested_3, = method_2.children assert NestedFunction('nested_3', _, _, _, _, '"""Nested."""', _, method_2) == nested_3 assert nested_3.module == module assert nested_3.all == dunder_all module = parse(StringIO(source_alt), 'file_alt.py') assert Module('file_alt.py', _, 1, len(source_alt.split('\n')), _, None, _, _, dunder_all, {}) == module module = parse(StringIO(source_alt_nl_at_bracket), 'file_alt_nl.py') assert Module('file_alt_nl.py', _, 1, len(source_alt_nl_at_bracket.split('\n')), _, None, _, _, dunder_all, {}) == module module = parse(StringIO(source_unicode_literals), 'file_ucl.py') assert Module('file_ucl.py', _, 1, _, _, None, _, _, _, {'unicode_literals': True}) == module module = parse(StringIO(source_multiple_future_imports), 'file_mfi.py') assert Module('file_mfi.py', _, 1, _, _, None, _, _, _, {'unicode_literals': True, 'nested_scopes': True}) \ == module assert module.future_imports['unicode_literals'] def _test_module(): module = Module(source, 'module.py') assert module.source == source assert module.parent is None assert module.name == 'module' assert module.docstring == '"""Module docstring."""' assert module.is_public function, = module.children assert function.source.startswith('def function') assert function.source.endswith('pass\n') assert function.parent is module assert function.name == 'function' assert function.docstring == '"""Function docstring."""' def test_token_stream(): stream = TokenStream(StringIO('hello#world')) assert stream.current.value == 'hello' assert stream.line == 1 assert stream.move().value == 'hello' assert stream.current.value == '#world' assert stream.line == 1 def test_pep257(): """Run domain-specific tests from test.py file.""" test_cases = ('test', 'unicode_literals') for test_case in test_cases: case_module = __import__('test_cases.{0}'.format(test_case), globals=globals(), locals=locals(), fromlist=['expectation'], level=1) # from .test_cases import test results = list(check([os.path.join(os.path.dirname(__file__), 'test_cases', test_case + '.py')], select=set(ErrorRegistry.get_error_codes()))) for error in results: assert isinstance(error, Error) results = set([(e.definition.name, e.message) for e in results]) assert case_module.expectation.expected == results pep257-0.7.0/src/tests/test_pep257.py000066400000000000000000000625261260573666400171560ustar00rootroot00000000000000# -*- coding: utf-8 -*- """Use tox or py.test to run the test-suite.""" from __future__ import with_statement from collections import namedtuple from functools import partial import sys import os import mock import shlex import shutil import tempfile import textwrap import subprocess from .. import pep257 __all__ = () class Pep257Env(object): """An isolated environment where pep257.py can be run. Since running pep257.py as a script is affected by local config files, it's important that tests will run in an isolated environment. This class should be used as a context manager and offers utility methods for adding files to the environment and changing the environment's configuration. """ Result = namedtuple('Result', ('out', 'err', 'code')) def __init__(self): self.tempdir = None def write_config(self, prefix='', **kwargs): """Change an environment config file. Applies changes to `tox.ini` relative to `tempdir/prefix`. If the given path prefix does not exist it is created. """ base = os.path.join(self.tempdir, prefix) if prefix else self.tempdir if not os.path.isdir(base): self.makedirs(base) with open(os.path.join(base, 'tox.ini'), 'wt') as conf: conf.write("[pep257]\n") for k, v in kwargs.items(): conf.write("{0} = {1}\n".format(k.replace('_', '-'), v)) def open(self, path, *args, **kwargs): """Open a file in the environment. The file path should be relative to the base of the environment. """ return open(os.path.join(self.tempdir, path), *args, **kwargs) def makedirs(self, path, *args, **kwargs): """Create a directory in a path relative to the environment base.""" os.makedirs(os.path.join(self.tempdir, path), *args, **kwargs) def invoke_pep257(self, args="", target=None): """Run pep257.py on the environment base folder with the given args. If `target` is not None, will run pep257 on `target` instead of the environment base folder. """ pep257_location = os.path.join(os.path.dirname(__file__), '..', 'pep257.py') run_target = self.tempdir if target is None else \ os.path.join(self.tempdir, target) cmd = shlex.split("python {0} {1} {2}" .format(pep257_location, run_target, args), posix=False) p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, err = p.communicate() return self.Result(out=out.decode('utf-8'), err=err.decode('utf-8'), code=p.returncode) def __enter__(self): self.tempdir = tempfile.mkdtemp() # Make sure we won't be affected by other config files self.write_config() return self def __exit__(self, *args, **kwargs): shutil.rmtree(self.tempdir) pass def parse_errors(err): """Parse `err` to a dictionary of {filename: error_codes}. This is for test purposes only. All file names should be different. """ result = {} py_ext = '.py' lines = err.split('\n') while lines: curr_line = lines.pop(0) filename = curr_line[:curr_line.find(py_ext) + len(py_ext)] if os.path.isfile(filename): if lines: err_line = lines.pop(0).strip() err_code = err_line.split(':')[0] basename = os.path.basename(filename) result.setdefault(basename, set()).add(err_code) return result def test_pep257_conformance(): relative = partial(os.path.join, os.path.dirname(__file__)) errors = list(pep257.check([relative('..', 'pep257.py'), relative('test_pep257.py')], select=pep257.conventions.pep257)) assert errors == [], errors def test_ignore_list(): function_to_check = textwrap.dedent(''' def function_with_bad_docstring(foo): """ does spacinwithout a period in the end no blank line after one-liner is bad. Also this - """ return foo ''') expected_error_codes = set(('D100', 'D400', 'D401', 'D205', 'D209', 'D210')) mock_open = mock.mock_open(read_data=function_to_check) from .. import pep257 with mock.patch.object(pep257, 'tokenize_open', mock_open, create=True): errors = tuple(pep257.check(['filepath'])) error_codes = set(error.code for error in errors) assert error_codes == expected_error_codes # We need to recreate the mock, otherwise the read file is empty mock_open = mock.mock_open(read_data=function_to_check) with mock.patch.object(pep257, 'tokenize_open', mock_open, create=True): errors = tuple(pep257.check(['filepath'], ignore=['D100', 'D202'])) error_codes = set(error.code for error in errors) assert error_codes == expected_error_codes - set(('D100', 'D202')) def test_config_file(): """Test that options are correctly loaded from a config file. This test create a temporary directory and creates two files in it: a Python file that has two pep257 violations (D100 and D103) and a config file (tox.ini). This test alternates settings in the config file and checks that pep257 gives the correct output. """ with Pep257Env() as env: with env.open('example.py', 'wt') as example: example.write(textwrap.dedent("""\ def foo(): pass """)) env.write_config(ignore='D100') _, err, code = env.invoke_pep257() assert code == 1 assert 'D100' not in err assert 'D103' in err env.write_config(ignore='') _, err, code = env.invoke_pep257() assert code == 1 assert 'D100' in err assert 'D103' in err env.write_config(ignore='D100,D103') _, err, code = env.invoke_pep257() assert code == 0 assert 'D100' not in err assert 'D103' not in err def test_verbose(): """Test that passing --verbose to pep257 prints more information.""" with Pep257Env() as env: with env.open('example.py', 'wt') as example: example.write('"""Module docstring."""\n') out, _, code = env.invoke_pep257() assert code == 0 assert 'example.py' not in out out, _, code = env.invoke_pep257(args="--verbose") assert code == 0 assert 'example.py' in out def test_count(): """Test that passing --count to pep257 correctly prints the error num.""" with Pep257Env() as env: with env.open('example.py', 'wt') as example: example.write(textwrap.dedent("""\ def foo(): pass """)) out, err, code = env.invoke_pep257(args='--count') assert code == 1 assert '2' in out def test_select_cli(): """Test choosing error codes with `--select` in the CLI.""" with Pep257Env() as env: with env.open('example.py', 'wt') as example: example.write(textwrap.dedent("""\ def foo(): pass """)) _, err, code = env.invoke_pep257(args="--select=D100") assert code == 1 assert 'D100' in err assert 'D103' not in err def test_select_config(): """Test choosing error codes with `select` in the config file.""" with Pep257Env() as env: with env.open('example.py', 'wt') as example: example.write(textwrap.dedent("""\ def foo(): pass """)) env.write_config(select="D100") _, err, code = env.invoke_pep257() assert code == 1 assert 'D100' in err assert 'D103' not in err def test_add_select_cli(): """Test choosing error codes with --add-select in the CLI.""" with Pep257Env() as env: with env.open('example.py', 'wt') as example: example.write(textwrap.dedent("""\ class Foo(object): def foo(): pass """)) env.write_config(select="D100") _, err, code = env.invoke_pep257(args="--add-select=D101") assert code == 1 assert 'D100' in err assert 'D101' in err assert 'D103' not in err def test_add_ignore_cli(): """Test choosing error codes with --add-ignore in the CLI.""" with Pep257Env() as env: with env.open('example.py', 'wt') as example: example.write(textwrap.dedent("""\ class Foo(object): def foo(): pass """)) env.write_config(select="D100,D101") _, err, code = env.invoke_pep257(args="--add-ignore=D101") assert code == 1 assert 'D100' in err assert 'D101' not in err assert 'D103' not in err def test_conflicting_select_ignore_config(): """Test that select and ignore are mutually exclusive.""" with Pep257Env() as env: env.write_config(select="D100", ignore="D101") _, err, code = env.invoke_pep257() assert code == 2 assert 'mutually exclusive' in err def test_conflicting_select_convention_config(): """Test that select and convention are mutually exclusive.""" with Pep257Env() as env: env.write_config(select="D100", convention="pep257") _, err, code = env.invoke_pep257() assert code == 2 assert 'mutually exclusive' in err def test_conflicting_ignore_convention_config(): """Test that select and convention are mutually exclusive.""" with Pep257Env() as env: env.write_config(ignore="D100", convention="pep257") _, err, code = env.invoke_pep257() assert code == 2 assert 'mutually exclusive' in err def test_unicode_raw(): """Test acceptance of unicode raw docstrings for python 2.x.""" if sys.version_info[0] >= 3: return # ur"" is a syntax error in python 3.x # This is all to avoid a syntax error for python 3.2 from codecs import unicode_escape_decode def u(x): return unicode_escape_decode(x)[0] with Pep257Env() as env: with env.open('example.py', 'wt') as example: example.write(textwrap.dedent(u('''\ # -*- coding: utf-8 -*- def foo(): ur"""Check unicode: \u2611 and raw: \\\\\\\\.""" ''').encode('utf-8'))) env.write_config(ignore='D100', verbose=True) out, err, code = env.invoke_pep257() assert code == 0 assert 'D301' not in err assert 'D302' not in err def test_missing_docstring_in_package(): with Pep257Env() as env: with env.open('__init__.py', 'wt') as init: pass # an empty package file out, err, code = env.invoke_pep257() assert code == 1 assert 'D100' not in err # shouldn't be treated as a module assert 'D104' in err # missing docstring in package def test_illegal_convention(): with Pep257Env() as env: out, err, code = env.invoke_pep257('--convention=illegal_conv') assert code == 2 assert "Illegal convention 'illegal_conv'." in err assert 'Possible conventions: pep257' in err def test_empty_select_cli(): """Test excluding all error codes with `--select=` in the CLI.""" with Pep257Env() as env: with env.open('example.py', 'wt') as example: example.write(textwrap.dedent("""\ def foo(): pass """)) _, _, code = env.invoke_pep257(args="--select=") assert code == 0 def test_empty_select_config(): """Test excluding all error codes with `select=` in the config file.""" with Pep257Env() as env: with env.open('example.py', 'wt') as example: example.write(textwrap.dedent("""\ def foo(): pass """)) env.write_config(select="") _, _, code = env.invoke_pep257() assert code == 0 def test_empty_select_with_added_error(): """Test excluding all errors but one.""" with Pep257Env() as env: with env.open('example.py', 'wt') as example: example.write(textwrap.dedent("""\ def foo(): pass """)) env.write_config(select="") _, err, code = env.invoke_pep257(args="--add-select=D100") assert code == 1 assert 'D100' in err assert 'D101' not in err assert 'D103' not in err def test_pep257_convention(): """Test that the 'pep257' convention options has the correct errors.""" with Pep257Env() as env: with env.open('example.py', 'wt') as example: example.write(textwrap.dedent(''' class Foo(object): """Docstring for this class""" def foo(): pass ''')) env.write_config(convention="pep257") _, err, code = env.invoke_pep257() assert code == 1 assert 'D100' in err assert 'D211' in err assert 'D203' not in err def test_config_file_inheritance(): """Test configuration files inheritance. The test creates 2 configuration files: env_base +-- tox.ini | This configuration will set `select=`. +-- A +-- tox.ini | This configuration will set `inherit=false`. +-- test.py The file will contain code that violates D100,D103. When invoking pep257, the first config file found in the base directory will set `select=`, so no error codes should be checked. The `A/tox.ini` configuration file sets `inherit=false` but has an empty configuration, therefore the default convention will be checked. We expect pep257 to ignore the `select=` configuration and raise all the errors stated above. """ with Pep257Env() as env: env.write_config(select='') env.write_config(prefix='A', inherit=False) with env.open(os.path.join('A', 'test.py'), 'wt') as test: test.write(textwrap.dedent("""\ def bar(): pass """)) _, err, code = env.invoke_pep257() assert code == 1 assert 'D100' in err assert 'D103' in err def test_config_file_cumulative_add_ignores(): """Test that add-ignore is cumulative. env_base +-- tox.ini | This configuration will set `select=D100,D103` and `add-ignore=D100`. +-- base.py | Will violate D100,D103 +-- A +-- tox.ini | This configuration will set `add-ignore=D103`. +-- a.py Will violate D100,D103. The desired result is that `base.py` will fail with D103 and `a.py` will pass. """ with Pep257Env() as env: env.write_config(select='D100,D103', add_ignore='D100') env.write_config(prefix='A', add_ignore='D103') test_content = textwrap.dedent("""\ def foo(): pass """) with env.open('base.py', 'wt') as test: test.write(test_content) with env.open(os.path.join('A', 'a.py'), 'wt') as test: test.write(test_content) _, err, code = env.invoke_pep257() err = parse_errors(err) assert code == 1 assert 'base.py' in err assert 'a.py' not in err assert 'D100' not in err['base.py'] assert 'D103' in err['base.py'] def test_config_file_cumulative_add_select(): """Test that add-select is cumulative. env_base +-- tox.ini | This configuration will set `select=` and `add-select=D100`. +-- base.py | Will violate D100,D103 +-- A +-- tox.ini | This configuration will set `add-select=D103`. +-- a.py Will violate D100,D103. The desired result is that `base.py` will fail with D100 and `a.py` will fail with D100,D103. """ with Pep257Env() as env: env.write_config(select='', add_select='D100') env.write_config(prefix='A', add_select='D103') test_content = textwrap.dedent("""\ def foo(): pass """) with env.open('base.py', 'wt') as test: test.write(test_content) with env.open(os.path.join('A', 'a.py'), 'wt') as test: test.write(test_content) _, err, code = env.invoke_pep257() err = parse_errors(err) assert code == 1 assert 'base.py' in err assert 'a.py' in err assert err['base.py'] == set(['D100']) assert err['a.py'] == set(['D100', 'D103']) def test_config_file_convention_overrides_select(): """Test that conventions override selected errors. env_base +-- tox.ini | This configuration will set `select=D103`. +-- base.py | Will violate D100. +-- A +-- tox.ini | This configuration will set `convention=pep257`. +-- a.py Will violate D100. The expected result is that `base.py` will be clear of errors and `a.py` will violate D100. """ with Pep257Env() as env: env.write_config(select='D103') env.write_config(prefix='A', convention='pep257') test_content = "" with env.open('base.py', 'wt') as test: test.write(test_content) with env.open(os.path.join('A', 'a.py'), 'wt') as test: test.write(test_content) _, err, code = env.invoke_pep257() assert code == 1 assert 'D100' in err assert 'base.py' not in err assert 'a.py' in err def test_cli_overrides_config_file(): """Test that the CLI overrides error codes selected in the config file. env_base +-- tox.ini | This configuration will set `select=D103` and `match-dir=foo`. +-- base.py | Will violate D100. +-- A +-- a.py Will violate D100,D103. We shall run pep257 with `--convention=pep257`. We expect `base.py` to be checked and violate `D100` and that `A/a.py` will not be checked because of `match-dir=foo` in the config file. """ with Pep257Env() as env: env.write_config(select='D103', match_dir='foo') with env.open('base.py', 'wt') as test: test.write("") env.makedirs('A') with env.open(os.path.join('A', 'a.py'), 'wt') as test: test.write(textwrap.dedent("""\ def foo(): pass """)) _, err, code = env.invoke_pep257(args="--convention=pep257") assert code == 1 assert 'D100' in err assert 'D103' not in err assert 'base.py' in err assert 'a.py' not in err def test_cli_match_overrides_config_file(): """Test that the CLI overrides the match clauses in the config file. env_base +-- tox.ini | This configuration will set `match-dir=foo`. +-- base.py | Will violate D100,D103. +-- A +-- tox.ini | This configuration will set `match=bar.py`. +-- a.py Will violate D100. We shall run pep257 with `--match=a.py` and `--match-dir=A`. We expect `base.py` will not be checked and that `A/a.py` will be checked. """ with Pep257Env() as env: env.write_config(match_dir='foo') env.write_config(prefix='A', match='bar.py') with env.open('base.py', 'wt') as test: test.write(textwrap.dedent("""\ def foo(): pass """)) with env.open(os.path.join('A', 'a.py'), 'wt') as test: test.write("") _, err, code = env.invoke_pep257(args="--match=a.py --match-dir=A") assert code == 1 assert 'D100' in err assert 'D103' not in err assert 'base.py' not in err assert 'a.py' in err def test_config_file_convention_overrides_ignore(): """Test that conventions override ignored errors. env_base +-- tox.ini | This configuration will set `ignore=D100,D103`. +-- base.py | Will violate D100,D103. +-- A +-- tox.ini | This configuration will set `convention=pep257`. +-- a.py Will violate D100,D103. The expected result is that `base.py` will be clear of errors and `a.py` will violate D103. """ with Pep257Env() as env: env.write_config(ignore='D100,D103') env.write_config(prefix='A', convention='pep257') test_content = textwrap.dedent("""\ def foo(): pass """) with env.open('base.py', 'wt') as test: test.write(test_content) with env.open(os.path.join('A', 'a.py'), 'wt') as test: test.write(test_content) _, err, code = env.invoke_pep257() assert code == 1 assert 'D100' in err assert 'D103' in err assert 'base.py' not in err assert 'a.py' in err def test_config_file_ignore_overrides_select(): """Test that ignoring any error overrides selecting errors. env_base +-- tox.ini | This configuration will set `select=D100`. +-- base.py | Will violate D100,D101,D102. +-- A +-- tox.ini | This configuration will set `ignore=D102`. +-- a.py Will violate D100,D101,D102. The expected result is that `base.py` will violate D100 and `a.py` will violate D100,D101. """ with Pep257Env() as env: env.write_config(select='D100') env.write_config(prefix='A', ignore='D102') test_content = textwrap.dedent("""\ class Foo(object): def bar(): pass """) with env.open('base.py', 'wt') as test: test.write(test_content) with env.open(os.path.join('A', 'a.py'), 'wt') as test: test.write(test_content) _, err, code = env.invoke_pep257() err = parse_errors(err) assert code == 1 assert 'base.py' in err assert 'a.py' in err assert err['base.py'] == set(['D100']) assert err['a.py'] == set(['D100', 'D101']) def test_config_file_nearest_to_checked_file(): """Test that the configuration to each file is the nearest one. In this test there will be 2 identical files in 2 branches in the directory tree. Both of them will violate the same error codes, but their config files will contain different ignores. env_base +-- tox.ini | This configuration will set `convention=pep257` and `add-ignore=D100` +-- base.py | Will violate D100,D101,D102. +-- A | +-- a.py | Will violate D100,D101,D102. +-- B +-- tox.ini | Will set `add-ignore=D101` +-- b.py Will violate D100,D101,D102. We should see that `a.py` and `base.py` act the same and violate D101,D102 (since they are both configured by `tox.ini`) and that `b.py` violates D102, since it's configured by `B/tox.ini` as well. """ with Pep257Env() as env: env.write_config(convention='pep257', add_ignore='D100') env.write_config(prefix='B', add_ignore='D101') test_content = textwrap.dedent("""\ class Foo(object): def bar(): pass """) with env.open('base.py', 'wt') as test: test.write(test_content) env.makedirs('A') with env.open(os.path.join('A', 'a.py'), 'wt') as test: test.write(test_content) with env.open(os.path.join('B', 'b.py'), 'wt') as test: test.write(test_content) _, err, code = env.invoke_pep257() err = parse_errors(err) assert code == 1 assert 'base.py' in err assert 'a.py' in err assert 'b.py' in err assert err['base.py'] == set(['D101', 'D102']) assert err['a.py'] == set(['D101', 'D102']) assert err['b.py'] == set(['D102']) def test_config_file_nearest_match_re(): """Test that the `match` and `match-dir` options are handled correctly. env_base +-- tox.ini | This configuration will set `convention=pep257` and `add-ignore=D100`. +-- A +-- tox.ini | Will set `match-dir=C`. +-- B | +-- b.py | Will violate D100,D103. +-- C +-- tox.ini | Will set `match=bla.py`. +-- c.py | Will violate D100,D103. +-- bla.py Will violate D100. We expect the call to pep257 to be successful, since `b.py` and `c.py` are not supposed to be found by the re. """ with Pep257Env() as env: env.write_config(convention='pep257', add_ignore='D100') env.write_config(prefix='A', match_dir='C') env.write_config(prefix=os.path.join('A', 'C'), match='bla.py') content = textwrap.dedent("""\ def foo(): pass """) env.makedirs(os.path.join('A', 'B')) with env.open(os.path.join('A', 'B', 'b.py'), 'wt') as test: test.write(content) with env.open(os.path.join('A', 'C', 'c.py'), 'wt') as test: test.write(content) with env.open(os.path.join('A', 'C', 'bla.py'), 'wt') as test: test.write('') _, _, code = env.invoke_pep257() assert code == 0 pep257-0.7.0/tox.ini000066400000000000000000000011111260573666400140640ustar00rootroot00000000000000# Tox (http://tox.testrun.org/) is a tool for running tests in # multiple virtualenvs. This configuration file will run the # test suite on all supported python versions. To use it, "pip # install tox" and then run "tox" from this directory. [tox] envlist = py26, py27, py32, py33, py34, pypy, pypy3 [testenv] # Make sure reading the UTF-8 from test.py works regardless of the locale used. setenv = LANG=C LC_ALL=C commands = py.test --pep8 --clearcache deps = -rrequirements/tests.txt [pytest] pep8ignore = test.py E701 E704 norecursedirs = docs .tox [pep257] inherit = false