From 224a59e8649d296d68e111f45562f5900b789f06 Mon Sep 17 00:00:00 2001 From: AlexWaygood Date: Sun, 25 Jun 2023 14:41:54 +0100 Subject: [PATCH 1/4] Improve ruff config --- .github/workflows/test.yml | 2 +- .pre-commit-config.yaml | 4 ---- CONTRIBUTING.md | 11 ++--------- README.md | 2 +- mypy/copytype.py | 2 +- pyproject.toml | 23 ++++++++--------------- test-requirements.txt | 1 - 7 files changed, 13 insertions(+), 32 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 6fc8fb11c6f4..5bd20f2773c5 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -96,7 +96,7 @@ jobs: # We also run these checks with pre-commit in CI, # but it's useful to run them with tox too, # to ensure the tox env works as expected - - name: Formatting with Black + isort and code style with ruff + - name: Formatting and code style with Black + ruff python: '3.10' arch: x64 os: ubuntu-latest diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index cbb3672bd986..a40ff24b5c98 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -9,10 +9,6 @@ repos: rev: 23.3.0 # must match test-requirements.txt hooks: - id: black - - repo: https://github.com/pycqa/isort - rev: 5.12.0 # must match test-requirements.txt - hooks: - - id: isort - repo: https://github.com/astral-sh/ruff-pre-commit rev: v0.0.272 # must match test-requirements.txt hooks: diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 99ba2f8c9a76..82e55f437e87 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -88,11 +88,8 @@ pytest -n0 -k 'test_name' # Run all test cases in the "test-data/unit/check-dataclasses.test" file pytest mypy/test/testcheck.py::TypeCheckSuite::check-dataclasses.test -# Run the linter -ruff . - -# Run formatters -black . && isort . +# Run the formatters and linters +python runtests.py lint ``` For an in-depth guide on running and writing tests, @@ -154,10 +151,6 @@ advice about good pull requests for open-source projects applies; we have [our own writeup](https://github.com/python/mypy/wiki/Good-Pull-Request) of this advice. -We are using `black` and `isort` to enforce a consistent coding style. -Run `black . && isort .` before your commits, otherwise you would receive -a CI failure. - Also, do not squash your commits after you have submitted a pull request, as this erases context during review. We will squash commits when the pull request is merged. diff --git a/README.md b/README.md index 164957b1491a..8b1ebbc0f2cb 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ Mypy: Static Typing for Python [![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](https://www.mypy-lang.org/static/mypy_badge.svg)](https://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/) +[![Linting: Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/charliermarsh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff) Got a question? --------------- diff --git a/mypy/copytype.py b/mypy/copytype.py index 0b63c8e07ae8..4ca381c4a8c4 100644 --- a/mypy/copytype.py +++ b/mypy/copytype.py @@ -28,7 +28,7 @@ ) # type_visitor needs to be imported after types -from mypy.type_visitor import TypeVisitor # isort: skip +from mypy.type_visitor import TypeVisitor # ruff: isort: skip def copy_type(t: ProperType) -> ProperType: diff --git a/pyproject.toml b/pyproject.toml index fc70d4279681..4b454b0df7e7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -27,29 +27,18 @@ force-exclude = ''' ^/test-data ''' -[tool.isort] -py_version = 37 -profile = "black" -line_length = 99 -combine_as_imports = true -skip_gitignore = true -extra_standard_library = ["typing_extensions"] -skip_glob = [ - "mypy/typeshed/*", - "mypyc/test-data/*", - "test-data/*", -] - [tool.ruff] line-length = 99 target-version = "py37" +fix = true select = [ - "E", # pycoderstyle (error) + "E", # pycodestyle (error) "F", # pyflakes "B", # flake8-bugbear + "I", # isort "RUF100", # Unused noqa comments - "PGH004" # blanket noqa comments + "PGH004" # blanket noqa comments ] ignore = [ @@ -76,3 +65,7 @@ extend-exclude = [ # typeshed has its own .pyi-specific configuration "mypy/typeshed/*", ] + +[tool.ruff.isort] +combine-as-imports = true +extra-standard-library = ["typing_extensions"] diff --git a/test-requirements.txt b/test-requirements.txt index f7d37058e544..6b046e1469eb 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -3,7 +3,6 @@ attrs>=18.0 black==23.3.0 # must match version in .pre-commit-config.yaml filelock>=3.3.0 -isort[colors]==5.12.0; python_version >= "3.8" # must match version in .pre-commit-config.yaml lxml>=4.9.1; (python_version<'3.11' or sys_platform!='win32') and python_version<'3.12' pre-commit pre-commit-hooks==4.4.0 From 87ceac8a15519b59fdb414e12789be19d6e7b9b3 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Mon, 26 Jun 2023 00:46:42 +0100 Subject: [PATCH 2/4] Mark some codes as unfixable --- pyproject.toml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/pyproject.toml b/pyproject.toml index 4b454b0df7e7..b343aa878b6f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -53,6 +53,13 @@ ignore = [ "E741", # Ambiguous variable name ] +unfixable = [ + "F841", # unused variable. ruff keeps the call, but mostly we want to get rid of it all + "F601", # automatic fix might obscure issue + "F602", # automatic fix might obscure issue + "B018", # automatic fix might obscure issue +] + extend-exclude = [ "@*", # Sphinx configuration is irrelevant From 9facc00653b7f100f37b7fe433f780b6c84a2ed3 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Mon, 26 Jun 2023 09:56:16 +0100 Subject: [PATCH 3/4] Update .pre-commit-config.yaml --- .pre-commit-config.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index a40ff24b5c98..1ad273ce5584 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -13,3 +13,4 @@ repos: rev: v0.0.272 # must match test-requirements.txt hooks: - id: ruff + args: [--exit-non-zero-on-fix] From e9695de88ff626db48a318d80e99cef81ee6d6c1 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Mon, 26 Jun 2023 17:13:11 +0100 Subject: [PATCH 4/4] speedup setting up the tox env in CI --- tox.ini | 1 + 1 file changed, 1 insertion(+) diff --git a/tox.ini b/tox.ini index 2ddda8281beb..b5314114570b 100644 --- a/tox.ini +++ b/tox.ini @@ -43,6 +43,7 @@ commands = [testenv:lint] description = check the code style skip_install = true +deps = pre-commit commands = pre-commit run --all-files --show-diff-on-failure [testenv:type]