diff --git a/mypyc/irbuild/classdef.py b/mypyc/irbuild/classdef.py index cd3d07a5d3aa..44defafb2e8a 100644 --- a/mypyc/irbuild/classdef.py +++ b/mypyc/irbuild/classdef.py @@ -862,7 +862,7 @@ def find_attr_initializers( return set(), [] attrs_with_defaults: set[str] = set() - default_assignments: list[tuple[AssignmentStmt, str]] = [] + default_assignments: dict[str, tuple[AssignmentStmt, str]] = {} # TODO: Support nested statements if builder.options.separate: @@ -882,9 +882,11 @@ def find_attr_initializers( if name is None: continue attrs_with_defaults.add(name) - default_assignments.append((stmt, info.module_name)) + # Keep only the last default per name: initializer stores don't free an earlier value. + default_assignments.pop(name, None) + default_assignments[name] = (stmt, info.module_name) - return attrs_with_defaults, default_assignments + return attrs_with_defaults, list(default_assignments.values()) def generate_attr_defaults_init( @@ -901,8 +903,8 @@ def generate_attr_defaults_init( in filename order, so this class may be IR-built before its base, and a method_decls lookup that depended on the base having been IR-built first would miss. Without separate compilation, find_attr_initializers has - already collected the full MRO's defaults into default_assignments, so - we inline them all as before. + already collected the full MRO's defaults into default_assignments, one + per attribute name, so we inline them all as before. """ cls = builder.mapper.type_to_ir[cdef.info] if cls.builtin_base: @@ -951,7 +953,13 @@ def generate_attr_defaults_init( finally: builder.globals_lookup_module = None init = SetAttr(self_var, lvalue.name, val, stmt.rvalue.line) - init.mark_as_initializer() + # The parent's setup may have stored a default for an inherited attribute + # already, so use a regular store, which releases it. Separate builds don't + # treat any attribute as always defined, so the store checks for NULL first. + if parent_with_defaults is None or not any( + lvalue.name in base.attributes for base in cls.mro[1:] + ): + init.mark_as_initializer() builder.add(init) builder.add(Return(builder.true())) diff --git a/mypyc/test-data/irbuild-classes.test b/mypyc/test-data/irbuild-classes.test index b4424cfa9e88..a62d54ef0653 100644 --- a/mypyc/test-data/irbuild-classes.test +++ b/mypyc/test-data/irbuild-classes.test @@ -1529,6 +1529,27 @@ L0: __mypyc_self__.b = 1 return 1 +[case testDefaultVarsOverriddenInSubclass] +class A: + x = 'a' +class B(A): + x = 'b' +[out] +def A.__mypyc_defaults_setup(__mypyc_self__): + __mypyc_self__ :: __main__.A + r0 :: str +L0: + r0 = 'a' + __mypyc_self__.x = r0 + return 1 +def B.__mypyc_defaults_setup(__mypyc_self__): + __mypyc_self__ :: __main__.B + r0 :: str +L0: + r0 = 'b' + __mypyc_self__.x = r0 + return 1 + [case testSubclassDictSpecalized] from typing import Dict class WelpDict(Dict[str, int]): diff --git a/mypyc/test-data/run-multimodule.test b/mypyc/test-data/run-multimodule.test index 3c8b4ceb9995..3c4e94e9e788 100644 --- a/mypyc/test-data/run-multimodule.test +++ b/mypyc/test-data/run-multimodule.test @@ -2001,6 +2001,39 @@ class Parent: from native import test test() +[case testCrossModuleOverriddenAttrDefaultIsReleased] +import sys +from other import BASE_X, Parent + +class Child(Parent): + x: object = None + y: object = None + +def test() -> None: + getrefcount = getattr(sys, "getrefcount") + c = Child() + assert c.x is None + assert c.y is None + assert Parent().x is BASE_X + rc = getrefcount(BASE_X) + for _ in range(10): + Child() + assert getrefcount(BASE_X) == rc + +[file other.py] +from typing import Final, List + +BASE_X: Final[List[int]] = [1] + +class Parent: + x: object = BASE_X + y: object + z: int = 1 + +[file driver.py] +from native import test +test() + [case testIncrementalCrossModuleInheritedAttrDefaults] # Regression: under separate=True, when only the subclass module is # recompiled (parent loaded from mypy's incremental cache, so its