style: Add black and isort to ensure code quality¶
Description¶
Abstract¶
Install and configure black and isort to force the code to be well written.
Motivation¶
We previously installed pylint and flake8 to ensure the code is well written, in a clean and common style used among the whole project.
But coding, waiting for the CI just to correct styling is overwhelming.
Rationale¶
Black is the uncompromising Python code formatter. So every code will be formatted the same way, no matter what.
isort is a Python utility / library to sort imports alphabetically, and automatically separated into sections.
5 make commands are added:
make isort: Run the isort tool and update files that need to
make black: Run the black tool and update files that need to
- make check-isort: Run the isort tool in check mode only (won’t
modify files)
- make check-black: Run the black tool in check mode only (won’t
modify files)
make pretty: Run all code beautifiers (isort, black)
And make lint is updated to add check-isort and check-black
Info¶
- Hash
2de7ffd18c5651d3361139cca60129dbf3ff184e
- Date
2019-05-27 17:14:12 +0200
- Parents
style: Add flake8 and pylint to ensure code quality [4458a00c] — 2019-05-27 17:14:11 +0200
- Children
style: Add mypy for better code quality [141b284a] — 2019-05-27 17:14:12 +0200
- Branches
- Tags
(No tags)
Changes¶
Makefile¶
- Type
Modified
- Stats
+26 -2
@@ -66,14 +66,24 @@ tests: ## Run tests for the isshub project.
@pytest
.PHONY: lint
-lint: ## Run all linters (flake8, pylint)
-lint: flake8 pylint
+lint: ## Run all linters (check-isort, check-black, flake8, pylint)
+lint: check-isort check-black flake8 pylint
.PHONY: check checks
check: checks
checks: ## Run all checkers (lint, tests)
checks: lint test
+.PHONY: check-isort
+check-isort: ## Run the isort tool in check mode only (won't modify files)
+ @echo "$(BOLD)Checking isort(RESET)"
+ @isort --check-only 2>&1
+
+.PHONY: check-black
+check-black: ## Run the black tool in check mode only (won't modify files)
+ @echo "$(BOLD)Checking black$(RESET)"
+ @black --target-version py38 --check . 2>&1
+
.PHONY: flake8
flake8: ## Run the flake8 tool
@echo "$(BOLD)Running flake8$(RESET)"
@@ -83,3 +93,17 @@ flake8: ## Run the flake8 tool
pylint: ## Run the pylint tool
@echo "$(BOLD)Running pylint$(RESET)"
@pylint isshub
+
+.PHONY: pretty
+pretty: ## Run all code beautifiers (isort, black)
+pretty: isort black
+
+.PHONY: isort
+isort: ## Run the isort tool and update files that need to
+ @echo "$(BOLD)Running isort$(RESET)"
+ @isort --atomic --apply
+
+.PHONY: black
+black: ## Run the black tool and update files that need to
+ @echo "$(BOLD)Running black$(RESET)"
+ @black --target-version py38 .
README.rst¶
- Type
Modified
- Stats
+37 -2
@@ -130,6 +130,35 @@ Linting
To enforce coding style in python, we'll use:
+`black <https://black.readthedocs.io/en/stable/>`_
+ Black is the "uncompromising code formatter"
+
+ Used with default configuration.
+
+ To check::
+
+ make check-black
+
+ To apply::
+
+ make black
+
+
+`isort <https://isort.readthedocs.io/>`_
+ isort is a Python utility / library to sort imports alphabetically, and automatically separated into sections
+
+ Used with special configuration.
+
+ To check::
+
+ make check-isort
+
+ To apply::
+
+ make isort
+
+But we still use lint checkers:
+
`pylint <https://docs.pylint.org/>`_
Pylint is a tool that checks for errors in Python code, tries to enforce a coding standard and looks for code smells.
@@ -170,9 +199,15 @@ To enforce coding style in python, we'll use:
make flake8
-Other related `make` command:
+Yes, it's a lot. My IDE is configured to run isort + black on every save. So pylint and flake8 should report very few things, and when they does, it will mainly be about code badly written.
+
+Other related `make` commands:
+
+- Run isort and black::
+
+ make pretty
-- Run all lint checkers (pylint, flake8)::
+- Run all lint checkers (isort, black, pylint, flake8 and mypy)::
make lint
setup.cfg¶
- Type
Modified
- Stats
+16 -0
@@ -37,12 +37,14 @@ tests =
pytest-cov
pytest-sugar
lint=
+ black
flake8
flake8-bugbear
flake8-comprehensions
flake8-docstrings
flake8-formatter-abspath
flake8-imports
+ isort
pycodestyle
pydocstyle
pylint
@@ -107,3 +109,17 @@ branch = True
[coverage:report]
exclude_lines =
raise NotImplementedError
+
+[isort]
+combine_as_imports=1
+default_section=THIRDPARTY
+include_trailing_comma=true
+indent=' '
+known_thirdparty_python=factory,pytest,pytest_bdd
+known_first_party=isshub
+line_length=88
+lines_after_imports=2
+multi_line_output=3
+not_skip = __init__.py
+sections=FUTURE,STDLIB,THIRDPARTY_PYTHON,DRF,THIRDPARTY,FIRSTPARTY,LOCALFOLDER
+use_parentheses=1