Skip to content

Commit 8f0a73e

Browse files
authored
Merge pull request #1646 from mathbunnyru/asalikhov/update_mypy_config
Update mypy config and make it work well with pre-commit
2 parents f519acb + d502cbe commit 8f0a73e

File tree

6 files changed

+47
-10
lines changed

6 files changed

+47
-10
lines changed

.pre-commit-config.yaml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,17 @@ repos:
2222
rev: v0.931
2323
hooks:
2424
- id: mypy
25-
additional_dependencies: ["pytest", "types-requests", "types-tabulate"]
25+
args: [--config, ./mypy.ini]
26+
additional_dependencies:
27+
["numpy", "pytest", "requests", "types-requests", "types-tabulate"]
28+
# Unfortunately, `pre-commit` only runs on changed files
29+
# This doesn't work well with `mypy --follow-imports error`
30+
# See: https://github.com/pre-commit/mirrors-mypy/issues/34#issuecomment-1062160321
31+
#
32+
# To workaround this we run `mypy` only in manual mode
33+
# So it won't run as part of `git commit` command
34+
# But it will still be run as part of `pre-commit` workflow and give expected results
35+
stages: [manual]
2636

2737
# Autoformat: YAML, JSON, Markdown, etc.
2838
- repo: https://github.com/pre-commit/mirrors-prettier

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ img-rm-dang: ## remove dangling images (tagged None)
170170

171171

172172
pre-commit-all: ## run pre-commit hook on all files
173-
@pre-commit run --all-files || (printf "\n\n\n" && git --no-pager diff --color=always)
173+
@pre-commit run --all-files --hook-stage manual || (printf "\n\n\n" && git --no-pager diff --color=always)
174174
pre-commit-install: ## set up the git hook scripts
175175
@pre-commit --version
176176
@pre-commit install

mypy.ini

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,25 @@
1+
# Mypy is an optional static type checker for Python that aims to combine
2+
# the benefits of dynamic (or "duck") typing and static typing.
3+
#
4+
# Documentation: http://www.mypy-lang.org
5+
# Project: https://github.com/python/mypy
6+
# Config reference: https://mypy.readthedocs.io/en/stable/config_file.html
7+
#
8+
# We use mypy as part of pre-commit checks
9+
110
[mypy]
211
python_version = 3.9
3-
follow_imports = normal
4-
strict = False
12+
follow_imports = error
13+
strict = True
514
no_incremental = True
15+
# This allows us to use pytest decorators, which are not typed yet
16+
disallow_untyped_decorators = False
17+
18+
# These sections allow us to ignore mypy errors for packages
19+
# which are not (hopefully yet) statically typed
20+
21+
[mypy-Cython.*]
22+
ignore_missing_imports = True
623

724
[mypy-docker.*]
825
ignore_missing_imports = True
@@ -22,5 +39,11 @@ ignore_missing_imports = True
2239
[mypy-pyspark.*]
2340
ignore_missing_imports = True
2441

42+
[mypy-setuptools.*]
43+
ignore_missing_imports = True
44+
2545
[mypy-tensorflow.*]
2646
ignore_missing_imports = True
47+
48+
[mypy-urllib3.*]
49+
ignore_missing_imports = True

tests/base-notebook/test_packages.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,9 @@ def r_package_predicate(package: str) -> bool:
111111
return not excluded_package_predicate(package) and package.startswith("r-")
112112

113113

114-
def _check_import_package(package_helper: CondaPackageHelper, command: list[str]):
114+
def _check_import_package(
115+
package_helper: CondaPackageHelper, command: list[str]
116+
) -> None:
115117
"""Generic function executing a command"""
116118
LOGGER.debug(f"Trying to import a package with [{command}] ...")
117119
exec_result = package_helper.running_container.exec_run(command)
@@ -120,12 +122,14 @@ def _check_import_package(package_helper: CondaPackageHelper, command: list[str]
120122
), f"Import package failed, output: {exec_result.output}"
121123

122124

123-
def check_import_python_package(package_helper: CondaPackageHelper, package: str):
125+
def check_import_python_package(
126+
package_helper: CondaPackageHelper, package: str
127+
) -> None:
124128
"""Try to import a Python package from the command line"""
125129
_check_import_package(package_helper, ["python", "-c", f"import {package}"])
126130

127131

128-
def check_import_r_package(package_helper: CondaPackageHelper, package: str):
132+
def check_import_r_package(package_helper: CondaPackageHelper, package: str) -> None:
129133
"""Try to import a R package from the command line"""
130134
_check_import_package(package_helper, ["R", "--slave", "-e", f"library({package})"])
131135

tests/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import pytest # type: ignore
1212
import requests
1313

14-
from requests.packages.urllib3.util.retry import Retry
14+
from urllib3.util.retry import Retry
1515
from requests.adapters import HTTPAdapter
1616

1717

@@ -23,7 +23,7 @@ def find_free_port() -> str:
2323
with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s:
2424
s.bind(("", 0))
2525
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
26-
return s.getsockname()[1]
26+
return s.getsockname()[1] # type: ignore
2727

2828

2929
@pytest.fixture(scope="session")

tests/images_hierarchy.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def get_test_dirs(
2323
short_image_name: Optional[str],
2424
) -> list[Path]:
2525
if short_image_name is None:
26-
return [] # type: ignore
26+
return []
2727

2828
test_dirs = get_test_dirs(ALL_IMAGES[short_image_name])
2929
if (current_image_tests_dir := THIS_DIR / short_image_name).exists():

0 commit comments

Comments
 (0)