From 0ee765a9e5a1aebacc2ca8da6e080ee590a97014 Mon Sep 17 00:00:00 2001 From: Adrien Cossa <21343492+souliane@users.noreply.github.com> Date: Wed, 10 Apr 2024 13:32:29 +0200 Subject: [PATCH 1/4] add mypy to pre-commit --- .pre-commit-config.yaml | 9 +++++++ .../sync_hooks_additional_dependencies.py | 6 ++--- poetry_to_pre_commit/sync_repos.py | 2 +- pyproject.toml | 8 ++++++ tests/conftest.py | 2 +- tests/test_common.py | 8 +++--- ...test_sync_hooks_additional_dependencies.py | 26 +++++++++++-------- tests/test_sync_repos.py | 19 +++++++++----- 8 files changed, 55 insertions(+), 25 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index cd59808..e4a71b1 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -43,3 +43,12 @@ repos: - id: ruff args: [--fix, --unsafe-fixes] - id: ruff-format + - repo: https://github.com/pre-commit/mirrors-mypy + rev: 9db9854e3041219b1eb619872a2dfaf58adfb20b # v1.8.0 + hooks: + - id: mypy + args: [--install-types, --non-interactive] + additional_dependencies: + - poetry==1.8.2 + - pytest==8.1.1 + - ruamel-yaml==0.18.6 diff --git a/poetry_to_pre_commit/sync_hooks_additional_dependencies.py b/poetry_to_pre_commit/sync_hooks_additional_dependencies.py index 5288971..4a5fcb2 100644 --- a/poetry_to_pre_commit/sync_hooks_additional_dependencies.py +++ b/poetry_to_pre_commit/sync_hooks_additional_dependencies.py @@ -25,7 +25,7 @@ def format_bind(value: str) -> tuple[str, set[str]]: def combine_bind_values(bind: list[tuple[str, set[str]]]) -> dict[str, set[str]]: - result = {} + result: dict[str, set[str]] = {} for key, value in bind: result.setdefault(key, set()).update(value) return result @@ -92,7 +92,7 @@ def update_or_remove_additional_deps( def _sync_hooks_additional_dependencies( *, config: dict[str, Any], - deps_by_group: dict[str, list[str]], + deps_by_group: dict[str, set[str]], bind: dict[str, set[str]], no_new_deps: bool = False, ) -> None: @@ -112,7 +112,7 @@ def _sync_hooks_additional_dependencies( groups = bind[hook_id] except KeyError: continue - deps = set() + deps: set[str] = set() for group in groups: deps.update(deps_by_group.get(group, set())) diff --git a/poetry_to_pre_commit/sync_repos.py b/poetry_to_pre_commit/sync_repos.py index 24341c5..0a75059 100644 --- a/poetry_to_pre_commit/sync_repos.py +++ b/poetry_to_pre_commit/sync_repos.py @@ -126,5 +126,5 @@ def sync_repos( ) -def sync_repos_cli(): +def sync_repos_cli() -> None: sync_repos(argv=sys.argv[1:]) diff --git a/pyproject.toml b/pyproject.toml index 570a113..a87fe49 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -82,3 +82,11 @@ extend-ignore = [ [tool.ruff.lint.isort] required-imports = ["from __future__ import annotations"] + +[tool.mypy] +strict = true +pretty = true +show_column_numbers = true +show_error_codes = true +show_error_context = true +warn_unreachable = true diff --git a/tests/conftest.py b/tests/conftest.py index d829494..26e6d63 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -6,5 +6,5 @@ @pytest.fixture -def poetry_cwd(): +def poetry_cwd() -> pathlib.Path: return pathlib.Path(__file__).parent diff --git a/tests/test_common.py b/tests/test_common.py index 5e90736..b53aec5 100644 --- a/tests/test_common.py +++ b/tests/test_common.py @@ -1,9 +1,11 @@ from __future__ import annotations +from pathlib import Path + from poetry_to_pre_commit import common -def test_get_poetry_packages(poetry_cwd): +def test_get_poetry_packages(poetry_cwd: Path) -> None: result = list( common.get_poetry_packages( cwd=poetry_cwd, @@ -13,7 +15,7 @@ def test_get_poetry_packages(poetry_cwd): assert common.PoetryPackage(name="attrs", version="23.2.0") in result -def test_pre_commit_config_roundtrip__no_change(tmp_path): +def test_pre_commit_config_roundtrip__no_change(tmp_path: Path) -> None: file = tmp_path / "file.yaml" yaml = "a: 1\nb: 2\n" file.write_text(yaml) @@ -22,7 +24,7 @@ def test_pre_commit_config_roundtrip__no_change(tmp_path): assert file.read_text() == yaml -def test_pre_commit_config_roundtrip__write_back(tmp_path): +def test_pre_commit_config_roundtrip__write_back(tmp_path: Path) -> None: file = tmp_path / "file.yaml" yaml = "a: 1\nb: 2\n" file.write_text(yaml) diff --git a/tests/test_sync_hooks_additional_dependencies.py b/tests/test_sync_hooks_additional_dependencies.py index 9abd07e..5b3e280 100644 --- a/tests/test_sync_hooks_additional_dependencies.py +++ b/tests/test_sync_hooks_additional_dependencies.py @@ -1,5 +1,7 @@ from __future__ import annotations +from pathlib import Path + import pytest import ruamel.yaml @@ -13,16 +15,16 @@ ("foo=bar,baz", ("foo", {"bar", "baz"})), ], ) -def test_format_bind(value, expected): +def test_format_bind(value: str, expected: tuple[str, set[str]]) -> None: assert sync_hooks_additional_dependencies.format_bind(value=value) == expected -def test_format_bind__error(): +def test_format_bind__error() -> None: with pytest.raises(ValueError): sync_hooks_additional_dependencies.format_bind(value="foo") -def test_combine_bind_values(): +def test_combine_bind_values() -> None: bind = [("foo", {"bar"}), ("foo", {"baz"}), ("qux", {"quux"})] assert sync_hooks_additional_dependencies.combine_bind_values(bind=bind) == { "foo": {"bar", "baz"}, @@ -30,15 +32,17 @@ def test_combine_bind_values(): } -def test_get_sync_hooks_additional_dependencies_parser(): - parser = sync_hooks_additional_dependencies.get_sync_hooks_additional_dependencies_parser() +def test_get_sync_hooks_additional_dependencies_parser() -> None: + parser = ( + sync_hooks_additional_dependencies.get_sync_hooks_additional_dependencies_parser() + ) assert parser.parse_args(["--bind", "foo=bar,baz", "--bind", "foo=qux"]).bind == [ ("foo", {"bar", "baz"}), ("foo", {"qux"}), ] -def test_get_poetry_deps(poetry_cwd): +def test_get_poetry_deps(poetry_cwd: Path) -> None: results = list( sync_hooks_additional_dependencies.get_poetry_deps( cwd=poetry_cwd, @@ -53,7 +57,7 @@ def test_get_poetry_deps(poetry_cwd): ] -def test_get_poetry_deps__error(poetry_cwd): +def test_get_poetry_deps__error(poetry_cwd: Path) -> None: with pytest.raises(SystemError): list( sync_hooks_additional_dependencies.get_poetry_deps( @@ -63,11 +67,11 @@ def test_get_poetry_deps__error(poetry_cwd): ) -def test__sync_hooks_additional_dependencies(): +def test__sync_hooks_additional_dependencies() -> None: config = {"repos": [{"hooks": [{"id": "mypy"}, {"id": "foo"}]}]} deps_by_group = { - "types": ["bar==1", "baz[e]==2"], - "main": ["qux==3"], + "types": {"bar==1", "baz[e]==2"}, + "main": {"qux==3"}, } bind = {"mypy": {"types", "main", "unknown"}, "other_unknown": {"abc"}} sync_hooks_additional_dependencies._sync_hooks_additional_dependencies( @@ -94,7 +98,7 @@ def test__sync_hooks_additional_dependencies(): } -def test_sync_hooks_additional_dependencies(tmp_path, poetry_cwd): +def test_sync_hooks_additional_dependencies(tmp_path: Path, poetry_cwd: Path) -> None: pre_commit_path = tmp_path / ".pre-commit-config.yaml" ruamel.yaml.YAML().dump( { diff --git a/tests/test_sync_repos.py b/tests/test_sync_repos.py index 53bd98f..1e55ca9 100644 --- a/tests/test_sync_repos.py +++ b/tests/test_sync_repos.py @@ -1,5 +1,7 @@ from __future__ import annotations +from pathlib import Path + import pytest import ruamel.yaml @@ -14,7 +16,7 @@ ("https://github.com/foo/mirrors-bar", "bar"), ], ) -def test_repo_url_to_pypi_name(input, expected): +def test_repo_url_to_pypi_name(input: str, expected: str) -> None: assert sync_repos.repo_url_to_pypi_name(input) == expected @@ -37,13 +39,13 @@ def test_repo_url_to_pypi_name(input, expected): ), ], ) -def test_get_parser(input, expected): +def test_get_parser(input: list[str], expected: dict[str, list[str]]) -> None: parser = sync_repos.get_sync_repos_parser() args = parser.parse_args(input) assert vars(args) == expected -def test_get_pre_commit_repos(): +def test_get_pre_commit_repos() -> None: result = sync_repos.get_pre_commit_repos( config={ "repos": [ @@ -75,12 +77,17 @@ def test_get_pre_commit_repos(): ([bar], ["baz"], {"bar": "baz"}, []), ], ) -def test_extract_pypi_names(repos, skip, map, expected): +def test_extract_pypi_names( + repos: list[sync_repos.PreCommitRepo], + skip: list[str], + map: dict[str, str], + expected: list[tuple[str, sync_repos.PreCommitRepo]], +) -> None: result = sync_repos.extract_pypi_names(repos=repos, skip=skip, map=map) assert list(result) == expected -def test_write_precommit_config(): +def test_write_precommit_config() -> None: projects = [ ( sync_repos.PreCommitRepo( @@ -115,7 +122,7 @@ def test_write_precommit_config(): } -def test_sync_repos(tmp_path, poetry_cwd): +def test_sync_repos(tmp_path: Path, poetry_cwd: Path) -> None: pre_commit_path = tmp_path / ".pre-commit-config.yaml" ruamel.yaml.YAML().dump( { From 4e21522abcac8c8d7bbe4fa3ca1a7ab410bc63cd Mon Sep 17 00:00:00 2001 From: Joachim Jablon Date: Wed, 17 Apr 2024 23:44:20 +0200 Subject: [PATCH 2/4] strict pyright --- poetry_to_pre_commit/sync_hooks_additional_dependencies.py | 2 +- pyproject.toml | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/poetry_to_pre_commit/sync_hooks_additional_dependencies.py b/poetry_to_pre_commit/sync_hooks_additional_dependencies.py index 4a5fcb2..8100f48 100644 --- a/poetry_to_pre_commit/sync_hooks_additional_dependencies.py +++ b/poetry_to_pre_commit/sync_hooks_additional_dependencies.py @@ -137,7 +137,7 @@ def sync_hooks_additional_dependencies( args = parser.parse_args(argv) bind = combine_bind_values(args.bind) - deps_by_group = {} + deps_by_group: dict[str, set[str]] = {} for groups in bind.values(): for group in groups: diff --git a/pyproject.toml b/pyproject.toml index a87fe49..97c9abf 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -66,6 +66,7 @@ relative_files = true [tool.pyright] exclude = ["tests"] +typeCheckingMode = "strict" [tool.ruff.lint] extend-select = [ From 7f04a37558051c35a6527c46f07b94dbee8603b0 Mon Sep 17 00:00:00 2001 From: Adrien Cossa <21343492+souliane@users.noreply.github.com> Date: Fri, 3 May 2024 03:47:36 +0200 Subject: [PATCH 3/4] remove mypy and fix pyright errors Keep pyright strict mode, but disable `reportUnknownMemberType` to ignore the errors caused by `ruaml.yaml` methods that return `Any`. --- .pre-commit-config.yaml | 9 --------- poetry_to_pre_commit/common.py | 4 ++-- pyproject.toml | 10 ++-------- 3 files changed, 4 insertions(+), 19 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index e4a71b1..cd59808 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -43,12 +43,3 @@ repos: - id: ruff args: [--fix, --unsafe-fixes] - id: ruff-format - - repo: https://github.com/pre-commit/mirrors-mypy - rev: 9db9854e3041219b1eb619872a2dfaf58adfb20b # v1.8.0 - hooks: - - id: mypy - args: [--install-types, --non-interactive] - additional_dependencies: - - poetry==1.8.2 - - pytest==8.1.1 - - ruamel-yaml==0.18.6 diff --git a/poetry_to_pre_commit/common.py b/poetry_to_pre_commit/common.py index 84a2a01..e6397e2 100644 --- a/poetry_to_pre_commit/common.py +++ b/poetry_to_pre_commit/common.py @@ -4,7 +4,7 @@ import copy import dataclasses import pathlib -from typing import Any, Generator, Iterable +from typing import Any, Generator, Iterable, cast import ruamel.yaml from poetry import factory @@ -15,7 +15,7 @@ def pre_commit_config_roundtrip( path: pathlib.Path, ) -> Generator[dict[str, Any], None, None]: yaml = ruamel.yaml.YAML() - config = yaml.load(path.read_text()) + config = cast(dict[str, Any], yaml.load(path.read_text())) old_config = copy.deepcopy(config) yield config if config != old_config: diff --git a/pyproject.toml b/pyproject.toml index 97c9abf..b313c19 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -67,6 +67,8 @@ relative_files = true [tool.pyright] exclude = ["tests"] typeCheckingMode = "strict" +reportUnknownMemberType = false + [tool.ruff.lint] extend-select = [ @@ -83,11 +85,3 @@ extend-ignore = [ [tool.ruff.lint.isort] required-imports = ["from __future__ import annotations"] - -[tool.mypy] -strict = true -pretty = true -show_column_numbers = true -show_error_codes = true -show_error_context = true -warn_unreachable = true From 69f9c43c3ea8a0debb7cef27f7bda38f34e7ade1 Mon Sep 17 00:00:00 2001 From: Joachim Jablon Date: Fri, 3 May 2024 10:15:24 +0200 Subject: [PATCH 4/4] Enclose runtime type within quotes --- poetry_to_pre_commit/common.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/poetry_to_pre_commit/common.py b/poetry_to_pre_commit/common.py index e6397e2..f536ea8 100644 --- a/poetry_to_pre_commit/common.py +++ b/poetry_to_pre_commit/common.py @@ -15,7 +15,7 @@ def pre_commit_config_roundtrip( path: pathlib.Path, ) -> Generator[dict[str, Any], None, None]: yaml = ruamel.yaml.YAML() - config = cast(dict[str, Any], yaml.load(path.read_text())) + config = cast("dict[str, Any]", yaml.load(path.read_text())) old_config = copy.deepcopy(config) yield config if config != old_config: