Repository navigation
Conversation
Contributor
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
jay23606
marked this pull request as ready for review
November 7, 2026 01:48
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug
mypy crashes with
INTERNAL ERROR(RecursionError) when a class is checked against a contravariant generic whose base refers back to the class:Checking
C <: N[C]needsC <: N[C]again for the contravariant argument, so the recursion never ends. Covariant and invariant parameters do not loop here, which is why only contravariance crashes.Change
While checking a contravariant type argument in
SubtypeVisitor.visit_instance, push the(left, right)pair onto the existing subtype assumption stack. A nested check of the same pair then hits the existingis_assumed_subtypecheck at the top ofis_subtypeand returnsTrue(coinductive assumption). The assumption is popped on exit, so a failed check still reports the error.Only contravariant arguments push the pair, so other variances keep their current behaviour.
Testing
testContravariantSelfReferentialBaseDoesNotCrashtotest-data/unit/check-generics.test, covering the issue'sCexample and the generic variantD[X]. With the fix it passes (PYTHONPATH=.venv/Lib/site-packages python -m pytest -o addopts="" -q -k testContravariantSelfReferentialBaseDoesNotCrash mypy/test/testcheck.py). I have not confirmed that it fails on the unfixed code: the run on the base commit stopped at collection withModuleNotFoundError: No module named 'librt'before reaching the test.PYTHONPATH=.venv/Lib/site-packages python -m pytest -o addopts="" -q -k "Variance or Generic or Protocol or Subtype or Callable or Tuple or TypeVar or Recursive or Alias or Overload or Typeddict or Namedtuple" mypy/test/testcheck.py: 3161 passed, 0 failed.Not run here: the full
runtests.pysuite and the self-check. This machine's.venvhas no pytest, so the commands above use the global Python with the venv's site-packages onPYTHONPATH. CI will run the full suite.Regression test: untested here: CI verifies this change
Fixes #22360
Disclosure: this change was prepared with AI assistance.