The modern virtual environment structure does not allow us to enable
"fake user site" while disabling the global site, so we need to do more
fine-grained configuration to correctly set up test environments for
each test case.
With this done, we can also properly support the stdlib venv ad the test
environment backend, since it basically works identically with modern
virtualenv. The incompatible_with_test_venv is thus removed.
Newer versions of Mypy seems to prefer packages in the site-packages
directory over those found in the project directory. This causes issues
if Mypy is installed in an environment that has an older pip version
present, e.g. pre-commit. Adding our pip into MYPY_PATH seems to fix
the issue.
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.
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.
The stdlib module has been available since Python 3.5 and the
TYPE_CHECKING constant has been available since 3.5.2.
By using stdlib, this removes the need for pip to maintain its own
Python 2 typing compatibility shim.
Since Python 3.3, the following classes have merged into OSError. They
remain as aliases for backward compatibility.
- EnvironmentError
- IOError
- WindowsError
https://docs.python.org/3/library/exceptions.html#OSError
Python 3 also has subclasses of OSError to help identify more specific
errors. For example, FileNotFoundError. This allows simplifying some
except blocks.
Since dropping Python 2 support, the wheel is no longer universal. See
the wheel docs:
https://wheel.readthedocs.io/en/stable/user_guide.html#building-wheels
> If your project … is expected to work on both Python 2 and 3, you will
> want to tell wheel to produce universal wheels by adding this to your
> setup.cfg file:
This handles:
* Sub-processes within unit tests (thanks pytest-cov)
* Our pytest-fixture-based virtual environments and subprocesses
therein
* Running with xdist (e.g. `-n auto`)
* Combining results from all of the above using paths rooted with
`src/pip/*`
This doesn't handle:
* Platform-specific branches
* Python 2
* CI integration