Skip to content

Commit

Permalink
Merge pull request #48 from moshemoshe137/mypy
Browse files Browse the repository at this point in the history
Add `mypy` support
  • Loading branch information
mxr authored Dec 29, 2024
2 parents bcf3d6d + db2bee7 commit 25a3ed7
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 8 deletions.
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'
)

0 comments on commit 25a3ed7

Please sign in to comment.