Asked 3 months ago by LunarMariner357
Why is FormMixin.initial a mutable class attribute and what purpose does it serve?
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
Asked 3 months ago by LunarMariner357
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
I raised a ticket about the potential pitfalls of having a mutable class attribute for FormMixin.initial
and hoped that some older contributors could explain the reasoning behind it. Unfortunately, the ticket was closed as invalid, so I'm not expecting much feedback from that channel.
I spent a couple of days tracking down a bug that occurred when a junior developer accidentally mutated this attribute. To summarize, the issue is that FormMixin.initial
is declared as a mutable class attribute, as shown below:
PYTHONclass FormMixin: initial = {}
I'm not here to rehash why mutable defaults are problematic, but I did notice that even if you initialize it in __init__()
, the test suite still passes.
My questions are:
initial
initialized inside __init__()
to begin with? (Claude seems to have been involved in the original discussion on this.)initial
or override get_initial()
, what benefit does this initialization provide? After all, the typical pattern for an attribute overridden by a method would be to set it to None
.I'm not upset if this behavior remains unchanged, but I want to raise awareness of this potentially dangerous anti-pattern in the codebase to help prevent similar issues for unsuspecting junior developers.
Comments 0
•Answers 0
•Views 58
No comments yet.