Skip to content

Instantly share code, notes, and snippets.

@guilatrova
Last active December 6, 2021 15:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guilatrova/c1483789aced7dc9af94472ab3cedb67 to your computer and use it in GitHub Desktop.
Save guilatrova/c1483789aced7dc9af94472ab3cedb67 to your computer and use it in GitHub Desktop.
Base Python Config: pre-commit hooks and pyproject.toml
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.4.0
hooks:
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/psf/black
rev: 21.11b1
hooks:
- id: black
- repo: https://gitlab.com/pycqa/flake8
rev: 4.0.1
hooks:
- id: flake8
exclude: samples/
- repo: https://github.com/timothycrosley/isort
rev: 5.10.1
hooks:
- id: isort
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.910
hooks:
- id: mypy
additional_dependencies: [types-toml==0.1.3]
- repo: https://github.com/guilatrova/tryceratops
rev: v1.0.0
hooks:
- id: tryceratops
[tool.poetry]
...
packages = [
{ include = "PACKAGE_NAME", from = "src" },
]
[tool.black]
line-length = 120
[tool.isort]
profile = "black"
line_length = 120
extra_standard_library = ["pytest"]
[tool.mypy]
ignore_missing_imports = true
[flake8]
max-line-length=120
extend-ignore=E203
exclude=
__init__.py
@guilatrova
Copy link
Author

Pre-commit config

This template uses the following linters:

Linter Reason
black Formatting
flake8 General linting (e.g. unused variables)
isort Sort of imports
mypy Typing validation
tryceratops Avoid try/except anti-patterns
end-of-file-fixer / trailing-whitespace General clean up

pyproject.toml

line length

I don't have strong opinions about line length, but 88 is often too short. The challenge is to keep all linters "agree" with each other, so the above model ensures it's 120 as default and makes it obvious for you to replace it with any other number you prefer.

packages

When testing a CLI or related package that I need to import globally, it's useful to add this section to make poetry install your package. (Similar to pip install -e .).

extra_standard_library

I feel pytest is often part of the standard python lib (it's not), so it makes me comfortable to keep it at the top when sorting.

ignore_missing_imports

When this flag is disable mypy can get really noisy.

setup.cfg

The only purpose of this file is because flake8 still doesn't support pyproject.toml.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment