Skip to main content

Code audit tool for python

Project description

Tests Status PYPI Version Python Versions

Code audit tool for Python and JavaScript. Pylama wraps these tools:

  • pycodestyle (formerly pep8) © 2012-2013, Florent Xicluna;

  • pydocstyle (formerly pep257 by Vladimir Keleshev) © 2014, Amir Rachum;

  • PyFlakes © 2005-2013, Kevin Watters;

  • Mccabe © Ned Batchelder;

  • Pylint © 2013, Logilab;

  • Radon © Michele Lacchia

  • eradicate © Steven Myint;

  • Mypy © Jukka Lehtosalo and contributors;

  • Vulture © Jendrik Seipp and contributors;

Docs are available at https://pylama.readthedocs.org/. Pull requests with documentation enhancements and/or fixes are awesome and most welcome.

Requirements:

  • Python (3.7, 3.8, 3.9, 3.10)

  • If your tests are failing on Win platform you are missing: curses - http://www.lfd.uci.edu/~gohlke/pythonlibs/ (The curses library supplies a terminal-independent screen-painting and keyboard-handling facility for text-based terminals)

For python versions < 3.7 install pylama 7.7.1

Installation:

Pylama can be installed using pip:

$ pip install pylama

You may optionally install the requirements with the library:

$ pip install pylama[mypy]
$ pip install pylama[pylint]
$ pip install pylama[eradicate]
$ pip install pylama[radon]
$ pip install pylama[vulture]

Or install them all:

$ pip install pylama[all]

Quickstart

Pylama is easy to use and really fun for checking code quality. Just run pylama and get common output from all pylama plugins (pycodestyle, PyFlakes, etc.)

Recursively check the current directory.

$ pylama

Recursively check a path.

$ pylama <path_to_directory_or_file>

Ignore errors

$ pylama -i W,E501

Choose code checkers

$ pylama -l "pycodestyle,mccabe"

Set Pylama (checkers) options

Command line options

$ pylama --help

usage: pylama [-h] [--verbose] [--version]
              [--format {pep8,pycodestyle,pylint,parsable}] [--select SELECT]
              [--sort SORT] [--linters LINTERS] [--ignore IGNORE]
              [--skip SKIP] [--report REPORT] [--hook] [--concurrent]
              [--options FILE] [--force] [--abspath]
              [paths [paths ...]]

Code audit tool for python.

positional arguments:
  paths                 Paths to files or directories for code check.

optional arguments:
  -h, --help            show this help message and exit
  --verbose, -v         Verbose mode.
  --version             show program's version number and exit
  --format {pep8,pycodestyle,pylint,parsable}, -f {pep8,pycodestyle,pylint,parsable}
                        Choose errors format (pycodestyle, pylint, parsable).
  --select SELECT, -s SELECT
                        Select errors and warnings. (comma-separated list)
  --sort SORT           Sort result by error types. Ex. E,W,D
  --linters LINTERS, -l LINTERS
                        Select linters. (comma-separated). Choices are mccabe,
                        pep257,pydocstyle,pep8,pycodestyle,pyflakes,pylint,iso
                        rt.
  --ignore IGNORE, -i IGNORE
                        Ignore errors and warnings. (comma-separated)
  --skip SKIP           Skip files by masks (comma-separated, Ex.
                        */messages.py)
  --report REPORT, -r REPORT
                        Send report to file [REPORT]
  --hook                Install Git (Mercurial) hook.
  --concurrent, --async
                        Enable async mode. Useful for checking a lot of files.
  --options FILE, -o FILE
                        Specify configuration file. Looks for pylama.ini,
                        setup.cfg, tox.ini, or pytest.ini in the current
                        directory (default: None).
  --force, -F           Force code checking (if linter doesn't allow)
  --abspath, -a         Use absolute paths in output.

File modelines

You can set options for Pylama inside a source file. Use a pylama modeline for this, anywhere in the file.

Format:

# pylama:{name1}={value1}:{name2}={value2}:...

For example, ignore warnings except W301:

# pylama:ignore=W:select=W301

Disable code checking for current file:

# pylama:skip=1

Those options have a higher priority.

Skip lines (noqa)

Just add # noqa at the end of a line to ignore:

def urgent_fuction():
    unused_var = 'No errors here' # noqa

Configuration file

Pylama looks for a configuration file in the current directory.

The program searches for the first matching ini-style configuration file in the directories of command line argument. Pylama looks for the configuration in this order:

pylama.ini
setup.cfg
tox.ini
pytest.ini

The --option / -o argument can be used to specify a configuration file.

Pylama searches for sections whose names start with pylama.

The pylama section configures global options like linters and skip.

[pylama]
format = pylint
skip = */.tox/*,*/.env/*
linters = pylint,mccabe
ignore = F0401,C0111,E731

Set code-checkers’ options

You can set options for a special code checkers with pylama configurations.

[pylama:pyflakes]
builtins = _

[pylama:pycodestyle]
max_line_length = 100

[pylama:pylint]
max_line_length = 100
disable = R

See code-checkers’ documentation for more info. Note that dashes are replaced by underscores (e.g. Pylint’s max-line-length becomes max_line_length).

Set options for file (group of files)

You can set options for special file (group of files) with sections:

The options have a higher priority than in the pylama section.

[pylama:*/pylama/main.py]
ignore = C901,R0914,W0212
select = R

[pylama:*/tests.py]
ignore = C0110

[pylama:*/setup.py]
skip = 1

Pytest integration

Pylama has Pytest support. The package automatically registers itself as a pytest plugin during installation. Pylama also supports the pytest_cache plugin.

Check files with pylama

pytest --pylama ...

The recommended way to set pylama options when using pytest — configuration files (see below).

Writing a linter

You can write a custom extension for Pylama. The custom linter should be a python module. Its name should be like ‘pylama_<name>’.

In ‘setup.py’, ‘pylama.linter’ entry point should be defined.

setup(
    # ...
    entry_points={
        'pylama.linter': ['lintername = pylama_lintername.main:Linter'],
    }
    # ...
)

‘Linter’ should be an instance of ‘pylama.lint.Linter’ class. It must implement two methods:

  1. allow takes a path argument and returns true if the linter can check this file for errors.

  2. run takes a path argument and meta keyword arguments and returns a list of errors.

Example:

Just a virtual ‘WOW’ checker.

setup.py:

setup(
    name='pylama_wow',
    install_requires=[ 'setuptools' ],
    entry_points={
        'pylama.linter': ['wow = pylama_wow.main:Linter'],
    }
    # ...
)

pylama_wow.py:

from pylama.lint import Linter as BaseLinter

class Linter(BaseLinter):

    def allow(self, path):
        return 'wow' in path

    def run(self, path, **meta):
        with open(path) as f:
            if 'wow' in f.read():
                return [{
                    lnum: 0,
                    col: 0,
                    text: '"wow" has been found.',
                    type: 'WOW'
                }]

Run pylama from python code

from pylama.main import check_path, parse_options

# Use and/or modify 0 or more of the options defined as keys in the variable my_redefined_options below.
# To use defaults for any option, remove that key completely.
my_redefined_options = {
    'linters': ['pep257', 'pydocstyle', 'pycodestyle', 'pyflakes' ...],
    'ignore': ['D203', 'D213', 'D406', 'D407', 'D413' ...],
    'select': ['R1705' ...],
    'sort': 'F,E,W,C,D,...',
    'skip': '*__init__.py,*/test/*.py,...',
    'async': True,
    'force': True
    ...
}
# relative path of the directory in which pylama should check
my_path = '...'

options = parse_options([my_path], **my_redefined_options)
errors = check_path(options, rootdir='.')

Bug tracker

If you have any suggestions, bug reports or annoyances please report them to the issue tracker at https://github.com/klen/pylama/issues

Contributing

Development of pylama happens at GitHub: https://github.com/klen/pylama

Contributors

See AUTHORS.

License

This is free software. You are permitted to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of it, under the terms of the MIT License. See LICENSE file for the complete license.

This software is provided WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE file for the complete disclaimer.

Project details


Release history Release notifications | RSS feed

Download files

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

Source Distribution

pylama-8.0.4.tar.gz (30.8 kB view details)

Uploaded Source

Built Distribution

pylama-8.0.4-py3-none-any.whl (29.7 kB view details)

Uploaded Python 3

File details

Details for the file pylama-8.0.4.tar.gz.

File metadata

  • Download URL: pylama-8.0.4.tar.gz
  • Upload date:
  • Size: 30.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for pylama-8.0.4.tar.gz
Algorithm Hash digest
SHA256 a5b5bce51579ba2c25997108cebf4116a5164f915e49c0d0c89bfc1631e750eb
MD5 c43ece4284773259299f05ecc63c1009
BLAKE2b-256 fa4c698871041d6ec258954bc773d246f3a79c2f83762cce56fcd786c718a51f

See more details on using hashes here.

File details

Details for the file pylama-8.0.4-py3-none-any.whl.

File metadata

  • Download URL: pylama-8.0.4-py3-none-any.whl
  • Upload date:
  • Size: 29.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for pylama-8.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 c5d9694733aaf7b6f55cfcf418537faccec2449f5d1a98247584ebe8f810b843
MD5 88e4b46afa78e4562e681d8dde11c3f2
BLAKE2b-256 0537b5c87b8cfb36121a83a11f4ff2a899cac2bc2ee8363c09653fee286f2ff1

See more details on using hashes here.

Supported by

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