Skip to content

Commit

Permalink
Merge pull request #1646 from mathbunnyru/asalikhov/update_mypy_config
Browse files Browse the repository at this point in the history
Update mypy config and make it work well with pre-commit
  • Loading branch information
mathbunnyru authored Mar 8, 2022
2 parents f519acb + d502cbe commit 8f0a73e
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 10 deletions.
12 changes: 11 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,17 @@ 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"]
# Unfortunately, `pre-commit` only runs on changed files
# This doesn't work well with `mypy --follow-imports error`
# See: https://github.com/pre-commit/mirrors-mypy/issues/34#issuecomment-1062160321
#
# To workaround this we run `mypy` only in manual mode
# So it won't run as part of `git commit` command
# But it will still be run as part of `pre-commit` workflow and give expected results
stages: [manual]

# Autoformat: YAML, JSON, Markdown, etc.
- repo: https://github.com/pre-commit/mirrors-prettier
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ img-rm-dang: ## remove dangling images (tagged None)


pre-commit-all: ## run pre-commit hook on all files
@pre-commit run --all-files || (printf "\n\n\n" && git --no-pager diff --color=always)
@pre-commit run --all-files --hook-stage manual || (printf "\n\n\n" && git --no-pager diff --color=always)
pre-commit-install: ## set up the git hook scripts
@pre-commit --version
@pre-commit install
Expand Down
27 changes: 25 additions & 2 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
# Mypy 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

[mypy-docker.*]
ignore_missing_imports = True
Expand All @@ -22,5 +39,11 @@ ignore_missing_imports = True
[mypy-pyspark.*]
ignore_missing_imports = True

[mypy-setuptools.*]
ignore_missing_imports = True

[mypy-tensorflow.*]
ignore_missing_imports = True

[mypy-urllib3.*]
ignore_missing_imports = True
10 changes: 7 additions & 3 deletions tests/base-notebook/test_packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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})"])

Expand Down
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
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


Expand All @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion tests/images_hierarchy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down

0 comments on commit 8f0a73e

Please sign in to comment.