Skip to content

Fix mypy type checking via creating a nox typecheck session #13476

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 21 commits into
base: main
Choose a base branch
from

Conversation

notatallshaw
Copy link
Member

@notatallshaw notatallshaw commented Jul 11, 2025

This moves type checking out of the pre-commit and into a dedicated nox -s typecheck, this was inspired by and succeeds #12866.

On top of the reasons outlined there there are two big issues with pip's type checking:

1. The additional dependencies in the .pre-commit.yaml are rarely updated and currently obsolete

This PR fixes that by making the nox -s typecheck a new type-check dependency groups, which inherits from the existing test dependency group, a new docs dependency group, and a couple of other dependency groups that define various parts of pip.

Taking the changes from #12866 and apply this increased the number of mypy errors from 0 to 48.

2. Vendored dependencies that use type stubs are not currently type checked

Pip vendors libraries like requests which do not include type hints, normally this is fixed by installing a stubs package, types-requests at type hint time, however this was not being picked up by mypy because it does not recognized pip._vendor.requests as requests.

This can be fixed by doing all the imports of vendored libraries that don't include type hints like this (fortunately there are only a few libraries that need this):

if TYPE_CHECKING:
    import requests
else:
    from pip._vendor import requests

Doing this increased the number of mypy errors from 48 to 71.

The rest of the PR is simply about fixing all the type check errors, I tried to be very conservative, using # type: ignore and cast rather than making complicated refactors, but there are definitely a few bugs that were fixed in this as well.

A side benefit is pre-commit runs much much faster, allowing you to enable it locally and it not be painful.

@notatallshaw notatallshaw marked this pull request as draft July 11, 2025 05:47
@notatallshaw
Copy link
Member Author

Ah, there's a number of issues to work through, it seems I broke a few tests, and vendoring uses an old version of toml that can not correctly parse mixed type tables. I'll have to return to this next week.

@notatallshaw notatallshaw changed the title Fix mypy type checking via nox typecheck session Fix mypy type checking via creating a nox typecheck session Jul 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants