Skip to main content

code checking using pep8 and pyflakes

Project description

Flake8 is a wrapper around these tools:

  • PyFlakes

  • pep8

  • Ned’s MacCabe script

Flake8 runs all tools by launching the single ‘flake8’ script, but ignores pep8 and PyFlakes extended options and just uses defaults. It displays the warnings in a per-file, merged output.

It also adds a few features:

  • files that starts with this header are skipped:

    # flake8: noqa
  • lines that contains a “# NOQA” comment at the end will not issue a warning.

  • a Mercurial hook.

  • a McCabe complexity checker.

QuickStart

To run flake8 just invoke it against any directory or Python module:

$ flake8 coolproject
coolproject/mod.py:1027: local variable 'errors' is assigned to but never used
coolproject/mod.py:97: 'shutil' imported but unused
coolproject/mod.py:729: redefinition of function 'readlines' from line 723
coolproject/mod.py:1028: local variable 'errors' is assigned to but never used
coolproject/mod.py:625:17: E225 missing whitespace around operato

The output of PyFlakes and pep8 is merged and returned.

flake8 offers an extra option: –max-complexity, which will emit a warning if the McCabe complexityu of a function is higher that the value. By default it’s deactivated:

$ bin/flake8 --max-complexity 12 flake8
coolproject/mod.py:97: 'shutil' imported but unused
coolproject/mod.py:729: redefinition of function 'readlines' from line 723
coolproject/mod.py:1028: local variable 'errors' is assigned to but never used
coolproject/mod.py:625:17: E225 missing whitespace around operator
coolproject/mod.py:452:1: 'missing_whitespace_around_operator' is too complex (18)
coolproject/mod.py:939:1: 'Checker.check_all' is too complex (12)
coolproject/mod.py:1204:1: 'selftest' is too complex (14)

This feature is quite useful to detect over-complex code. According to McCabe, anything that goes beyond 10 is too complex. See https://en.wikipedia.org/wiki/Cyclomatic_complexity.

Mercurial hook

To use the Mercurial hook on any commit or qrefresh, change your .hg/rc file like this:

[hooks]
commit = python:flake8.run.hg_hook
qrefresh = python:flake8.run.hg_hook

[flake8]
strict = 0
complexity = 12

If strict option is set to 1, any warning will block the commit. When strict is set to 0, warnings are just displayed in the standard output.

complexity defines the maximum McCabe complexity allowed before a warning is emited. If you don’t specify it it’s just ignored. If specified, must be a positive value. 12 is usually a good value.

Original projects

Flake8 is just a glue project, all the merits go to the creators of the original projects:

CHANGES

1.0 - 2011-11-29

  • Deactivates by default the complexity checker

  • Introduces the complexity option in the HG hook and the command line.

0.9 - 2011-11-09

  • update pep8 version to 0.6.1

  • mccabe check: gracefully handle compile failure

0.8 - 2011-02-27

  • fixed hg hook

  • discard unexisting files on hook check

0.7 - 2010-02-18

  • Fix pep8 intialization when run through Hg

  • Make pep8 short options work when run throug the command line

  • skip duplicates when controlling files via Hg

0.6 - 2010-02-15

  • Fix the McCabe metric on some loops

Project details


Release history Release notifications | RSS feed

This version

1.0

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flake8-1.0.tar.gz (40.7 kB view details)

Uploaded Source

File details

Details for the file flake8-1.0.tar.gz.

File metadata

  • Download URL: flake8-1.0.tar.gz
  • Upload date:
  • Size: 40.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for flake8-1.0.tar.gz
Algorithm Hash digest
SHA256 31c0b9ee0f2239d6c2e903d35cd262631818f6486fb98924597de883a6b95b3b
MD5 f8a92349b39910d8a9af10ef2961b284
BLAKE2b-256 a6c444c41b868d499c4162f86e5312a690ff06ec45b7e1465fc305c6d3dd866d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page