From 15af1789d3bf5543be0ee7e02b4714f8fee7eb37 Mon Sep 17 00:00:00 2001 From: Charles Patel Date: Mon, 9 Oct 2023 11:29:02 -0500 Subject: [PATCH 1/9] Migrate mypy config to pyproject.toml --- .gitignore | 1 + .pre-commit-config.yaml | 1 + mypy.ini | 46 ----------------------------------------- pyproject.toml | 30 +++++++++++++++++++++++++++ 4 files changed, 32 insertions(+), 46 deletions(-) delete mode 100644 mypy.ini diff --git a/.gitignore b/.gitignore index 249499b135e..4a4f1b738ad 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ _build .DS_Store .vscode +.python-version docs/_static/pypi.svg .tox __pycache__ diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 99b3565ed0e..b6236537213 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -43,6 +43,7 @@ repos: hooks: - id: mypy exclude: ^docs/conf.py + args: ["--config", "pyproject.toml"] additional_dependencies: - types-PyYAML - tomli >= 0.2.6, < 2.0.0 diff --git a/mypy.ini b/mypy.ini deleted file mode 100644 index ad916185bce..00000000000 --- a/mypy.ini +++ /dev/null @@ -1,46 +0,0 @@ -[mypy] -# Specify the target platform details in config, so your developers are -# free to run mypy on Windows, Linux, or macOS and get consistent -# results. -python_version=3.8 - -mypy_path=src - -show_column_numbers=True -show_error_codes=True - -# be strict -strict=True - -# except for... -no_implicit_reexport = False - -# Unreachable blocks have been an issue when compiling mypyc, let's try -# to avoid 'em in the first place. -warn_unreachable=True - -[mypy-blib2to3.driver.*] -ignore_missing_imports = True - -[mypy-IPython.*] -ignore_missing_imports = True - -[mypy-colorama.*] -ignore_missing_imports = True - -[mypy-pathspec.*] -ignore_missing_imports = True - -[mypy-tokenize_rt.*] -ignore_missing_imports = True - -[mypy-uvloop.*] -ignore_missing_imports = True - -[mypy-_black_version.*] -ignore_missing_imports = True - -# CI only checks src/, but in case users are running LSP or similar we explicitly ignore -# errors in test data files. -[mypy-tests.data.*] -ignore_errors = True diff --git a/pyproject.toml b/pyproject.toml index d246eb0b272..0b824549359 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -223,3 +223,33 @@ omit = [ ] [tool.coverage.run] relative_files = true + +[tool.mypy] +# Specify the target platform details in config, so your developers are +# free to run mypy on Windows, Linux, or macOS and get consistent +# results. +python_version = "3.8" +mypy_path = "src" +strict = true +warn_unused_configs = true +warn_redundant_casts = true +warn_unused_ignores = true +warn_return_any = true +# Unreachable blocks have been an issue when compiling mypyc, let's try to avoid 'em in the first place. +warn_unreachable = true +disallow_untyped_calls = true +disallow_untyped_defs = true +disallow_incomplete_defs = true +implicit_reexport = true +show_error_codes = true +show_column_numbers = true + +[[tool.mypy.overrides]] +module = ["pathspec.*", "IPython.*", "colorama.*", "tokenize_rt.*", "uvloop.*", "_black_version.*"] +ignore_missing_imports = true + +# CI only checks src/, but in case users are running LSP or similar we explicitly ignore +# errors in test data files. +[[tool.mypy.overrides]] +module = ["tests.data.*"] +ignore_errors = true From 0727532701c7455731bd9b84c8494cf75a3b3cda Mon Sep 17 00:00:00 2001 From: Charles Patel Date: Mon, 9 Oct 2023 15:09:33 -0500 Subject: [PATCH 2/9] Ignore mypy errors for scripts.* and add aiohttp in mypy extra deps --- .pre-commit-config.yaml | 1 + pyproject.toml | 2 +- src/blackd/__init__.py | 2 +- tests/optional.py | 2 +- tests/test_blackd.py | 2 +- 5 files changed, 5 insertions(+), 4 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index b6236537213..9e38dbbf9a4 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -52,6 +52,7 @@ repos: - platformdirs >= 2.1.0 - pytest - hypothesis + - aiohttp >= 3.7.4 - repo: https://github.com/pre-commit/mirrors-prettier rev: v3.0.3 diff --git a/pyproject.toml b/pyproject.toml index 0b824549359..74948cbe996 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -251,5 +251,5 @@ ignore_missing_imports = true # CI only checks src/, but in case users are running LSP or similar we explicitly ignore # errors in test data files. [[tool.mypy.overrides]] -module = ["tests.data.*"] +module = ["tests.data.*", "scripts.*"] ignore_errors = true diff --git a/src/blackd/__init__.py b/src/blackd/__init__.py index 6b0f3d33295..0da66ff70e0 100644 --- a/src/blackd/__init__.py +++ b/src/blackd/__init__.py @@ -74,7 +74,7 @@ def main(bind_host: str, bind_port: int) -> None: app = make_app() ver = black.__version__ black.out(f"blackd version {ver} listening on {bind_host} port {bind_port}") - web.run_app(app, host=bind_host, port=bind_port, handle_signals=True, print=None) + web.run_app(app, host=bind_host, port=bind_port, handle_signals=True, print=None) # type: ignore[arg-type] def make_app() -> web.Application: diff --git a/tests/optional.py b/tests/optional.py index 8a39cc440a6..3f5277b6b03 100644 --- a/tests/optional.py +++ b/tests/optional.py @@ -26,7 +26,7 @@ from pytest import StashKey except ImportError: # pytest < 7 - from _pytest.store import StoreKey as StashKey # type: ignore[no-redef] + from _pytest.store import StoreKey as StashKey # type: ignore[import, no-redef] log = logging.getLogger(__name__) diff --git a/tests/test_blackd.py b/tests/test_blackd.py index dd2126e6bc2..8207c4409d3 100644 --- a/tests/test_blackd.py +++ b/tests/test_blackd.py @@ -31,7 +31,7 @@ def unittest_run_loop(func, *args, **kwargs): @pytest.mark.blackd -class BlackDTestCase(AioHTTPTestCase): # type: ignore[misc] +class BlackDTestCase(AioHTTPTestCase): def test_blackd_main(self) -> None: with patch("blackd.web.run_app"): result = CliRunner().invoke(blackd.main, []) From ae481edb447bd8f56d9927f9bbb2fb1193af6c02 Mon Sep 17 00:00:00 2001 From: Charles Patel Date: Mon, 9 Oct 2023 21:07:43 -0500 Subject: [PATCH 3/9] Remove mypy configs covered in strict mode --- pyproject.toml | 7 ------- 1 file changed, 7 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 74948cbe996..51976173d9c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -231,15 +231,8 @@ relative_files = true python_version = "3.8" mypy_path = "src" strict = true -warn_unused_configs = true -warn_redundant_casts = true -warn_unused_ignores = true -warn_return_any = true # Unreachable blocks have been an issue when compiling mypyc, let's try to avoid 'em in the first place. warn_unreachable = true -disallow_untyped_calls = true -disallow_untyped_defs = true -disallow_incomplete_defs = true implicit_reexport = true show_error_codes = true show_column_numbers = true From 725871233dcef2f074a80465f1c3250150aa203b Mon Sep 17 00:00:00 2001 From: Charles Patel Date: Mon, 9 Oct 2023 21:33:58 -0500 Subject: [PATCH 4/9] Handle scripts.* mypy error and add additional deps in mypy CICD --- .pre-commit-config.yaml | 3 +++ pyproject.toml | 2 +- scripts/check_pre_commit_rev_in_example.py | 2 +- scripts/check_version_in_basics_example.py | 2 +- scripts/diff_shades_gha_helper.py | 2 +- scripts/fuzz.py | 2 +- scripts/make_width_table.py | 2 +- 7 files changed, 9 insertions(+), 6 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 9e38dbbf9a4..805342fdb38 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -53,6 +53,9 @@ repos: - pytest - hypothesis - aiohttp >= 3.7.4 + - types-commonmark + - urllib3 + - hypothesmith - repo: https://github.com/pre-commit/mirrors-prettier rev: v3.0.3 diff --git a/pyproject.toml b/pyproject.toml index 51976173d9c..d15f1631cf9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -244,5 +244,5 @@ ignore_missing_imports = true # CI only checks src/, but in case users are running LSP or similar we explicitly ignore # errors in test data files. [[tool.mypy.overrides]] -module = ["tests.data.*", "scripts.*"] +module = ["tests.data.*"] ignore_errors = true diff --git a/scripts/check_pre_commit_rev_in_example.py b/scripts/check_pre_commit_rev_in_example.py index 9560b3b8401..107c6444dca 100644 --- a/scripts/check_pre_commit_rev_in_example.py +++ b/scripts/check_pre_commit_rev_in_example.py @@ -14,7 +14,7 @@ import commonmark import yaml -from bs4 import BeautifulSoup +from bs4 import BeautifulSoup # type: ignore[import] def main(changes: str, source_version_control: str) -> None: diff --git a/scripts/check_version_in_basics_example.py b/scripts/check_version_in_basics_example.py index 7f559b3aee1..0f42bafe334 100644 --- a/scripts/check_version_in_basics_example.py +++ b/scripts/check_version_in_basics_example.py @@ -8,7 +8,7 @@ import sys import commonmark -from bs4 import BeautifulSoup +from bs4 import BeautifulSoup # type: ignore[import] def main(changes: str, the_basics: str) -> None: diff --git a/scripts/diff_shades_gha_helper.py b/scripts/diff_shades_gha_helper.py index 7a58fbe9b28..895516deb51 100644 --- a/scripts/diff_shades_gha_helper.py +++ b/scripts/diff_shades_gha_helper.py @@ -119,7 +119,7 @@ def main() -> None: @main.command("config", help="Acquire run configuration and metadata.") @click.argument("event", type=click.Choice(["push", "pull_request"])) def config(event: Literal["push", "pull_request"]) -> None: - import diff_shades + import diff_shades # type: ignore[import] if event == "push": jobs = [{"mode": "preview-changes", "force-flag": "--force-preview-style"}] diff --git a/scripts/fuzz.py b/scripts/fuzz.py index 25362c927d4..8763be524df 100644 --- a/scripts/fuzz.py +++ b/scripts/fuzz.py @@ -80,7 +80,7 @@ def test_idempotent_any_syntatically_valid_python( try: import sys - import atheris + import atheris # type: ignore[import] except ImportError: pass else: diff --git a/scripts/make_width_table.py b/scripts/make_width_table.py index 30fd32c34b0..85c8b623367 100644 --- a/scripts/make_width_table.py +++ b/scripts/make_width_table.py @@ -19,7 +19,7 @@ from os.path import basename, dirname, join from typing import Iterable, Tuple -import wcwidth +import wcwidth # type: ignore[import] def make_width_table() -> Iterable[Tuple[int, int, int]]: From 6aca347dcefd243180cecabbbec13a1d8c48c333 Mon Sep 17 00:00:00 2001 From: Charles Patel Date: Tue, 10 Oct 2023 23:12:11 -0500 Subject: [PATCH 5/9] Add inline comment for removing type: ignore - take 2 --- src/blackd/__init__.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/blackd/__init__.py b/src/blackd/__init__.py index 0da66ff70e0..972f24181cb 100644 --- a/src/blackd/__init__.py +++ b/src/blackd/__init__.py @@ -74,6 +74,8 @@ def main(bind_host: str, bind_port: int) -> None: app = make_app() ver = black.__version__ black.out(f"blackd version {ver} listening on {bind_host} port {bind_port}") + # TODO: aiohttp had an incorrect annotation for `print` argument, + # It'll be fixed once aiohttp releases that code web.run_app(app, host=bind_host, port=bind_port, handle_signals=True, print=None) # type: ignore[arg-type] From 346dd8deba2c0799c15fb5920a5ec6bc2088e45a Mon Sep 17 00:00:00 2001 From: Charles Patel Date: Tue, 10 Oct 2023 23:25:26 -0500 Subject: [PATCH 6/9] Add mypy-args option in hatch build mypyc hook --- .pre-commit-config.yaml | 2 +- pyproject.toml | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 805342fdb38..623e661ac07 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -43,7 +43,7 @@ repos: hooks: - id: mypy exclude: ^docs/conf.py - args: ["--config", "pyproject.toml"] + args: ["--config-file", "pyproject.toml"] additional_dependencies: - types-PyYAML - tomli >= 0.2.6, < 2.0.0 diff --git a/pyproject.toml b/pyproject.toml index d15f1631cf9..58c61497ffc 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -138,6 +138,7 @@ exclude = [ "/src/black/__main__.py", ] options = { debug_level = "0" } +mypy-args = ["--config-file", "pyproject.toml"] [tool.cibuildwheel] build-verbosity = 1 From 7575ae90c996b5a95b3295e92833bc527510e1bf Mon Sep 17 00:00:00 2001 From: Charles Patel Date: Wed, 11 Oct 2023 00:36:06 -0500 Subject: [PATCH 7/9] Update mypy-arg formating --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 58c61497ffc..be1f1b46809 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -138,7 +138,7 @@ exclude = [ "/src/black/__main__.py", ] options = { debug_level = "0" } -mypy-args = ["--config-file", "pyproject.toml"] +mypy-args = ["--config-file=pyproject.toml"] [tool.cibuildwheel] build-verbosity = 1 From a8c3be508b160d36ca836fc651598d4e122a2cb2 Mon Sep 17 00:00:00 2001 From: Charles Patel Date: Wed, 11 Oct 2023 00:42:34 -0500 Subject: [PATCH 8/9] Revert mypy-args in hatch build wheel check --- pyproject.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index be1f1b46809..d15f1631cf9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -138,7 +138,6 @@ exclude = [ "/src/black/__main__.py", ] options = { debug_level = "0" } -mypy-args = ["--config-file=pyproject.toml"] [tool.cibuildwheel] build-verbosity = 1 From 41a5cd9c267d2f646c0427c522568cc920e65545 Mon Sep 17 00:00:00 2001 From: Charles Date: Wed, 11 Oct 2023 01:22:58 -0500 Subject: [PATCH 9/9] Add wildcard --ignore-missing-imports mypy-args --- pyproject.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/pyproject.toml b/pyproject.toml index d15f1631cf9..8c55076e4c9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -137,6 +137,7 @@ exclude = [ # Compiled modules can't be run directly and that's a problem here: "/src/black/__main__.py", ] +mypy-args = ["--ignore-missing-imports"] options = { debug_level = "0" } [tool.cibuildwheel]