Skip to content

Commit ad39686

Browse files
committed
[mypyc] Fix is_native_ref_expr for class attrs.
1 parent 3420ef1 commit ad39686

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

mypyc/irbuild/builder.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -979,17 +979,13 @@ def _analyze_iterable_item_type(self, expr: Expression) -> Type:
979979

980980
def is_native_module(self, module: str) -> bool:
981981
"""Is the given module one compiled by mypyc?"""
982-
return module in self.mapper.group_map
982+
return self.mapper.is_native_module(module)
983983

984984
def is_native_ref_expr(self, expr: RefExpr) -> bool:
985-
if expr.node is None:
986-
return False
987-
if "." in expr.node.fullname:
988-
return self.is_native_module(expr.node.fullname.rpartition(".")[0])
989-
return True
985+
return self.mapper.is_native_ref_expr(expr)
990986

991987
def is_native_module_ref_expr(self, expr: RefExpr) -> bool:
992-
return self.is_native_ref_expr(expr) and expr.kind == GDEF
988+
return self.mapper.is_native_module_ref_expr(expr)
993989

994990
def is_synthetic_type(self, typ: TypeInfo) -> bool:
995991
"""Is a type something other than just a class we've created?"""

mypyc/irbuild/mapper.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,12 @@ def is_native_ref_expr(self, expr: RefExpr) -> bool:
217217
if expr.node is None:
218218
return False
219219
if "." in expr.node.fullname:
220-
return self.is_native_module(expr.node.fullname.rpartition(".")[0])
220+
cur = expr.node.fullname
221+
while "." in cur:
222+
cur = cur.rpartition(".")[0]
223+
if self.is_native_module(cur):
224+
return True
225+
return False
221226
return True
222227

223228
def is_native_module_ref_expr(self, expr: RefExpr) -> bool:

mypyc/test-data/run-classes.test

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2594,3 +2594,15 @@ def test_class_final_attribute_inherited() -> None:
25942594
assert C().b == 2
25952595
assert B().c == 3
25962596
assert C().c == 3
2597+
2598+
[case testClassWithFinalAttributeAccess]
2599+
from typing import Final
2600+
2601+
class C:
2602+
a: Final = {'x': 'y'}
2603+
b: Final = C.a
2604+
2605+
def test_final_attribute() -> None:
2606+
assert C.a['x'] == 'y'
2607+
assert C.b['x'] == 'y'
2608+
assert C.a is C.b

0 commit comments

Comments
 (0)