pylama 8.4.1


pip install pylama

  Latest version

Released: Aug 08, 2022


Meta
Author: Kirill Klenov
Requires Python: >=3.7

Classifiers

Development Status
  • 5 - Production/Stable

Environment
  • Console

Intended Audience
  • Developers
  • System Administrators

License
  • OSI Approved :: MIT License

Programming Language
  • Python
  • Python :: 3
  • Python :: 3.10
  • Python :: 3.7
  • Python :: 3.8
  • Python :: 3.9

Topic
  • Software Development :: Quality Assurance
  • Software Development :: Testing
Tests Status Documentation Status PYPI Version Python Versions

Code audit tool for Python. 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://klen.github.io/pylama/. 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

TOML configuration can be enabled optionally:

$ pip install pylama[toml]

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] [--version] [--verbose] [--options FILE] [--linters LINTERS] [--from-stdin] [--concurrent] [--format {pydocstyle,pycodestyle,pylint,parsable,json}] [--abspath]
              [--max-line-length MAX_LINE_LENGTH] [--select SELECT] [--ignore IGNORE] [--skip SKIP] [--sort SORT] [--report REPORT] [--hook] [--max-complexity MAX_COMPLEXITY]
              [--pydocstyle-convention {pep257,numpy,google}] [--pylint-confidence {HIGH,INFERENCE,INFERENCE_FAILURE,UNDEFINED}]
              [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
  --version             show program's version number and exit
  --verbose, -v         Verbose mode.
  --options FILE, -o FILE
                        Specify configuration file. Looks for pylama.ini, setup.cfg, tox.ini, or pytest.ini in the current directory (default: None)
  --linters LINTERS, -l LINTERS
                        Select linters. (comma-separated). Choices are eradicate,mccabe,mypy,pycodestyle,pydocstyle,pyflakes,pylint,isort.
  --from-stdin          Interpret the stdin as a python script, whose filename needs to be passed as the path argument.
  --concurrent, --async
                        Enable async mode. Useful for checking a lot of files.
  --format {pydocstyle,pycodestyle,pylint,parsable,json}, -f {pydocstyle,pycodestyle,pylint,parsable,json}
                        Choose output format.
  --abspath, -a         Use absolute paths in output.
  --max-line-length MAX_LINE_LENGTH, -m MAX_LINE_LENGTH
                        Maximum allowed line length
  --select SELECT, -s SELECT
                        Select errors and warnings. (comma-separated list)
  --ignore IGNORE, -i IGNORE
                        Ignore errors and warnings. (comma-separated)
  --skip SKIP           Skip files by masks (comma-separated, Ex. */messages.py)
  --sort SORT           Sort result by error types. Ex. E,W,D
  --report REPORT, -r REPORT
                        Send report to file [REPORT]
  --hook                Install Git (Mercurial) hook.
  --max-complexity MAX_COMPLEXITY
                        Max complexity threshold

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.

You can use a “global” configuration, stored in .pylama.ini in your home directory. This will be used as a fallback configuration.

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

./pylama.ini
./pyproject.toml
./setup.cfg
./tox.ini
./pytest.ini
~/.pylama.ini

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

INI-style configuration

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

TOML configuration

Pylama searches for sections whose names start with tool.pylama.

The tool.pylama section configures global options like linters and skip.

[tool.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.

[tool.pylama.linter.pyflakes]
builtins = "_"

[tool.pylama.linter.pycodestyle]
max_line_length = 100

[tool.pylama.linter.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 tool.pylama section.

[[tool.pylama.files]]
path = "*/pylama/main.py"
ignore = "C901,R0914,W0212"
select = "R"

[[tool.pylama.files]]
path = "pylama:*/tests.py"
ignore = "C0110"

[[tool.pylama.files]]
path = "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_paths, 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_paths(my_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 CONTRIBUTORS.

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.

8.4.1 Aug 08, 2022
8.4.0 Aug 08, 2022
8.3.8 Mar 11, 2022
8.3.7 Jan 10, 2022
8.3.6 Dec 15, 2021
8.3.5 Dec 13, 2021
8.3.3 Dec 03, 2021
8.3.2 Dec 02, 2021
8.3.1 Dec 02, 2021
8.3.0 Dec 02, 2021
8.2.1 Nov 30, 2021
8.2.0 Nov 29, 2021
8.1.4 Nov 27, 2021
8.1.3 Nov 27, 2021
8.1.1 Nov 27, 2021
8.0.6 Nov 26, 2021
8.0.4 Nov 25, 2021
7.7.1 Apr 10, 2019
7.7.0 Apr 10, 2019
7.6.6 Nov 02, 2018
7.6.5 Oct 10, 2018
7.6.4 Oct 09, 2018
7.6.2 Oct 09, 2018
7.5.5 Oct 02, 2018
7.5.0 Oct 02, 2018
7.4.3 Sep 13, 2017
7.4.2 Sep 13, 2017
7.4.1 Jun 28, 2017
7.4.0 Jun 26, 2017
7.3.3 Dec 26, 2016
7.3.2 Dec 26, 2016
7.3.1 Nov 29, 2016
7.3.0 Nov 29, 2016
7.2.3 Oct 25, 2016
7.2.2 Oct 25, 2016
7.2.1 Oct 25, 2016
7.1.0 Sep 07, 2016
7.0.9 Apr 04, 2016
7.0.7 Feb 16, 2016
7.0.6 Sep 16, 2015
7.0.4 Sep 08, 2015
7.0.3 Sep 04, 2015
7.0.0 Aug 31, 2015
6.4.0 Aug 17, 2015
6.3.4 Jun 30, 2015
6.3.3 Jun 30, 2015
6.3.2 Jun 30, 2015
6.3.1 Mar 25, 2015
6.3.0 Mar 25, 2015
6.2.0 Jan 15, 2015
6.1.2 Jan 07, 2015
6.1.1 Oct 29, 2014
6.1.0 Oct 26, 2014
6.0.1 Jul 23, 2014
6.0.0 Jul 01, 2014
5.0.5 Jun 15, 2014
5.0.4 Jun 15, 2014
5.0.3 Jun 15, 2014
5.0.2 Jun 15, 2014
5.0.1 Jun 11, 2014
5.0.0 Jun 10, 2014
4.0.2 Jun 09, 2014
4.0.1 Jun 08, 2014
4.0.0 Jun 08, 2014
3.3.2 Jun 06, 2014
3.3.1 Jun 06, 2014
3.3.0 Jun 06, 2014
3.2.0 May 07, 2014
3.1.2 May 04, 2014
3.1.0 Mar 26, 2014
3.0.2 Mar 24, 2014
3.0.1 Mar 24, 2014
2.0.4 Dec 04, 2013
2.0.3 Nov 12, 2013
2.0.2 Oct 26, 2013
2.0.1 Oct 14, 2013
2.0.0 Oct 14, 2013
1.5.4 Sep 16, 2013
1.5.3 Sep 04, 2013
1.5.1 Aug 30, 2013
1.5.0 Aug 29, 2013
1.4.0 Aug 07, 2013
1.3.3 Jul 25, 2013
1.3.2 Jul 25, 2013
1.3.1 Jul 08, 2013
1.3.0 Jul 08, 2013
1.2.0 Jul 03, 2013
1.1.1 Jun 25, 2013
1.1.0 Jun 20, 2013
1.0.4 Jun 06, 2013
1.0.2 May 31, 2013
1.0.1 May 31, 2013
1.0.0 May 30, 2013
0.3.8 May 29, 2013
0.3.7 May 29, 2013
0.3.6 May 22, 2013
0.3.5 May 21, 2013
0.3.4 May 21, 2013
0.3.3 May 21, 2013
0.3.2 May 15, 2013
0.3.1 May 03, 2013
0.3.0 Mar 30, 2013
0.2.8 Mar 29, 2013
0.2.7 Mar 22, 2013
0.2.6 Mar 22, 2013
0.2.5 Mar 22, 2013
0.2.4 Mar 22, 2013
0.2.3 Mar 15, 2013
0.2.2 Mar 14, 2013
0.2.1 Mar 14, 2013
0.2.0 Mar 14, 2013
0.1.4 Feb 15, 2013
0.1.3 Feb 15, 2013
0.1.2 Feb 15, 2013
0.1.1 Feb 15, 2013
0.1.0 Aug 17, 2012

Wheel compatibility matrix

Platform Python 3
any

Files in release

Extras:
Dependencies:
mccabe (>=0.7.0)
pycodestyle (>=2.9.1)
pydocstyle (>=6.1.1)
pyflakes (>=2.5.0)