Fix crash on self-referential Generic base - #22046
Draft
Dextheking1 wants to merge 1 commit into
Draft
Dextheking1 wants to merge 1 commit into
Dextheking1 wants to merge 1 commit into
Conversation
class C(Generic[C]) hit "AssertionError: Must not defer during final iteration" because the placeholder for C never resolves. On the final iteration, report an error instead of deferring, matching how other placeholder sites behave. Adds a regression test.
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.
Fixes #21549.
class C(Generic[C])crashed with "AssertionError: Must not defer during final iteration". The placeholder forCcan never resolve (the class is still being defined), so the deferral inanalyze_unbound_tvar_impllooped until the final iteration and hit the assert.Changed
analyze_unbound_tvar_implinmypy/semanal.pyto not defer on the final iteration when the looked-up symbol is still a placeholder. It falls through and the caller reports "Free type variable expected in Generic[...]" plus "Cannot resolve name" instead of crashing. This matches the existing convention thatdefer()must never run on the final iteration.Verified:
class C(Generic[C])andclass E(Generic["E"])no longer crash, both now report errors.class D(Generic[T])withTdefined later) still resolve, no new errors.testSelfReferenceInGenericBaseintest-data/unit/semanal-errors.test.mypy/test/testsemanal.py: 578 passed.mypy/test/testcheck.py(generics, classes, protocols, typevar-tuple suites): 1159 passed, 3 skipped, 1 xfailed.mypy/semanal.py: clean.