Skip to content

[mypyc] Use shared value for Final attribute initialized in class body - #21932

Merged
JukkaL merged 5 commits into
masterfrom
mypyc-final-class-body
Sep 3, 2026
Merged

[mypyc] Use shared value for Final attribute initialized in class body#21932
JukkaL merged 5 commits into
masterfrom
mypyc-final-class-body

Conversation

@JukkaL

@JukkaL JukkaL commented Sep 3, 2026

Copy link
Copy Markdown
Collaborator

Use a module-level static for Final attributes initialized in class body:

class C:
    X: Final = [1]  # Only one list allocated

Previously such final attributes where stored in the per-instance struct, and a new object was allocated for each instance, which deviated from Python semantics and was inefficient. Now the initializer is only run once.

Final attributes that have a type with an overlapping error value like i64 are still not supported properly. This is left as follow-up work.

I used coding agent assist but reviewed the result manually and iterated on it.

@JukkaL JukkaL changed the title [mypyc] Fixes to Final attribute initialized in class body [mypyc] Use shared value for Final attribute initialized in class body Sep 3, 2026
@JukkaL
JukkaL merged commit 543dbce into master Sep 3, 2026
18 checks passed
@JukkaL
JukkaL deleted the mypyc-final-class-body branch September 3, 2026 14:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants