Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4eea90c

Browse files
committed
leverage mypy to check language implementations
1 parent 327ed92 commit 4eea90c

4 files changed

Lines changed: 69 additions & 114 deletions

File tree

pre_commit/languages/all.py

Lines changed: 41 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
from typing import Any
2-
from typing import Dict
1+
from typing import Callable
2+
from typing import NamedTuple
3+
from typing import Optional
4+
from typing import Sequence
5+
from typing import Tuple
6+
from typing import TYPE_CHECKING
37

48
from pre_commit.languages import conda
59
from pre_commit.languages import docker
@@ -15,58 +19,43 @@
1519
from pre_commit.languages import script
1620
from pre_commit.languages import swift
1721
from pre_commit.languages import system
22+
from pre_commit.prefix import Prefix
1823

24+
if TYPE_CHECKING:
25+
from pre_commit.repository import Hook
1926

20-
# A language implements the following constant and functions in its module:
21-
#
22-
# # Use None for no environment
23-
# ENVIRONMENT_DIR = 'foo_env'
24-
#
25-
# def get_default_version():
26-
# """Return a value to replace the 'default' value for language_version.
27-
#
28-
# return 'default' if there is no better option.
29-
# """
30-
#
31-
# def healthy(prefix, language_version):
32-
# """Return whether or not the environment is considered functional."""
33-
#
34-
# def install_environment(prefix, version, additional_dependencies):
35-
# """Installs a repository in the given repository. Note that the current
36-
# working directory will already be inside the repository.
37-
#
38-
# Args:
39-
# prefix - `Prefix` bound to the repository.
40-
# version - A version specified in the hook configuration or 'default'.
41-
# """
42-
#
43-
# def run_hook(hook, file_args, color):
44-
# """Runs a hook and returns the returncode and output of running that
45-
# hook.
46-
#
47-
# Args:
48-
# hook - `Hook`
49-
# file_args - The files to be run
50-
# color - whether the hook should be given a pty (when supported)
51-
#
52-
# Returns:
53-
# (returncode, output)
54-
# """
5527

56-
languages: Dict[str, Any] = {
57-
'conda': conda,
58-
'docker': docker,
59-
'docker_image': docker_image,
60-
'fail': fail,
61-
'golang': golang,
62-
'node': node,
63-
'pygrep': pygrep,
64-
'python': python,
65-
'python_venv': python_venv,
66-
'ruby': ruby,
67-
'rust': rust,
68-
'script': script,
69-
'swift': swift,
70-
'system': system,
28+
class Language(NamedTuple):
29+
name: str
30+
# Use `None` for no installation / environment
31+
ENVIRONMENT_DIR: Optional[str]
32+
# return a value to replace `'default` for `language_version`
33+
get_default_version: Callable[[], str]
34+
# return whether the environment is healthy (or should be rebuilt)
35+
healthy: Callable[[Prefix, str], bool]
36+
# install a repository for the given language and language_version
37+
install_environment: Callable[[Prefix, str, Sequence[str]], None]
38+
# execute a hook and return the exit code and output
39+
run_hook: 'Callable[[Hook, Sequence[str], bool], Tuple[int, bytes]]'
40+
41+
42+
# TODO: back to modules + Protocol: https://github.com/python/mypy/issues/5018
43+
languages = {
44+
# BEGIN GENERATED (testing/gen-languages-all)
45+
'conda': Language(name='conda', ENVIRONMENT_DIR=conda.ENVIRONMENT_DIR, get_default_version=conda.get_default_version, healthy=conda.healthy, install_environment=conda.install_environment, run_hook=conda.run_hook), # noqa: E501
46+
'docker': Language(name='docker', ENVIRONMENT_DIR=docker.ENVIRONMENT_DIR, get_default_version=docker.get_default_version, healthy=docker.healthy, install_environment=docker.install_environment, run_hook=docker.run_hook), # noqa: E501
47+
'docker_image': Language(name='docker_image', ENVIRONMENT_DIR=docker_image.ENVIRONMENT_DIR, get_default_version=docker_image.get_default_version, healthy=docker_image.healthy, install_environment=docker_image.install_environment, run_hook=docker_image.run_hook), # noqa: E501
48+
'fail': Language(name='fail', ENVIRONMENT_DIR=fail.ENVIRONMENT_DIR, get_default_version=fail.get_default_version, healthy=fail.healthy, install_environment=fail.install_environment, run_hook=fail.run_hook), # noqa: E501
49+
'golang': Language(name='golang', ENVIRONMENT_DIR=golang.ENVIRONMENT_DIR, get_default_version=golang.get_default_version, healthy=golang.healthy, install_environment=golang.install_environment, run_hook=golang.run_hook), # noqa: E501
50+
'node': Language(name='node', ENVIRONMENT_DIR=node.ENVIRONMENT_DIR, get_default_version=node.get_default_version, healthy=node.healthy, install_environment=node.install_environment, run_hook=node.run_hook), # noqa: E501
51+
'pygrep': Language(name='pygrep', ENVIRONMENT_DIR=pygrep.ENVIRONMENT_DIR, get_default_version=pygrep.get_default_version, healthy=pygrep.healthy, install_environment=pygrep.install_environment, run_hook=pygrep.run_hook), # noqa: E501
52+
'python': Language(name='python', ENVIRONMENT_DIR=python.ENVIRONMENT_DIR, get_default_version=python.get_default_version, healthy=python.healthy, install_environment=python.install_environment, run_hook=python.run_hook), # noqa: E501
53+
'python_venv': Language(name='python_venv', ENVIRONMENT_DIR=python_venv.ENVIRONMENT_DIR, get_default_version=python_venv.get_default_version, healthy=python_venv.healthy, install_environment=python_venv.install_environment, run_hook=python_venv.run_hook), # noqa: E501
54+
'ruby': Language(name='ruby', ENVIRONMENT_DIR=ruby.ENVIRONMENT_DIR, get_default_version=ruby.get_default_version, healthy=ruby.healthy, install_environment=ruby.install_environment, run_hook=ruby.run_hook), # noqa: E501
55+
'rust': Language(name='rust', ENVIRONMENT_DIR=rust.ENVIRONMENT_DIR, get_default_version=rust.get_default_version, healthy=rust.healthy, install_environment=rust.install_environment, run_hook=rust.run_hook), # noqa: E501
56+
'script': Language(name='script', ENVIRONMENT_DIR=script.ENVIRONMENT_DIR, get_default_version=script.get_default_version, healthy=script.healthy, install_environment=script.install_environment, run_hook=script.run_hook), # noqa: E501
57+
'swift': Language(name='swift', ENVIRONMENT_DIR=swift.ENVIRONMENT_DIR, get_default_version=swift.get_default_version, healthy=swift.healthy, install_environment=swift.install_environment, run_hook=swift.run_hook), # noqa: E501
58+
'system': Language(name='system', ENVIRONMENT_DIR=system.ENVIRONMENT_DIR, get_default_version=system.get_default_version, healthy=system.healthy, install_environment=system.install_environment, run_hook=system.run_hook), # noqa: E501
59+
# END GENERATED
7160
}
7261
all_languages = sorted(languages)

pre_commit/repository.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ def install(self) -> None:
113113
logger.info('This may take a few minutes...')
114114

115115
lang = languages[self.language]
116+
assert lang.ENVIRONMENT_DIR is not None
116117
venv = environment_dir(lang.ENVIRONMENT_DIR, self.language_version)
117118

118119
# There's potentially incomplete cleanup from previous runs

testing/gen-languages-all

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env python3
2+
import sys
3+
4+
LANGUAGES = [
5+
'conda', 'docker', 'docker_image', 'fail', 'golang', 'node', 'pygrep',
6+
'python', 'python_venv', 'ruby', 'rust', 'script', 'swift', 'system',
7+
]
8+
FIELDS = [
9+
'ENVIRONMENT_DIR', 'get_default_version', 'healthy', 'install_environment',
10+
'run_hook',
11+
]
12+
13+
14+
def main() -> int:
15+
print(f' # BEGIN GENERATED ({sys.argv[0]})')
16+
for lang in LANGUAGES:
17+
parts = [f' {lang!r}: Language(name={lang!r}']
18+
for k in FIELDS:
19+
parts.append(f', {k}={lang}.{k}')
20+
parts.append('), # noqa: E501')
21+
print(''.join(parts))
22+
print(' # END GENERATED')
23+
return 0
24+
25+
26+
if __name__ == '__main__':
27+
exit(main())

tests/languages/all_test.py

Lines changed: 0 additions & 62 deletions
This file was deleted.

0 commit comments

Comments
 (0)