• Stars
    star
    1,048
  • Rank 43,968 (Top 0.9 %)
  • Language
    Python
  • License
    MIT License
  • Created over 12 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Code audit tool for python.

logo Pylama

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

Note

You can choose a group of errors like D, E1, etc, or special errors like C0312

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

Note

additional options may be available depending on installed linters

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.

More Repositories

1

mixer

Mixer -- Is a fixtures replacement. Supported Django, Flask, SqlAlchemy and custom python objects.
Python
942
star
2

py-frameworks-bench

Another benchmark for some python frameworks
Python
712
star
3

muffin

Muffin is a fast, simple and asyncronous web-framework for Python 3
Python
671
star
4

graphite-beacon

Simple alerting system for Graphite metrics
Python
453
star
5

django_markdown

Django markdown support and wysiwig
JavaScript
391
star
6

peewee_migrate

Simple migration engine for Peewee
Python
353
star
7

nvim-test

A Neovim wrapper for running tests
Lua
181
star
8

Flask-Foundation

Quick start with Flask
Python
154
star
9

nvim-config-local

Secure load local config files for neovim
Lua
150
star
10

aioauth-client

OAuth client for aiohttp
Python
141
star
11

atmark

Awk+Sed for humans
Python
69
star
12

python-scss

Python scss parser.
Python
67
star
13

dealer

Make some staff
Python
62
star
14

marshmallow-peewee

Peewee ORM integration with the marshmallow (de)serialization library.
Python
59
star
15

zeta-library

Css, scss, js parser and linker. Also framework for working with static files
Python
52
star
16

Flask-Collect

Collect static files in flask application
Python
49
star
17

makesite

makesite is a collection of scripts for deploying and managing web projects
Python
47
star
18

flask-pw

Peewee ORM integration for Flask framework
Python
45
star
19

peewee-aio

Async support for Peewee ORM
Python
45
star
20

adrest

Another django rest framework
Python
42
star
21

.vim

my vim settings
Vim Script
39
star
22

django-netauth

django auth backend
Python
36
star
23

asgi-tools

Tools to build ASGI apps
Python
35
star
24

pomodoro-tracker-locales

Language files
24
star
25

asgi-babel

Adds internationalization (i18n) support to ASGI applications (Asyncio/Trio)
Python
24
star
26

http-router

A simple router for HTTP applications
Python
24
star
27

rope-vim

Pathogen compatable ropevim plugin. Dont need install rope libs in system.
Python
21
star
28

bottle-peewee

Integrate Peewee ORM to Bottle framework
Python
19
star
29

muffin-admin

Admin interface for Muffin Framework
Python
18
star
30

mahjong.horneds.com

Riichi Mahjong Scores Trainer
CoffeeScript
18
star
31

flask-restler

Yet another REST library for Flask
Python
15
star
32

unite-radio.vim

Play radio stations in your VIM
Vim Script
15
star
33

aio-databases

Async Support for various databases
Python
14
star
34

django-gitrevision

Django git revision, simple add current git revision to request object for use in tempaltes and views.
Python
14
star
35

imgproxy

Python support for ImgProxy image processing server (https://imgproxy.net)
Python
14
star
36

pytest-aio

Is a simple pytest plugin for testing async python code
Python
12
star
37

klen.github.io

My github powered site.
HTML
12
star
38

muffin-rest

REST helpers for Muffin Framework
Python
12
star
39

aio-peewee

Tools to make Peewee work when using Asyncio
Python
11
star
40

muffin-jinja2

Jinja2 templates for Muffin framework
Python
11
star
41

donald

Make asyncio great again
Python
10
star
42

pypika-orm

Async ORM based on PyPika
Python
9
star
43

bottle-login

Implement users' sessions in Bottle framework
Python
9
star
44

asgi-prometheus

Support Prometheus metrics for ASGI applications
Python
9
star
45

muffin-peewee

Peewee integration to Muffin framework
Python
9
star
46

asgi-sessions

Signed Cookie-Based HTTP sessions for ASGI applications
Python
8
star
47

muffin-session

Session for Muffin Framework
Python
7
star
48

muffin-mongo

MongoDB support for Muffin framework
Python
7
star
49

starter

Create the skeleton for new projects.
Python
6
star
50

muffin-redis

Redis support for Muffin framework
Python
6
star
51

muffin-sentry

Sentry integration to Muffin Framework.
Python
5
star
52

pyserve

Serve local dirs (human version)
Python
5
star
53

muffin-example

Example Muffin application
Python
5
star
54

dotfiles

kk .dotfiles / use it for your own risks
Shell
4
star
55

bottle-jade

Provide Jade templates for Bottle framework
Python
4
star
56

django-gishelper

Useful commands for geodjango
Python
4
star
57

muffin-oauth

OAuth1/2 support for Muffin framework.
Python
4
star
58

muffin-databases

Async support for a range of databases for Muffin Framework
Python
4
star
59

knocker

A self contained service to make HTTP calls
Python
4
star
60

muffin-debugtoolbar

Debug Toolbar for Muffin applications
JavaScript
4
star
61

muffin-babel

Extension to Muffin that adds localization support with help of babel.
Python
3
star
62

inirama

Simple INI parser
Python
3
star
63

pylama_pylint

Pylint support for pylama.
Makefile
3
star
64

starlette-plugins

Create Starlette Plugins easier
Python
3
star
65

example_tornadio_project

Sources for http://klen.github.com/tornadio_socket-io-ru.html
Python
3
star
66

muffin-peewee-aio

Peewee integration to Muffin framework with async support
Python
2
star
67

redux-axios-reducers

Redux Reducers for Axios
CoffeeScript
2
star
68

pytest-redislite

Pytest plugin for testing code using Redi
Python
2
star
69

bottle-manage

Script manager for bottle framework.
Python
2
star
70

muffin-grpc

GRPC Support for Muffin Framework
Python
2
star
71

fquest

ZeroQuest lazy RPG. Moscow Facebook Hackday.
Python
2
star
72

aio-apiclient

Simple Asyncio Client for any HTTP APIs
Python
2
star
73

muffin-jade

Jade templates for Muffin Framework
Python
2
star
74

signalbus

Simple and small library to broadcast signals with typing support
Python
2
star
75

muffin-metrics

Send application metrics to Graphite
Python
2
star
76

hvim

Haskell mode for vim
Vim Script
2
star
77

Flask-jsonrpc-example

Some flask experements
Python
2
star
78

starlette-views

A helper to make views faster with Starlette
Python
2
star
79

hydrogenjs

Simple MVC system for atomjs
JavaScript
1
star
80

simpletree

Fastest and simplest tree implementations for Django
Python
1
star
81

filler

Simple game on javascript and canvas.
JavaScript
1
star
82

modconfig

Simple hierarchic configuration manager for apps
Python
1
star
83

sailplay

Python client for API sailplay.ru
Python
1
star
84

zeta-libs

Frameworks repo for zetalibrary
JavaScript
1
star
85

redux-code

Yet another creators library
TypeScript
1
star
86

tweetchi

Python
1
star
87

muffin-prometheus

Prometheus metrics exporter for Muffin framework
Python
1
star
88

fl

Forbidden lands russian generator
TypeScript
1
star