Make mypy config strict and work on pre-commit

This commit is contained in:
Ayaz Salikhov
2022-03-08 23:23:13 +03:00
parent cfcf11aefe
commit b487d1afbe
5 changed files with 33 additions and 9 deletions

View File

@@ -22,7 +22,10 @@ repos:
rev: v0.931
hooks:
- id: mypy
additional_dependencies: ["pytest", "types-requests", "types-tabulate"]
args: [--config, ./mypy.ini]
additional_dependencies:
["numpy", "pytest", "requests", "types-requests", "types-tabulate"]
stages: [manual]
# Autoformat: YAML, JSON, Markdown, etc.
- repo: https://github.com/pre-commit/mirrors-prettier

View File

@@ -1,8 +1,22 @@
# MMypy is an optional static type checker for Python that aims to combine
# the benefits of dynamic (or "duck") typing and static typing.
#
# Documentation: http://www.mypy-lang.org
# Project: https://github.com/python/mypy
# Config reference: https://mypy.readthedocs.io/en/stable/config_file.html
#
# We use mypy as part of pre-commit checks
[mypy]
python_version = 3.9
follow_imports = normal
strict = False
follow_imports = error
strict = True
no_incremental = True
# This allows us to use pytest decorators, which are not typed yet
disallow_untyped_decorators = False
# These sections allow us to ignore mypy errors for packages
# which are not (hopefully yet) statically typed
[mypy-Cython.*]
ignore_missing_imports = True
@@ -30,3 +44,6 @@ ignore_missing_imports = True
[mypy-tensorflow.*]
ignore_missing_imports = True
[mypy-urllib3.*]
ignore_missing_imports = True

View File

@@ -111,7 +111,9 @@ def r_package_predicate(package: str) -> bool:
return not excluded_package_predicate(package) and package.startswith("r-")
def _check_import_package(package_helper: CondaPackageHelper, command: list[str]):
def _check_import_package(
package_helper: CondaPackageHelper, command: list[str]
) -> None:
"""Generic function executing a command"""
LOGGER.debug(f"Trying to import a package with [{command}] ...")
exec_result = package_helper.running_container.exec_run(command)
@@ -120,12 +122,14 @@ def _check_import_package(package_helper: CondaPackageHelper, command: list[str]
), f"Import package failed, output: {exec_result.output}"
def check_import_python_package(package_helper: CondaPackageHelper, package: str):
def check_import_python_package(
package_helper: CondaPackageHelper, package: str
) -> None:
"""Try to import a Python package from the command line"""
_check_import_package(package_helper, ["python", "-c", f"import {package}"])
def check_import_r_package(package_helper: CondaPackageHelper, package: str):
def check_import_r_package(package_helper: CondaPackageHelper, package: str) -> None:
"""Try to import a R package from the command line"""
_check_import_package(package_helper, ["R", "--slave", "-e", f"library({package})"])

View File

@@ -11,7 +11,7 @@ from docker.models.containers import Container
import pytest # type: ignore
import requests
from requests.packages.urllib3.util.retry import Retry
from urllib3.util.retry import Retry
from requests.adapters import HTTPAdapter
@@ -23,7 +23,7 @@ def find_free_port() -> str:
with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s:
s.bind(("", 0))
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
return s.getsockname()[1]
return s.getsockname()[1] # type: ignore
@pytest.fixture(scope="session")

View File

@@ -23,7 +23,7 @@ def get_test_dirs(
short_image_name: Optional[str],
) -> list[Path]:
if short_image_name is None:
return [] # type: ignore
return []
test_dirs = get_test_dirs(ALL_IMAGES[short_image_name])
if (current_image_tests_dir := THIS_DIR / short_image_name).exists():