Skip to content

Commit

Permalink
Fix crash on undefined name in generic method
Browse files Browse the repository at this point in the history
  • Loading branch information
JukkaL committed May 15, 2024
1 parent 4819d60 commit 196b2a3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
12 changes: 9 additions & 3 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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

Expand Down
10 changes: 10 additions & 0 deletions test-data/unit/check-python312.test
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 196b2a3

Please sign in to comment.