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
20 changes: 14 additions & 6 deletions mypyc/irbuild/classdef.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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(
Expand All @@ -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:
Expand Down Expand Up @@ -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()))
Expand Down
21 changes: 21 additions & 0 deletions mypyc/test-data/irbuild-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -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]):
Expand Down
33 changes: 33 additions & 0 deletions mypyc/test-data/run-multimodule.test
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading