Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions mypyc/irbuild/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -1381,21 +1381,33 @@ 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).

Args:
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
Expand Down
16 changes: 14 additions & 2 deletions mypyc/irbuild/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions mypyc/test-data/irbuild-basic.test
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down
18 changes: 18 additions & 0 deletions mypyc/test-data/run-misc.test
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Loading