Skip to content

Commit 2a8c91e

Browse files
authored
[PEP 695] Fix crash on invalid type var reference (#17788)
Test case from typing conformance test suite: https://github.com/python/typing/blob/main/conformance/tests/generics_syntax_declarations.py#L45
1 parent 4554bd0 commit 2a8c91e

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

mypy/semanal.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6955,6 +6955,7 @@ def name_not_defined(self, name: str, ctx: Context, namespace: str | None = None
69556955
namespace is None
69566956
and self.type
69576957
and not self.is_func_scope()
6958+
and self.incomplete_type_stack
69586959
and self.incomplete_type_stack[-1]
69596960
and not self.final_iteration
69606961
):

test-data/unit/check-python312.test

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1320,6 +1320,14 @@ class P[T](Protocol[T]): # E: No arguments expected for "Protocol" base class
13201320
class P2[T](Protocol[S]): # E: No arguments expected for "Protocol" base class
13211321
pass
13221322

1323+
[case testPEP695CannotUseTypeVarFromOuterClass]
1324+
# mypy: enable-incomplete-feature=NewGenericSyntax
1325+
class ClassG[V]:
1326+
# This used to crash
1327+
class ClassD[T: dict[str, V]]: # E: Name "V" is not defined
1328+
...
1329+
[builtins fixtures/dict.pyi]
1330+
13231331
[case testPEP695MixNewAndOldStyleGenerics]
13241332
# mypy: enable-incomplete-feature=NewGenericSyntax
13251333
from typing import TypeVar

0 commit comments

Comments
 (0)