Simple Python style checker in one Python file

Overview

pycodestyle (formerly called pep8) - Python style guide checker

Build status Documentation Status Wheel Status Join the chat at https://gitter.im/PyCQA/pycodestyle

pycodestyle is a tool to check your Python code against some of the style conventions in PEP 8.

Note

This package used to be called pep8 but was renamed to pycodestyle to reduce confusion. Further discussion can be found in the issue where Guido requested this change, or in the lightning talk at PyCon 2016 by @IanLee1521: slides video.

Features

  • Plugin architecture: Adding new checks is easy.
  • Parseable output: Jump to error location in your editor.
  • Small: Just one Python file, requires only stdlib. You can use just the pycodestyle.py file for this purpose.
  • Comes with a comprehensive test suite.

Installation

You can install, upgrade, and uninstall pycodestyle.py with these commands:

$ pip install pycodestyle
$ pip install --upgrade pycodestyle
$ pip uninstall pycodestyle

There's also a package for Debian/Ubuntu, but it's not always the latest version.

Example usage and output

$ pycodestyle --first optparse.py
optparse.py:69:11: E401 multiple imports on one line
optparse.py:77:1: E302 expected 2 blank lines, found 1
optparse.py:88:5: E301 expected 1 blank line, found 0
optparse.py:222:34: W602 deprecated form of raising exception
optparse.py:347:31: E211 whitespace before '('
optparse.py:357:17: E201 whitespace after '{'
optparse.py:472:29: E221 multiple spaces before operator
optparse.py:544:21: W601 .has_key() is deprecated, use 'in'

You can also make pycodestyle.py show the source code for each error, and even the relevant text from PEP 8:

$ pycodestyle --show-source --show-pep8 testsuite/E40.py
testsuite/E40.py:2:10: E401 multiple imports on one line
import os, sys
         ^
    Imports should usually be on separate lines.

    Okay: import os\nimport sys
    E401: import sys, os

Or you can display how often each error was found:

$ pycodestyle --statistics -qq Python-2.5/Lib
232     E201 whitespace after '['
599     E202 whitespace before ')'
631     E203 whitespace before ','
842     E211 whitespace before '('
2531    E221 multiple spaces before operator
4473    E301 expected 1 blank line, found 0
4006    E302 expected 2 blank lines, found 1
165     E303 too many blank lines (4)
325     E401 multiple imports on one line
3615    E501 line too long (82 characters)
612     W601 .has_key() is deprecated, use 'in'
1188    W602 deprecated form of raising exception

Links

Comments
  • Please rename this tool

    Please rename this tool

    Tools should not be named after style guide PEPs. A style guide is a document written for humans, and has lots of subtlety. Issues caused by the rigidity or simplicity of the tool end up causing pointless discussion about the letter of the PEP, as if it was a law, which was never the intention of the PEP. If you want to write a tool that checks style, please give it some clever name, don't name it after a PEP, so it's clear that whenever humans don't like what the tool says, it is a tool issue, not an issue with the PEP (which is merely intended to guide humans, not to require them to follow it every time).

    FWIW I am happy that pep8 exist! It can be very useful and I use it myself. But I always let it know who's boss. :-)

    opened by gvanrossum 56
  • Add per file disabling of warnings

    Add per file disabling of warnings

    Per "suggestion" of @dubglan in https://github.com/jcrocholl/pep8/issues/264#issuecomment-73584055, add the ability to disable warnings on a per file basis (ala pylint). Something like:

    # pep8: ignore=EXXX
    
    feature 
    opened by IanLee1521 42
  • New release?

    New release?

    Is there a planned date for a new release of pycodestyle? This issue for example has been fixed last april, and yet still present on the main release. For direct use of the linter, one can always use the latest master, but it gets complicated to choose pycodestyle version when using it through flake8. It's even worse when using it through even more layers of code, like for example using pre-commit. For example, in our project, we use pre-commit to run black formatter, and then flake8. Black and current flake8-pycodestyle disagree on E252 in function signature with type annotation and we had to silence errors to be able to commit. And as a more general question, what is the release policy of this package?

    opened by antoine-gallix 36
  • Adding framework for fixing pep8 violations automatically including common violations.

    Adding framework for fixing pep8 violations automatically including common violations.

    Contributions to this effort done by:

    • Aditya Bhargava
    • Calen Pennington
    • Hugh Brown
    • Chris Angove

    Squashed commit of the following:

    commit 75b1044e0d5599402eadc65106b68193032fd6b4 Author: Chris Angove [email protected] Date: Mon Apr 16 14:35:33 2012 -0400

    fixing issue with a pep8 check that reutrned instead of yielding
    

    commit 3bfee6bbee9ccaa13c4797a62c558ac1d83ad82c Author: Chris Angove [email protected] Date: Mon Apr 16 14:21:59 2012 -0400

    fixing pep8 problms
    

    commit f2144460ea8e35750cc3b456c02a6463fe80f9cd Merge: 63aa278 3df8635 Author: Chris Angove [email protected] Date: Mon Apr 16 09:54:00 2012 -0400

    Merge remote-tracking branch 'upstream/master'
    
    Conflicts:
        pep8.py
        setup.py
    

    commit 63aa2783bac3960c036e1e29a84a5e2f7595e94f Merge: 826c9cc 5bc7c55 Author: Chris Angove [email protected] Date: Mon Apr 16 06:46:17 2012 -0700

    Merge pull request #1 from hughdbrown/master
    
    Fixed a bug in modifying whitespace around named parameters and =
    

    commit 5bc7c55656258be4783a62d2ae053a431e1af289 Author: Hugh Brown [email protected] Date: Sun Apr 15 22:42:22 2012 -0400

    Add test for fix in e929e8a
    

    commit e929e8a29631b7d8a769240e74e77bf05c020fdd Author: Hugh Brown [email protected] Date: Sun Apr 15 22:32:37 2012 -0400

    Fix whitespace errors
    Apply pep8 fixes to pep8.py (why would this never have happened before?)
    Fix bug in fixing missing whitespace around named parameters in a call
    

    commit 826c9cc9f3bfca25d8ba8f011f3fff6f76cabf4a Author: Calen Pennington [email protected] Date: Fri Jun 3 10:10:46 2011 -0400

    Updating to version 0.6.2
    

    commit ca139634643bd8282ab8c4b30b72149a69a8319f Author: Calen Pennington [email protected] Date: Fri May 27 15:08:33 2011 -0400

    Only open/write to file if there are changes to be made to it
    

    commit 756457c3dd15506f7cc49686d6c49b3313ae626e Author: Calen Pennington [email protected] Date: Fri May 27 14:59:24 2011 -0400

    Skip edits that will conflict
    

    commit ed1b45b73ec49cbcad79055f9fae25e7d019dce3 Author: Calen Pennington [email protected] Date: Fri May 27 13:15:12 2011 -0400

    Adding fixes for more checks
    

    commit 2fcd046abade4a3b38f9769d54c9adf8fd9d7be1 Author: Calen Pennington [email protected] Date: Fri May 27 12:29:22 2011 -0400

    Adding selftests for the fix_* functions
    

    commit f7272dbb4ad6168833a0c728e7375e3a068aa8e9 Author: Calen Pennington [email protected] Date: Fri May 27 09:04:57 2011 -0400

    Make fix code work for tests with no fix attached
    

    commit e01c7626ed568a71cccc2898f075a4c6b5e7b869 Author: Calen Pennington [email protected] Date: Fri May 27 09:04:20 2011 -0400

    Add fix for whitespace_before_parameters
    

    commit da39c3369858c0a1c579464782d722557a45138a Author: Calen Pennington [email protected] Date: Fri May 27 09:04:06 2011 -0400

    Add test for missing_whitespace
    

    commit 874b65dd2e30b1bf343601a2a2eab4f096adde26 Author: Calen Pennington [email protected] Date: Fri May 27 09:03:54 2011 -0400

    Make fix_extraneous_whitespace work with tabs and spaces
    

    commit 3e44076e394290f31509b3398672128fe6cface0 Author: Calen Pennington [email protected] Date: Fri May 27 08:47:51 2011 -0400

    Allow logical checks to yield more than one error per line
    

    commit 203fb20fd9d9d857353d5298fc54de54719fbbed Author: Calen Pennington [email protected] Date: Fri May 27 08:31:57 2011 -0400

    Adding fix for extraneous_whitespace
    

    commit 1c58077454359c32cccd55681ac4fa7d91bc557c Author: Calen Pennington [email protected] Date: Fri May 27 08:31:41 2011 -0400

    Adding fix for blank_lines
    

    commit 32d37342ec7dae4413947cb0bc7b4a2447da5a9e Author: Calen Pennington [email protected] Date: Fri May 27 08:31:28 2011 -0400

    Adding fix for missing_newline
    

    commit c0553094191d859a13892d41f0cb8312079d117e Author: Calen Pennington [email protected] Date: Fri May 27 08:31:12 2011 -0400

    Adding fix for trailing_blank_lines
    

    commit 810318f2e1f5490c407ce238f499d5cf94ec2b1f Author: Calen Pennington [email protected] Date: Fri May 27 08:30:14 2011 -0400

    Add fix for trailing_whitespace
    

    commit d8f990049224105cef882d2824956ae07b911a7e Author: Calen Pennington [email protected] Date: Fri May 27 08:30:01 2011 -0400

    Add fix for tabs_obsolete
    

    commit 7244dcf1e6bdb0513be1a389a8d488e16cbaa0a0 Author: Calen Pennington [email protected] Date: Fri May 27 08:29:24 2011 -0400

    Add the mechanism for making fixes as easy to add as checks
    

    commit 0ad68e67ed758d57ea9c9512c086d63a3376a96c Author: Calen Pennington [email protected] Date: Fri May 27 08:28:12 2011 -0400

    Fix reporting of IOErrors during fix output
    

    commit 2185e866a1380f20e24713ee4a8a5af979769e43 Merge: 29968a6 6b87796 Author: Calen Pennington [email protected] Date: Thu May 26 16:19:05 2011 -0400

    Merge changes from https://github.com/egonSchiele/pep8/commit/6b87796987dadf7f872dc390fb31bc71add05407
    
    Conflicts:
        pep8.py
    

    commit 6b87796987dadf7f872dc390fb31bc71add05407 Author: Aditya Bhargava [email protected] Date: Thu Apr 8 22:56:33 2010 -0500

    --inplace flag added to allow changes to files in-place. try/catch block added for write file.
    

    commit c01c47bdd351cc68fbf641af42c5aebfbde240b3 Author: Aditya Bhargava [email protected] Date: Wed Apr 7 17:10:44 2010 -0500

    fix functionality added for trailing whitespace and superfluous blank lines at the end of the file.
    

    commit 6c6e01edcbb0d8a823311b3fd95debb2ff9d30ff Author: Aditya Bhargava [email protected] Date: Wed Apr 7 16:45:22 2010 -0500

    some fix functionality added. Now pep8 can be used to fix certain issues to be consistent with PEP 8.
    
    opened by cangove 30
  • AttributeError: 'module' object has no attribute 'break_around_binary_operator

    AttributeError: 'module' object has no attribute 'break_around_binary_operator

    2.4 release broke AttributeError: 'module' object has no attribute 'break_around_binary_operator' see https://travis-ci.org/EBI-Metagenomics/emgapi/jobs/364572048#L637

    opened by ghost 27
  • Enforce 72 character docstring line width

    Enforce 72 character docstring line width

    PEP8 states:

    For flowing long blocks of text with fewer structural restrictions (docstrings or comments), the line length should be limited to 72 characters.

    The current implementation only enforces the maximum line length (E501). I'd like to suggest the addition of an E503 error to enforce the restriction of these "flowing long blocks of text."

    extension 
    opened by mblayman 27
  • Closing paren indentation should be more flexible

    Closing paren indentation should be more flexible

    From what I could see, there is no advice in PEP-8 to discourage using closing parens on the same line as the last item in a list, e.g.

    foo = {
           "bar": (
               "baz",
               "qux"
               )  # <-- this is discouraged
           }  # <-- as is this (n.b. it is one column to the right)
    

    This is the default indentation style in emacs.

    pep8 says: E123 closing bracket does not match indentation of opening bracket's line

    bug 
    opened by iartarisi 27
  • Ability to change PEP8 flags on the fly in source file via inline comments

    Ability to change PEP8 flags on the fly in source file via inline comments

    Sometimes we need to violate PEP8 rules to improve readability of source file, at the same time we would like file to be still checked by PEP8.

    I've added simple change that allows to influence pep8 'expected' codes:

    :PEP8 -E221 -W603

    a = 3 long = 4

    :PEP8 +E221

    Prepending minus sign disables the code, while prepending plus enables it back.

    Please let me know if that makes sense for inclusion, I may need to add tests...

    opened by smira 26
  • Support for W505 - doc line length

    Support for W505 - doc line length

    An implementation of #344.

    This implementation takes the effective default of 79 characters for the default max doc length despite PEP8 recommendations being 72, as well as setting only a warning. In addition, W504 is added to the default ignore list.

    Builds tested with py27 and py35 (don't have others on my platform, sorry!)

    opened by brianv0 25
  • pep8 gives false alarms on PEP 8 approved examples

    pep8 gives false alarms on PEP 8 approved examples

    I ran pep8 over the "this is OK" examples in PEP 8, and found the following false alarms with pep8's default settings:

    E121 (requiring continuation lines to use 4 space indents is not in PEP 8 - that guideline only applies to suite indents) E701 (PEP 8 allows single line compound statements, and uses one as the preferred alternative to having a lambda expression on the RHS of an assignment statement)

    opened by ncoghlan 25
  • Not importing files at top of the file is a PEP8 violation

    Not importing files at top of the file is a PEP8 violation

    not importing files at top is a PEP8 violation.

    http://legacy.python.org/dev/peps/pep-0008/ Imports are always put at the top of the file, just after any module comments and docstrings, and before module globals and constants.

    Not flagged in current Pycharm... rerouted from Pycharm per their request

    feature 
    opened by DavidHwu 23
  • W504: Fix positional-only / keyword-only mark being treated as a binary operator

    W504: Fix positional-only / keyword-only mark being treated as a binary operator

    Fixes #951

    Done by tracking enclosure count when inside a function param list (detected by def keyword) and checking whether the previous token's text is , to be sure that the currently checked token can be evaluated in a param name context.

    opened by Jackenmen 0
  • E721 regression(s)

    E721 regression(s)

    so #1041 brought up a lot more instances of E721 and I don't feel confident adding this in a release since it's going to cause a bunch of (imo) unnecessary work

    for example, here's something which wasn't flagged before but is now:

    assert type(x) is not bytes
    

    strictly speaking the lint rule probably should match this, but almost every case I can find with is and is not is being very explicit about the types being checked

    I think the most reasonable thing to do here is to only check == and != -- those are the ones that are most likely actual mistakes rather than an explicit check.

    the other option is revert #1041 -- which might be the quickest thing to do to not block a release -- unclear what the best approach is here

    opened by asottile 4
  • Fixing false negatives with E731 lambda assignments

    Fixing false negatives with E731 lambda assignments

    fixes #1061 Changed out the current implementation of lambda assignment detection for a regex based one. As stated in #1061, there are several lambda assignment cases that are being missed. Additionally, the current detection implementation is not in the most logical place. Before, detection of lambdas was tied to searching for ':' characters. Now lambda assignment statements are all just detected by:

    (?<!\.)\w+\s*(\,\s*\w+\s*)*\=(.*\,)*[\s|\(]*lambda
    

    This implementation has more coverage and is more simple than the previous one.

    opened by Sam-Cleveland 3
  • E731 false negatives

    E731 false negatives

    a = lambda x: x  # Raises E731
    a = (
        lambda x: x 
    ) # Doesn't raise E731
    a, b = lambda x: x, lambda y: y  # Doesn't raise E731
    a, b = 1, lambda y: y  # Doesn't raise E731
    
    opened by jpy-git 3
  • add GitHub URL for PyPi

    add GitHub URL for PyPi

    Warehouse now uses the project_urls provided to display links in the sidebar on this screen, as well as including them in API responses to help the automation tool find the source code for Requests.

    opened by andriyor 0
Releases(1.7.1)
  • 1.7.1(Oct 25, 2017)

    This will be the final release of the pep8 package, all future usage should be of the pycodestyle package instead (version 2.x + of this repository).

    Changes:

    • Prominently note via warning message that the tool is no longer released as pep8 and will only be fixed in the pycodestyle package
    Source code(tar.gz)
    Source code(zip)
  • 2.3.1(Jun 29, 2017)

  • 2.3.0(Jun 29, 2017)

    New Checks:

    • Add E722 warning for bare except clauses
    • Report E704 for async function definitions (async def)

    Bugs:

    • Fix another E305 false positive for variables beginning with "class" or "def"
    • Fix detection of multiple spaces betwen async and def
    • Fix handling of variable annotations. Stop reporting E701 on Python 3.6 for variable annotations.
    Source code(tar.gz)
    Source code(zip)
  • 2.2.0(Nov 14, 2016)

Owner
Python Code Quality Authority
Organization for code quality tools (and plugins) for the Python programming language
Python Code Quality Authority
An open-source, mini imitation of GitHub Copilot for Emacs.

Second Mate An open-source, mini imitation of GitHub Copilot using EleutherAI GPT-Neo-2.7B (via Huggingface Model Hub) for Emacs. This is a much small

Sam Rawal 238 Dec 27, 2022
Design by contract for Python. Write bug-free code. Add a few decorators, get static analysis and tests for free.

A Python library for design by contract (DbC) and checking values, exceptions, and side-effects. In a nutshell, deal empowers you to write bug-free co

Life4 473 Dec 28, 2022
Tool to automatically fix some issues reported by flake8 (forked from autoflake).

autoflake8 Introduction autoflake8 removes unused imports and unused variables from Python code. It makes use of pyflakes to do this. autoflake8 also

francisco souza 27 Sep 08, 2022
Unbearably fast O(1) runtime type-checking in pure Python.

Look for the bare necessities, the simple bare necessities. Forget about your worries and your strife. — The Jungle Book.

beartype 1.4k Jan 01, 2023
:sparkles: Surface lint errors during code review

✨ Linty Fresh ✨ Keep your codebase sparkly clean with the power of LINT! Linty Fresh parses lint errors and report them back to GitHub as comments on

Lyft 183 Dec 18, 2022
open source tools to generate mypy stubs from protobufs

mypy-protobuf: Generate mypy stub files from protobuf specs We just released a new major release mypy-protobuf 2. on 02/02/2021! It includes some back

Dropbox 527 Jan 03, 2023
coala provides a unified command-line interface for linting and fixing all your code, regardless of the programming languages you use.

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." ― John F. Woods coala provides a

coala development group 3.4k Dec 29, 2022
Optional static typing for Python 3 and 2 (PEP 484)

Mypy: Optional Static Typing for Python Got a question? Join us on Gitter! We don't have a mailing list; but we are always happy to answer questions o

Python 14.4k Jan 08, 2023
It's not just a linter that annoys you!

README for Pylint - https://pylint.pycqa.org/ Professional support for pylint is available as part of the Tidelift Subscription. Tidelift gives softwa

Python Code Quality Authority 4.4k Jan 04, 2023
🦆 Better duck-typing with mypy-compatible extensions to Protocol

🦆 Quacks If it walks like a duck and it quacks like a duck, then it must be a duck Thanks to PEP544, Python now has protocols: a way to define duck t

Arie Bovenberg 9 Nov 14, 2022
Silence mypy by adding or removing code comments

mypy-silent Automatically add or remove # type: ignore commends to silence mypy. Inspired by pylint-silent Why? Imagine you want to add type check for

Wu Haotian 8 Nov 30, 2022
❄️ A flake8 plugin to help you write better list/set/dict comprehensions.

flake8-comprehensions A flake8 plugin that helps you write better list/set/dict comprehensions. Requirements Python 3.6 to 3.9 supported. Installation

Adam Johnson 398 Dec 23, 2022
Custom Python linting through AST expressions

bellybutton bellybutton is a customizable, easy-to-configure linting engine for Python. What is this good for? Tools like pylint and flake8 provide, o

H. Chase Stevens 249 Dec 31, 2022
Stubs with type annotations for ordered-set Python library

ordered-set-stubs - stubs with type annotations for ordered-set Python library Archived - now type annotations are the part of the ordered-set library

Roman Inflianskas 2 Feb 06, 2020
Convert relative imports to absolute

absolufy-imports A tool and pre-commit hook to automatically convert relative imports to absolute. Installation $ pip install absolufy-imports Usage a

Marco Gorelli 130 Dec 30, 2022
A simple program which checks Python source files for errors

Pyflakes A simple program which checks Python source files for errors. Pyflakes analyzes programs and detects various errors. It works by parsing the

Python Code Quality Authority 1.2k Dec 30, 2022
A plugin for flake8 integrating Mypy.

flake8-mypy NOTE: THIS PROJECT IS DEAD It was created in early 2017 when Mypy performance was often insufficient for in-editor linting. The Flake8 plu

Łukasz Langa 103 Jun 23, 2022
Automated security testing using bandit and flake8.

flake8-bandit Automated security testing built right into your workflow! You already use flake8 to lint all your code for errors, ensure docstrings ar

Tyler Wince 96 Jan 01, 2023
flake8 plugin to catch useless `assert` statements

flake8-useless-assert flake8 plugin to catch useless assert statements Download or install on the PyPI page Violations Code Description Example ULA001

1 Feb 12, 2022
Mypy stubs, i.e., type information, for numpy, pandas and matplotlib

Mypy type stubs for NumPy, pandas, and Matplotlib This is a PEP-561-compliant stub-only package which provides type information for matplotlib, numpy

Predictive Analytics Lab 194 Dec 19, 2022