From 2a8c91e6413f07f800833d6e3409070b91d725b4 Mon Sep 17 00:00:00 2001 From: Jukka Lehtosalo Date: Thu, 19 Sep 2024 17:44:29 +0100 Subject: [PATCH] [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 --- mypy/semanal.py | 1 + test-data/unit/check-python312.test | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/mypy/semanal.py b/mypy/semanal.py index 27d2a3abf93f..780d0b614ae3 100644 --- a/mypy/semanal.py +++ b/mypy/semanal.py @@ -6955,6 +6955,7 @@ def name_not_defined(self, name: str, ctx: Context, namespace: str | None = None namespace is None and self.type and not self.is_func_scope() + and self.incomplete_type_stack and self.incomplete_type_stack[-1] and not self.final_iteration ): diff --git a/test-data/unit/check-python312.test b/test-data/unit/check-python312.test index d0a39f7e56a6..bf1115dc51c5 100644 --- a/test-data/unit/check-python312.test +++ b/test-data/unit/check-python312.test @@ -1320,6 +1320,14 @@ class P[T](Protocol[T]): # E: No arguments expected for "Protocol" base class class P2[T](Protocol[S]): # E: No arguments expected for "Protocol" base class pass +[case testPEP695CannotUseTypeVarFromOuterClass] +# mypy: enable-incomplete-feature=NewGenericSyntax +class ClassG[V]: + # This used to crash + class ClassD[T: dict[str, V]]: # E: Name "V" is not defined + ... +[builtins fixtures/dict.pyi] + [case testPEP695MixNewAndOldStyleGenerics] # mypy: enable-incomplete-feature=NewGenericSyntax from typing import TypeVar