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

Add mypy support #48

Merged
merged 4 commits into from
Dec 29, 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ $ sync-pre-commit-deps path/to/.pre-commit-config.yaml

## what it does

Ensures tools which declare `flake8` and `black` as additional dependencies will have those versions synced with the `flake8` and `black` versions in the rest of the config. For example, `flake8` under `yesqa` is updated from `5.0.0` to `6.0.0`.
Ensures tools which declare `flake8`, `black`, or `mypy` as additional dependencies will have those versions synced with the `flake8`, `black`, or `mypy` versions in the rest of the config. For example, `flake8` under `yesqa` is updated from `5.0.0` to `6.0.0`.

```diff
repos:
Expand Down
7 changes: 5 additions & 2 deletions sync_pre_commit_deps.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import ruamel.yaml

SUPPORTED = frozenset(('black', 'flake8'))
SUPPORTED = frozenset(('black', 'flake8', 'mypy'))


_ARGUMENT_HELP_TEMPLATE = (
Expand Down Expand Up @@ -55,7 +55,10 @@ def main(argv: Sequence[str] | None = None) -> int:
for repo in loaded['repos']:
for hook in repo['hooks']:
if (hid := hook['id']) in SUPPORTED:
versions[hid] = repo['rev']
# `mirrors-mypy` uses versions with a 'v' prefix, so we have to
# strip it out to get the mypy version.
cleaned_rev = repo['rev'].removeprefix('v')
versions[hid] = cleaned_rev

updated = []
for repo in loaded['repos']:
Expand Down
48 changes: 43 additions & 5 deletions tests/sync_pre_commit_deps_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def test_main_noop(tmpdir):
assert cfg.read() == s


def test_main_writes_both(tmpdir):
def test_main_writes_all(tmpdir):
cfg = tmpdir.join('.pre-commit-config.yaml')
cfg.write(
'repos:\n'
Expand All @@ -44,7 +44,12 @@ def test_main_writes_both(tmpdir):
' rev: 6.0.0\n'
' hooks:\n'
' - id: flake8\n'
# all 3 below should be rewritten
# gives the `mypy` version
'- repo: https://github.com/pre-commit/mirrors-mypy\n'
' rev: v1.13.0\n'
' hooks:\n'
' - id: mypy\n'
# all repos below should have their additional_dependencies rewritten
'- repo: https://github.com/asottile/yesqa\n'
' rev: v1.5.0\n'
' hooks:\n'
Expand All @@ -57,13 +62,20 @@ def test_main_writes_both(tmpdir):
' - id: blacken-docs\n'
' additional_dependencies:\n'
' - black==22.12.0\n'
'- repo: https://github.com/nbQA-dev/nbQA\n'
' rev: 1.9.1\n'
' hooks:\n'
' - id: nbqa-mypy\n'
' additional_dependencies:\n'
' - mypy==0.910\n'
'- repo: https://github.com/example/example\n'
' rev: v1.0.0\n'
' hooks:\n'
' - id: example\n'
' additional_dependencies:\n'
' - black==22.12.0\n'
' - flake8==5.0.0\n',
' - flake8==5.0.0\n'
' - mypy==0.123\n',
)

assert main((str(cfg),))
Expand All @@ -82,6 +94,10 @@ def test_main_writes_both(tmpdir):
' rev: 6.0.0\n'
' hooks:\n'
' - id: flake8\n'
'- repo: https://github.com/pre-commit/mirrors-mypy\n'
' rev: v1.13.0\n'
' hooks:\n'
' - id: mypy\n'
'- repo: https://github.com/asottile/yesqa\n'
' rev: v1.5.0\n'
' hooks:\n'
Expand All @@ -94,13 +110,20 @@ def test_main_writes_both(tmpdir):
' - id: blacken-docs\n'
' additional_dependencies:\n'
' - black==23.3.0\n'
'- repo: https://github.com/nbQA-dev/nbQA\n'
' rev: 1.9.1\n'
' hooks:\n'
' - id: nbqa-mypy\n'
' additional_dependencies:\n'
' - mypy==1.13.0\n'
'- repo: https://github.com/example/example\n'
' rev: v1.0.0\n'
' hooks:\n'
' - id: example\n'
' additional_dependencies:\n'
' - black==23.3.0\n'
' - flake8==6.0.0\n'
' - mypy==1.13.0\n'
)


Expand All @@ -117,15 +140,23 @@ def test_main_no_dep_on_one_and_writes_other(tmpdir):
' hooks:\n'
' - id: yesqa\n'
' additional_dependencies:\n'
# should not be rewritten because target version can't be found
# should not be rewritten because target versions can't be found
' - flake8==5.0.0\n'
' - mypy==0.910\n'
'- repo: https://github.com/adamchainz/blacken-docs\n'
' rev: 1.15.0\n'
' hooks:\n'
' - id: blacken-docs\n'
' additional_dependencies:\n'
# should be rewritten
' - black==22.12.0\n',
' - black==22.12.0\n'
'- repo: https://github.com/nbQA-dev/nbQA\n'
' rev: 1.9.1\n'
' hooks:\n'
' - id: nbqa-mypy\n'
' additional_dependencies:\n'
# should not be rewritten because target version can't be found
' - mypy==0.123',
)

assert main((str(cfg),))
Expand All @@ -142,10 +173,17 @@ def test_main_no_dep_on_one_and_writes_other(tmpdir):
' - id: yesqa\n'
' additional_dependencies:\n'
' - flake8==5.0.0\n'
' - mypy==0.910\n'
'- repo: https://github.com/adamchainz/blacken-docs\n'
' rev: 1.15.0\n'
' hooks:\n'
' - id: blacken-docs\n'
' additional_dependencies:\n'
' - black==23.3.0\n'
'- repo: https://github.com/nbQA-dev/nbQA\n'
' rev: 1.9.1\n'
' hooks:\n'
' - id: nbqa-mypy\n'
' additional_dependencies:\n'
' - mypy==0.123\n'
)