Skip to content

Commit

Permalink
Use mypy for static type checking
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jdufresne committed Jan 2, 2021
1 parent fb5486b commit 6f1e32e
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 3 deletions.
1 change: 1 addition & 0 deletions .github/workflows/qa.yml
Expand Up @@ -16,6 +16,7 @@ jobs:
matrix:
toxenv:
- checkqa
- mypy
- readme
python-version:
- "3.x"
Expand Down
2 changes: 1 addition & 1 deletion piptools/logging.py
Expand Up @@ -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
Expand Down
Empty file added piptools/py.typed
Empty file.
3 changes: 2 additions & 1 deletion piptools/scripts/compile.py
Expand Up @@ -3,6 +3,7 @@
import sys
import tempfile
import warnings
from typing import Any

from click import Command
from click.utils import safecall
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion piptools/utils.py
Expand Up @@ -43,7 +43,7 @@ def key_from_req(req):
return key


def comment(text):
def comment(text: str) -> str:
return style(text, fg="green")


Expand Down
14 changes: 14 additions & 0 deletions setup.cfg
Expand Up @@ -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
6 changes: 6 additions & 0 deletions tox.ini
Expand Up @@ -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

Expand Down Expand Up @@ -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 =
Expand Down

0 comments on commit 6f1e32e

Please sign in to comment.