Skip to content

fix(events): honor subclass field overrides in BaseEvent - #3704

Open
wToTw123 wants to merge 2 commits into
ag2ai:mainfrom
wToTw123:codex/1120-event-default-overrides
Open

wToTw123 wants to merge 2 commits into
ag2ai:mainfrom
wToTw123:codex/1120-event-default-overrides

Conversation

@wToTw123

@wToTw123 wToTw123 commented Nov 21, 2026 •

Copy link
Copy Markdown

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 affects compare and repr: 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:

  • Against the previous PR head a001ad42, the five new cases produce three failures and two passing controls.
  • After the update, all 226 related tests pass: the event suite plus existing Agent composition, usage and in-memory stream tests, on Windows / CPython 3.12.16.
  • Project-pinned Ruff 0.16.10 lint/format, mypy 2.4.0 on both changed files, codespell 2.4.3, AST parsing and git diff --check pass.
  • The review's exact example now compares the child instances as equal and includes the child's visible field in repr; the parent's behavior is retained.
  • Full repository tests, the Python/OS CI matrix and the complete pre-commit suite have not run locally. No real model API calls were made.

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

  • I've included any doc changes needed for https://docs.ag2.ai/. No documentation changes are proposed for this field-override bug fix; public behavior is covered by regression tests.
  • I've added tests (if relevant) corresponding to the changes introduced in this PR.
  • I've made sure all auto checks have passed. The local checks above pass; upstream CI and the complete pre-commit suite remain pending.

AI assistance

Prepared with Codex assistance. The contributor reviewed the update, its complete diff and validation results, and approved publication.

  • I understand the changes in this PR and can explain them in my own words.
  • I have verified that the PR description accurately reflects the actual diff.
  • If AI assistance was used, I reviewed, tested, and validated the generated code/text before submitting.

@CLAassistant

CLAassistant commented Nov 21, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@github-actions github-actions Bot added the area:core Agent runtime core: ag2/*.py, ag2/events, ag2/response, ag2/streams label Nov 21, 2026

@BichengWang BichengWang left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 hidden

Same 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.

Comment thread ag2/events/base.py Outdated
if default is not Ellipsis:
defaults[name] = default
seen.add(name)
fields.update(getattr(klass, "_event_fields_", {}))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@wToTw123

Copy link
Copy Markdown
Author

Thanks @BichengWang for testing the PR and providing the clear repro and suggestion!

I've implemented the shared field helper in c1f1159 so that __init__, __eq__, and __repr__ use consistent field definitions. I also added regression tests for compare/repr overrides and multiple inheritance. All 226 related tests pass locally.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:core Agent runtime core: ag2/*.py, ag2/events, ag2/response, ag2/streams

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants