From 3d6faf689eebfc54851860a313b34a02a4ce2650 Mon Sep 17 00:00:00 2001 From: KotlinIsland Date: Wed, 23 Mar 2022 11:58:12 +1000 Subject: [PATCH] add black/isort configuration --- .git-blame-ignore-revs | 1 + .github/workflows/test.yml | 2 +- .pre-commit-config.yaml | 9 +++++++++ README.md | 3 ++- pyproject.toml | 21 +++++++++++++++++++++ runtests.py | 8 +++++--- setup.cfg | 4 +++- test-requirements.txt | 2 ++ tox.ini | 5 ++++- 9 files changed, 48 insertions(+), 7 deletions(-) create mode 100644 .git-blame-ignore-revs create mode 100644 .pre-commit-config.yaml diff --git a/.git-blame-ignore-revs b/.git-blame-ignore-revs new file mode 100644 index 0000000000000..a11f7314633c4 --- /dev/null +++ b/.git-blame-ignore-revs @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 5b975931f0e3b..6b337155ae95a 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -86,7 +86,7 @@ jobs: arch: x64 os: windows-latest toxenv: type - - name: Code style with flake8 + - name: Formatting with Black + isort and code style with flake8 python: '3.7' arch: x64 os: ubuntu-latest diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000000000..2b364e74faca5 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,9 @@ +repos: + - repo: https://github.com/psf/black + rev: 22.6.0 # must match test-requirements.txt + hooks: + - id: black + - repo: https://github.com/pycqa/isort + rev: 5.10.1 # must match test-requirements.txt + hooks: + - id: isort diff --git a/README.md b/README.md index f9b7fc1cd8536..76e5a4a7795d0 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,8 @@ Mypy: Static Typing for Python [![Documentation Status](https://readthedocs.org/projects/mypy/badge/?version=latest)](https://mypy.readthedocs.io/en/latest/?badge=latest) [![Chat at https://gitter.im/python/typing](https://badges.gitter.im/python/typing.svg)](https://gitter.im/python/typing?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) [![Checked with mypy](http://www.mypy-lang.org/static/mypy_badge.svg)](http://mypy-lang.org/) - +[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg")](https://github.com/psf/black) +[![Imports: isort](https://img.shields.io/badge/%20imports-isort-%231674b1?style=flat&labelColor=ef8336)](https://pycqa.github.io/isort/) Got a question? --------------- diff --git a/pyproject.toml b/pyproject.toml index c8f1e558b9631..75cf925743cf9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,3 +4,24 @@ requires = [ "wheel >= 0.30.0", ] build-backend = "setuptools.build_meta" + +[tool.black] +line-length = 99 +target-version = ['py37'] +skip-magic-trailing-comma = true +extend-exclude = ''' +^/mypy/typeshed| +^/mypyc/test-data| +^/test-data +''' + +[tool.isort] +profile = "black" +line_length = 99 +combine_as_imports = true +skip_gitignore = true +skip = [ + "mypy/typeshed", + "mypyc/test-data", + "test-data", +] diff --git a/runtests.py b/runtests.py index 57542f7d458d8..b075fdb4a5199 100755 --- a/runtests.py +++ b/runtests.py @@ -52,8 +52,10 @@ 'self': [executable, '-m', 'mypy', '--config-file', 'mypy_self_check.ini', '-p', 'mypy'], # Lint 'lint': ['flake8', '-j0'], + "format-black": ["black", "."], + "format-isort": ["isort", "."], # Fast test cases only (this is the bulk of the test suite) - 'pytest-fast': ['pytest', '-q', '-k', 'not (%s)' % ' or '.join(ALL_NON_FAST)], + 'pytest-fast': ['pytest', '-q', '-k', f"not ({' or '.join(ALL_NON_FAST)})"], # Test cases that invoke mypy (with small inputs) 'pytest-cmdline': ['pytest', '-q', '-k', ' or '.join([CMDLINE, EVALUATION, @@ -118,7 +120,7 @@ def wait_background_cmd(name: str, proc: Popen) -> int: print(f'run {name}: {cmds[name]}') if status: print(output.decode().rstrip()) - print('\nFAILED: %s' % name) + print("\nFAILED:", name) if name in FAST_FAIL: exit(status) return status @@ -128,7 +130,7 @@ def main() -> None: prog, *args = argv if not set(args).issubset(cmds): - print("usage:", prog, " ".join('[%s]' % k for k in cmds)) + print("usage:", prog, " ".join(f"[{k}]" for k in cmds)) print() print('Run the given tests. If given no arguments, run everything except' + ' pytest-extra and mypyc-extra.') diff --git a/setup.cfg b/setup.cfg index c7adc285db7b3..d822fdc7a8c89 100644 --- a/setup.cfg +++ b/setup.cfg @@ -34,6 +34,8 @@ exclude = # Things to ignore: # E128: continuation line under-indented (too noisy) +# E203: conflicts with black +# E501: conflicts with black # W601: has_key() deprecated (false positives) # E701: multiple statements on one line (colon) (we use this for classes with empty body) # E704: multiple statements on one line (def) @@ -44,7 +46,7 @@ exclude = # B011: Don't use assert False # F821: Name not defined (generates false positives with error codes) # E741: Ambiguous variable name -extend-ignore = E128,W601,E701,E704,E402,B3,B006,B007,B011,F821,E741 +extend-ignore = E128,E203,E501,W601,E701,E704,E402,B3,B006,B007,B011,F821,E741 [coverage:run] branch = true diff --git a/test-requirements.txt b/test-requirements.txt index d5de4f2ace426..215943f4aef69 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -1,11 +1,13 @@ -r mypy-requirements.txt -r build-requirements.txt attrs>=18.0 +black==22.6.0 # must match version in .pre-commit-config.yaml filelock>=3.3.0,<3.4.2; python_version<'3.7' filelock>=3.3.0; python_version>='3.7' flake8==3.9.2 flake8-bugbear==22.3.20 flake8-pyi>=20.5 +isort[colors]==5.10.1 # must match version in .pre-commit-config.yaml lxml>=4.4.0; python_version<'3.11' psutil>=4.0 # pytest 6.2.3 does not support Python 3.10 diff --git a/tox.ini b/tox.ini index 7cee9a11f7882..c2159cd6fdba0 100644 --- a/tox.ini +++ b/tox.ini @@ -47,7 +47,10 @@ parallel_show_output = True [testenv:lint] description = check the code style -commands = flake8 {posargs} +commands = + flake8 {posargs} + black --check --diff . + isort --check --diff . [testenv:type] description = type check ourselves