pytest includes type annotations and is PEP-561 compliant. This helps
ensure correct pytest API usage.
This caught a misuse of pytest.mark.skipif which did not include a
boolean argument. The test is now marked "network" and no longer
skipped.
The tests are a large consumer of the pip API (both the internal API and
otherwise). By running mypy on tests, we help to:
1. Ensure the internal API is consistent with regards to typing
2. Ensure the tests are representative of real life scenarios as the API
are used correctly.
3. Helps to recognize unnecessary tests that simply pass junk data to
functions which can be caught by the type checker.
4. Make sure test support code in tests/lib/ is correct and consistent.
This is especially important when refactoring such code. For example, if
we were to replace tests/lib/path.py with pathlib.
As a first start, untyped defs are allowed. All existing typing issues
have been resolved. Overtime, we can chip away at untyped defs and
eventually remove the configuration option for stricter type checking of
tests.
The following changes were made to help make mypy pass:
Remove unused record_callback argument from make_wheel() in tests.
Unused since its introduction in
6d8a58f7e1.
Replace toml with tomli_w in tests/functional/test_pep517.py. Unlike the
toml package, tomli_w contains inline typing annotations.
Remove unnecessary make_no_network_finder(). Unnecessary since
bab1e4f8a1 where the _get_pages method was
removed.
This notably gets us on a version of mypy that uses modular typeshed:
https://mypy-lang.blogspot.com/2021/05/the-upcoming-switch-to-modular-typeshed.html
which allows us to have finer control over what version of stubs we pull
in from typeshed. Also contains other routine improvements to mypy.
Required a few minor typing changes.
Add flag --show-error-code so errors look like this
src/pip/_internal/network/auth.py:70: error: Incompatible types in assignment
(expression has type "None", variable has type Module) [assignment]
rather than
src/pip/_internal/network/auth.py:70: error: Incompatible types in assignment
(expression has type "None", variable has type Module)
In autocompletion.py, the should_list_installed boolean expression was
flipped to workaround upstream black bug:
https://github.com/psf/black/issues/1629
The bug makes black fail to stabilize formatting when the list isn't the
last piece of the expression.
Allows removing a "mypy: disallow-untyped-defs=False" comment.
To workaround a mypy bug, map(os.path.basename, distribution_files) was
changed to use a generator expression. See:
https://github.com/python/mypy/issues/9864
To verify correct usage of the nox API, it is now a dependency during
pre-commit mypy runs.
The mypy configuration "follow_imports = silent" allowed erroneous code
to pass, so it has been removed. Now, all imports must be available
during type.
HashError.order is now annotated as an int to allow
HashErrors.errors.sort(key=lambda e: e.order). Alternatively we can
define a function which assert e is not None but I prefer the more
concise version, since we never raise HashError directly anyway.