Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strict pyright #17

Merged
merged 4 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions poetry_to_pre_commit/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down
8 changes: 4 additions & 4 deletions poetry_to_pre_commit/sync_hooks_additional_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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()))
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion poetry_to_pre_commit/sync_repos.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,5 @@ def sync_repos(
)


def sync_repos_cli():
def sync_repos_cli() -> None:
sync_repos(argv=sys.argv[1:])
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ relative_files = true

[tool.pyright]
exclude = ["tests"]
typeCheckingMode = "strict"
reportUnknownMemberType = false


[tool.ruff.lint]
extend-select = [
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@


@pytest.fixture
def poetry_cwd():
def poetry_cwd() -> pathlib.Path:
return pathlib.Path(__file__).parent
8 changes: 5 additions & 3 deletions tests/test_common.py
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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)
Expand All @@ -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)
Expand Down
26 changes: 15 additions & 11 deletions tests/test_sync_hooks_additional_dependencies.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations

from pathlib import Path

import pytest
import ruamel.yaml

Expand All @@ -13,32 +15,34 @@
("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"},
"qux": {"quux"},
}


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,
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
{
Expand Down
19 changes: 13 additions & 6 deletions tests/test_sync_repos.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations

from pathlib import Path

import pytest
import ruamel.yaml

Expand All @@ -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


Expand All @@ -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": [
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
{
Expand Down
Loading