Skip to content

chore(python): handle duplicate field names#3384

Merged
chaokunyang merged 10 commits intoapache:mainfrom
eyad-hazem-elmorsy:chore/handle-duplicate-field-names
Feb 28, 2026
Merged

chore(python): handle duplicate field names#3384
chaokunyang merged 10 commits intoapache:mainfrom
eyad-hazem-elmorsy:chore/handle-duplicate-field-names

Conversation

@eyad-hazem-elmorsy
Copy link
Contributor

Why?

This PR handles the duplicate of field names for inheritance.

What does this PR do?

The handling is done by making every child class's field override its parent's till the most-derived field kept.

  • First, we iterate over the full class MRO (cls.mro) to extract field metadata from all parent classes.

  • Then, deduplicate fields so that each field name appears only once in the resulting list with keeping the child class definition if a field is shadowed.

  • Finally, added some tests to cover:

    • Single-level inheritance
    • Multi-level inheritance
    • Field shadowing in child and grandchild classes
    • Deduplication of overridden fields
  • Does this PR introduce any public API change?

  • Does this PR introduce any binary protocol compatibility change?

@chaokunyang
Copy link
Collaborator

It seems that dataclass don't allow sublass has duplicate fields with parent class. Could you check whether this is true? If so, the issue can be simplified a lot

@eyad-hazem-elmorsy
Copy link
Contributor Author

eyad-hazem-elmorsy commented Feb 22, 2026

I found that dataclass allows a subclass to have duplicate fields with a parent class, but when you define a field with the same name in the subclass, it overrides the parent's field

@eyad-hazem-elmorsy
Copy link
Contributor Author

Does it mean that I needed to only remove the comment of TODO?

@chaokunyang
Copy link
Collaborator

Just remove TODO, your code should make it work now

@eyad-hazem-elmorsy eyad-hazem-elmorsy force-pushed the chore/handle-duplicate-field-names branch from c725da7 to cb32dd2 Compare February 26, 2026 15:05
@eyad-hazem-elmorsy
Copy link
Contributor Author

You can check now @chaokunyang

@chaokunyang
Copy link
Collaborator

@eyad-hazem-elmorsy Could you add some duplciate fields tests back? THe tests can ensure we don't break it in future

@chaokunyang chaokunyang changed the title chore(python): handle duplicate field names (TODO for typedef encoding) chore(python): handle duplicate field names Feb 27, 2026
@eyad-hazem-elmorsy
Copy link
Contributor Author

Returned back and the tests run successfully

assert field_map["name"].defined_class == "Child", f"Expected field to be defined in Child but got {field_map['name'].defined_class}"


if __name__ == "__main__":
Copy link
Collaborator

@chaokunyang chaokunyang Feb 28, 2026

Choose a reason for hiding this comment

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

I think we also need e2e serialization tests in each case instead of only typedef tests

assert decoded_field_names.count("name") == 1


def test_multilevel_inheritance():
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not sure whether such tests are meaningful. The shadow ones shuld already cover it.More code means more maintainance overhead.

Please refactor all tests to to make it consise and remove redundancy

fory.register(Parent, namespace="test", typename="Parent")
fory.register(ChildWithShadow, namespace="test", typename="ChildWithShadow")

obj = ChildWithShadow(name="shadowed", value=10, extra=3.14)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you add TypeDef encoding check that only threefields for ChildWithShadow?

Copy link
Collaborator

@chaokunyang chaokunyang left a comment

Choose a reason for hiding this comment

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

LGTM

@eyad-hazem-elmorsy
Copy link
Contributor Author

Thanks for your time and these good points. I explored many pieces of code in this PR and learned a lot

@chaokunyang chaokunyang merged commit c5bb74f into apache:main Feb 28, 2026
64 checks passed
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