From 14cd3488cfe3a082cd9de65a4cc77e9f4bd9f53e Mon Sep 17 00:00:00 2001 From: Jukka Lehtosalo Date: Wed, 7 Oct 2026 16:11:24 +0100 Subject: [PATCH] [mypyc] Fix undeclared static for local Final with the same name as a module Fixes mypyc/mypyc#1152. A reference to a local `Final` variable generated invalid C if a module with the same name existed (such as `types` or `os`): ```python def f(items: list[str]) -> dict[str, int]: types: Final = {i: len(i) for i in items} return dict(types) # error: 'CPyStatic_types___' undeclared ``` The name of the variable was treated as a module-level final name, since it looked like a fully qualified name with a module prefix. Now only a literal final value is used directly for local final variables, and otherwise they are read like other local variables. --- mypyc/irbuild/builder.py | 18 +++++++++++++++--- mypyc/irbuild/expression.py | 16 ++++++++++++++-- mypyc/test-data/irbuild-basic.test | 11 +++++++++++ mypyc/test-data/run-misc.test | 18 ++++++++++++++++++ 4 files changed, 58 insertions(+), 5 deletions(-) diff --git a/mypyc/irbuild/builder.py b/mypyc/irbuild/builder.py index 8c76f96c84893..389e2cec4c2b4 100644 --- a/mypyc/irbuild/builder.py +++ b/mypyc/irbuild/builder.py @@ -1381,7 +1381,15 @@ def class_final_var_of_instance(self, instance: Instance, name: str) -> Var | No return sym.node def emit_load_final( - self, final_var: Var, fullname: str, name: str, native: bool, typ: Type, line: int + self, + final_var: Var, + fullname: str, + name: str, + typ: Type, + line: int, + *, + is_native: bool, + is_local: bool, ) -> Value | None: """Emit code for loading value of a final name (if possible). @@ -1389,13 +1397,17 @@ def emit_load_final( final_var: Var corresponding to the final name fullname: its qualified name name: shorter name to show in errors - native: whether the name was defined in a compiled module typ: its type line: line number where loading occurs + is_native: whether the name was defined in a compiled module + is_local: whether the name is a local variable """ if final_var.final_value is not None: # this is safe even for non-native names return self.load_literal_value(final_var.final_value, line) - elif native and module_prefix(self.graph, fullname): + elif is_native and not is_local and module_prefix(self.graph, fullname): + # A local final variable isn't stored in a static (its fullname is just the + # bare name, which could also be the name of a module), but a literal value + # was already handled above return self.load_final_static(fullname, self.mapper.type_to_rtype(typ), line, name) else: return None diff --git a/mypyc/irbuild/expression.py b/mypyc/irbuild/expression.py index fb573353acf19..e2ae96e6f161f 100644 --- a/mypyc/irbuild/expression.py +++ b/mypyc/irbuild/expression.py @@ -204,7 +204,13 @@ def transform_name_expr(builder: IRBuilder, expr: NameExpr) -> Value: if final_type is None: final_type = AnyType(TypeOfAny.special_form) value = builder.emit_load_final( - expr.node, fullname, expr.name, builder.is_native_ref_expr(expr), final_type, expr.line + expr.node, + fullname, + expr.name, + final_type, + expr.line, + is_native=builder.is_native_ref_expr(expr), + is_local=expr.kind == LDEF, ) if value is not None: return value @@ -286,7 +292,13 @@ def transform_member_expr(builder: IRBuilder, expr: MemberExpr) -> Value: if final_type is None: final_type = AnyType(TypeOfAny.special_form) value = builder.emit_load_final( - final_var, fullname, final_var.name, native, final_type, expr.line + final_var, + fullname, + final_var.name, + final_type, + expr.line, + is_native=native, + is_local=False, ) if value is not None: return value diff --git a/mypyc/test-data/irbuild-basic.test b/mypyc/test-data/irbuild-basic.test index af1859e3a110e..c1e902887da9b 100644 --- a/mypyc/test-data/irbuild-basic.test +++ b/mypyc/test-data/irbuild-basic.test @@ -3192,6 +3192,11 @@ def inlined() -> str: def local() -> str: const: Final[str] = inlined() return const + +def same_name_as_module() -> str: + # "types" is also a module name + types: Final = inlined() + return types [out] def inlined(): r0, const, r1 :: str @@ -3206,6 +3211,12 @@ L0: r0 = inlined() const = r0 return const +def same_name_as_module(): + r0, types :: str +L0: + r0 = inlined() + types = r0 + return types [case testDirectlyCall__bool__] class A: diff --git a/mypyc/test-data/run-misc.test b/mypyc/test-data/run-misc.test index f0aacf695eddc..3467a2427eafa 100644 --- a/mypyc/test-data/run-misc.test +++ b/mypyc/test-data/run-misc.test @@ -1544,3 +1544,21 @@ def test_walrus() -> None: assert check_nested_walrus(1.5) is True assert check_nested_walrus("x") is True assert check_nested_walrus(b"x") is False + +[case testFinal] +from typing import Final + +def local_final_with_same_name_as_module(items: list[str]) -> dict[str, int]: + # "types" is also a module name + types: Final[dict[str, int]] = {i: len(i) for i in items} + return {k: v + 1 for k, v in types.items()} + +def local_final_literal() -> list[int]: + n: Final = 5 + return [n + i for i in range(2)] + +def test_local_final_with_same_name_as_module() -> None: + assert local_final_with_same_name_as_module(["a", "bb"]) == {"a": 2, "bb": 3} + +def test_local_final_literal() -> None: + assert local_final_literal() == [5, 6]