Repository navigation
Conversation
BichengWang
left a comment
There was a problem hiding this comment.
Checked out a001ad4 and ran test/events (177 pass, new tests included). The __init__ change does what the description says.
One gap in the same area: __eq__ and __repr__ (a few lines below, base.py ~L310 and ~L319) still loop over every class in the MRO and read compare / repr from each one, so a parent's flag still wins over a child's redeclaration. Repro on this head:
class P(BaseEvent):
x: int = Field(default=1, compare=True)
secret: str = Field(default="s", repr=False)
class C(P):
x: int = Field(default=1, compare=False)
secret: str = Field(default="s", repr=True)
C(x=1) == C(x=2) # False, expected True
repr(C()) # C(x=1), secret still hiddenSame result on main, so this isn't a regression, but the PR is about a subclass's definition being the effective one, and these two flags are part of the definition. A small helper that returns the merged dict[str, FieldInfo] (reversed MRO, update) and is used by __init__, __eq__ and __repr__ would cover it, and a test that overrides compare/repr in a child would pin it. from_dict only needs the names, so it can stay as is.
| if default is not Ellipsis: | ||
| defaults[name] = default | ||
| seen.add(name) | ||
| fields.update(getattr(klass, "_event_fields_", {})) |
There was a problem hiding this comment.
This merged mapping is only used by __init__. __eq__ and __repr__ still union compare / repr across the MRO, so a child that redeclares a field with compare=False or repr=True is overridden by the parent's flag (repro in the review comment). Worth sharing this helper with them.
|
Thanks @BichengWang for testing the PR and providing the clear repro and suggestion! I've implemented the shared field helper in c1f1159 so that |
Why are these changes needed?
Custom event subclasses cannot reliably override inherited field definitions. A subclass declaring
state: str = "ready"still gets its parent's default, and replacement default factories and keyword-only settings are ignored. The same precedence problem affectscompareandrepr: a parent can keep comparing a field the child excludes, or hide a field the child makes visible.Collect effective field definitions in reversed MRO, preserving inherited field positions while selecting the most-derived definition. Share that collection between construction, equality and repr. Keep the existing name-only collection in
from_dict.Retain the four constructor regressions and add five flag/MRO cases covering both directions of compare/repr overrides, unchanged parent behavior, and both base orders in multiple inheritance. This also addresses the gap identified in the review of the initial PR commit.
Validation on this PR branch:
a001ad42, the five new cases produce three failures and two passing controls.git diff --checkpass.Related issue number
No public issue has been opened. The original problem was found through a local custom-event extension reproduction. The additional compare/repr coverage follows the review on this PR. These gaps also exist upstream; they were not introduced by the initial constructor fix.
Checks
AI assistance
Prepared with Codex assistance. The contributor reviewed the update, its complete diff and validation results, and approved publication.