From c22767359464ab43cc1c62288778b7c8d4570934 Mon Sep 17 00:00:00 2001 From: Jon Dufresne Date: Tue, 15 Dec 2020 07:20:10 -0800 Subject: [PATCH 1/2] Use mypy for static type checking Type checking helps give confidence that APIs are being used correctly, especially during large refactoring (e.g. eventually dropping Python 2 support). This initial pass only includes type annotations that were necessary to make mypy pass. Future pull requests will add types for the rest of the project once the workflow is established. pip has started including type annotation in a subset of its code. When major changes to pip are released, the type checking will help identify API mismatches and areas to adjust. mypy is configured to be as strict as possible without introducing errors. The configuration is a subset of the --strict CLI argument. Refs #972 --- .github/workflows/qa.yml | 1 + piptools/logging.py | 2 +- piptools/py.typed | 0 piptools/scripts/compile.py | 3 ++- piptools/utils.py | 2 +- setup.cfg | 14 ++++++++++++++ tox.ini | 6 ++++++ 7 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 piptools/py.typed diff --git a/.github/workflows/qa.yml b/.github/workflows/qa.yml index aa4343315..571ead8eb 100644 --- a/.github/workflows/qa.yml +++ b/.github/workflows/qa.yml @@ -16,6 +16,7 @@ jobs: matrix: toxenv: - checkqa + - mypy - readme python-version: - "3.x" diff --git a/piptools/logging.py b/piptools/logging.py index 87c552667..cc9828c24 100644 --- a/piptools/logging.py +++ b/piptools/logging.py @@ -12,7 +12,7 @@ class LogContext: stream = sys.stderr - def __init__(self, verbosity=0, indent_width=2): + def __init__(self, verbosity: int = 0, indent_width: int = 2): self.verbosity = verbosity self.current_indent = 0 self._indent_width = indent_width diff --git a/piptools/py.typed b/piptools/py.typed new file mode 100644 index 000000000..e69de29bb diff --git a/piptools/scripts/compile.py b/piptools/scripts/compile.py index f9e26ea0a..c861838bb 100755 --- a/piptools/scripts/compile.py +++ b/piptools/scripts/compile.py @@ -3,6 +3,7 @@ import sys import tempfile import warnings +from typing import Any from click import Command from click.utils import safecall @@ -25,7 +26,7 @@ DEFAULT_REQUIREMENTS_OUTPUT_FILE = "requirements.txt" -def _get_default_option(option_name): +def _get_default_option(option_name: str) -> Any: """ Get default value of the pip's option (including option from pip.conf) by a given option name. diff --git a/piptools/utils.py b/piptools/utils.py index 1b2560b70..e6f9cef47 100644 --- a/piptools/utils.py +++ b/piptools/utils.py @@ -43,7 +43,7 @@ def key_from_req(req): return key -def comment(text): +def comment(text: str) -> str: return style(text, fg="green") diff --git a/setup.cfg b/setup.cfg index 710868286..5f30716e6 100644 --- a/setup.cfg +++ b/setup.cfg @@ -69,3 +69,17 @@ pytest-parametrize-values-row-type = tuple [isort] profile = black + +[mypy] +disallow_any_generics = true +disallow_incomplete_defs = true +disallow_subclassing_any = true +disallow_untyped_calls = true +disallow_untyped_decorators = true +ignore_missing_imports = true +no_implicit_optional = true +strict_equality = true +warn_redundant_casts = true +warn_return_any = true +warn_unused_configs = true +warn_unused_ignores = true diff --git a/tox.ini b/tox.ini index d3f9d736c..893d43e7c 100644 --- a/tox.ini +++ b/tox.ini @@ -3,6 +3,7 @@ envlist = # NOTE: keep this in sync with the env list in .github/workflows/ci.yml. py{36,37,38,39,py,py3}-pip{20.1,20.2,20.3,previous,latest,master}-coverage checkqa + mypy readme skip_missing_interpreters = True @@ -33,6 +34,11 @@ deps = pre-commit commands_pre = commands = pre-commit run --all-files --show-diff-on-failure +[testenv:mypy] +deps = mypy +commands_pre = +commands = mypy piptools + [testenv:readme] deps = twine commands_pre = From 6ed320e4636f9ffc2be443126d151a79636e8f3d Mon Sep 17 00:00:00 2001 From: Jon Dufresne Date: Fri, 1 Jan 2021 12:32:04 -0800 Subject: [PATCH 2/2] Move mypy check to a pre-commit hook --- .github/workflows/qa.yml | 1 - .pre-commit-config.yaml | 7 +++++++ tox.ini | 6 ------ 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/.github/workflows/qa.yml b/.github/workflows/qa.yml index 571ead8eb..aa4343315 100644 --- a/.github/workflows/qa.yml +++ b/.github/workflows/qa.yml @@ -16,7 +16,6 @@ jobs: matrix: toxenv: - checkqa - - mypy - readme python-version: - "3.x" diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 552d29cea..1af47cfe8 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -17,6 +17,13 @@ repos: language_version: python3 additional_dependencies: - flake8-pytest-style + - repo: https://github.com/pre-commit/mirrors-mypy + rev: v0.790 + hooks: + - id: mypy + # Avoid error: Duplicate module named 'setup' + # https://github.com/python/mypy/issues/4008 + exclude: ^tests/test_data/ - repo: https://github.com/PyCQA/bandit rev: 1.7.0 hooks: diff --git a/tox.ini b/tox.ini index 893d43e7c..d3f9d736c 100644 --- a/tox.ini +++ b/tox.ini @@ -3,7 +3,6 @@ envlist = # NOTE: keep this in sync with the env list in .github/workflows/ci.yml. py{36,37,38,39,py,py3}-pip{20.1,20.2,20.3,previous,latest,master}-coverage checkqa - mypy readme skip_missing_interpreters = True @@ -34,11 +33,6 @@ deps = pre-commit commands_pre = commands = pre-commit run --all-files --show-diff-on-failure -[testenv:mypy] -deps = mypy -commands_pre = -commands = mypy piptools - [testenv:readme] deps = twine commands_pre =