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 2 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
8 changes: 6 additions & 2 deletions sync_pre_commit_deps.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from __future__ import annotations

import argparse
import re
from collections.abc import Sequence

import ruamel.yaml

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


_ARGUMENT_HELP_TEMPLATE = (
Expand Down Expand Up @@ -55,7 +56,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']
hook_rev = re.match(r'[vV]?(?P<rev>.+)$', repo['rev'])
# assert for mypy
assert hook_rev is not None, f'Invalid rev {repo["rev"]!r}'
versions[hid] = hook_rev.group('rev')
mxr marked this conversation as resolved.
Show resolved Hide resolved

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'
)