From 196b2a32be9080b025146d3029fb95a2e579492f Mon Sep 17 00:00:00 2001 From: Jukka Lehtosalo Date: Wed, 15 May 2024 16:44:17 +0100 Subject: [PATCH] Fix crash on undefined name in generic method --- mypy/semanal.py | 12 +++++++++--- test-data/unit/check-python312.test | 10 ++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/mypy/semanal.py b/mypy/semanal.py index a63e58ba54f2..f92471c159de 100644 --- a/mypy/semanal.py +++ b/mypy/semanal.py @@ -1653,7 +1653,7 @@ def push_type_args( tvs.append((p.name, tv)) for name, tv in tvs: - self.add_symbol(name, tv, context) + self.add_symbol(name, tv, context, no_progress=True) return tvs @@ -5967,6 +5967,7 @@ def lookup( for table in reversed(self.locals): if table is not None and name in table: return table[name] + # 4. Current file global scope if name in self.globals: return self.globals[name] @@ -6279,6 +6280,7 @@ def add_symbol( module_hidden: bool = False, can_defer: bool = True, escape_comprehensions: bool = False, + no_progress: bool = False, ) -> bool: """Add symbol to the currently active symbol table. @@ -6300,7 +6302,9 @@ def add_symbol( symbol = SymbolTableNode( kind, node, module_public=module_public, module_hidden=module_hidden ) - return self.add_symbol_table_node(name, symbol, context, can_defer, escape_comprehensions) + return self.add_symbol_table_node( + name, symbol, context, can_defer, escape_comprehensions, no_progress + ) def add_symbol_skip_local(self, name: str, node: SymbolNode) -> None: """Same as above, but skipping the local namespace. @@ -6331,6 +6335,7 @@ def add_symbol_table_node( context: Context | None = None, can_defer: bool = True, escape_comprehensions: bool = False, + no_progress: bool = False, ) -> bool: """Add symbol table node to the currently active symbol table. @@ -6379,7 +6384,8 @@ def add_symbol_table_node( self.name_already_defined(name, context, existing) elif name not in self.missing_names[-1] and "*" not in self.missing_names[-1]: names[name] = symbol - self.progress = True + if not no_progress: + self.progress = True return True return False diff --git a/test-data/unit/check-python312.test b/test-data/unit/check-python312.test index 2b85020e6609..53656ae5e3fb 100644 --- a/test-data/unit/check-python312.test +++ b/test-data/unit/check-python312.test @@ -948,3 +948,13 @@ type B[T] = Foo[T] [out2] tmp/a.py:3: note: Revealed type is "builtins.str" tmp/a.py:5: note: Revealed type is "b.Foo[builtins.int]" + +[case testPEP695UndefinedNameInGenericFunction] +# mypy: enable-incomplete-feature=NewGenericSyntax + +def f[T](x: T) -> T: + return unknown() # E: Name "unknown" is not defined + +class C: + def m[T](self, x: T) -> T: + return unknown() # E: Name "unknown" is not defined