You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@fory.apache.org by "eyad-hazem-elmorsy (via GitHub)" <gi...@apache.org> on 2026/02/22 02:32:13 UTC

[PR] chore(python): handle duplicate field names (TODO for typedef encoding) [fory]

eyad-hazem-elmorsy opened a new pull request, #3384:
URL: https://github.com/apache/fory/pull/3384

   ## 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?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@fory.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fory.apache.org
For additional commands, e-mail: commits-help@fory.apache.org


Re: [PR] chore(python): handle duplicate field names [fory]

Posted by "chaokunyang (via GitHub)" <gi...@apache.org>.
chaokunyang commented on code in PR #3384:
URL: https://github.com/apache/fory/pull/3384#discussion_r2867250647


##########
python/pyfory/tests/test_duplicate_field_names.py:
##########
@@ -0,0 +1,277 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+"""
+Tests for inheritance in TypeDef implementation.
+"""
+
+from dataclasses import dataclass
+from pyfory.buffer import Buffer
+from pyfory.meta.typedef_encoder import encode_typedef
+from pyfory.meta.typedef_decoder import decode_typedef
+from pyfory import Fory
+
+
+@dataclass
+class ParentClass:
+    """Parent class with some fields."""
+
+    name: str
+    value: int
+
+
+@dataclass
+class ChildClass(ParentClass):
+    """Child class that inherits from ParentClass."""
+
+    description: str
+    count: int
+
+
+@dataclass
+class ChildWithShadowedField(ParentClass):
+    """Child class that shadows a parent field name."""
+
+    name: str  # Shadows ParentClass.name
+    extra: float
+
+
+@dataclass
+class GrandchildClass(ChildClass):
+    """Grandchild class for multi-level inheritance."""
+
+    level: int
+
+
+@dataclass
+class GrandchildWithShadowedFields(ChildClass):
+    """Grandchild that shadows fields from multiple levels."""
+
+    name: str  # Shadows ParentClass.name
+    value: int  # Shadows ParentClass.value
+    description: str  # Shadows ChildClass.description
+
+
+def test_inheritance_basic():
+    """Test TypeDef encoding/decoding with basic inheritance."""
+    fory = Fory(xlang=True)
+    fory.register(ParentClass, namespace="example", typename="ParentClass")
+    fory.register(ChildClass, namespace="example", typename="ChildClass")
+
+    resolver = fory.type_resolver
+
+    # Test parent class
+    parent_typedef = encode_typedef(resolver, ParentClass)
+    assert len(parent_typedef.fields) == 2
+    field_names = [f.name for f in parent_typedef.fields]
+    assert "name" in field_names
+    assert "value" in field_names
+
+    # Test child class - should have all fields from parent plus its own
+    child_typedef = encode_typedef(resolver, ChildClass)
+    assert len(child_typedef.fields) == 4
+    field_names = [f.name for f in child_typedef.fields]
+    assert "name" in field_names
+    assert "value" in field_names
+    assert "description" in field_names
+    assert "count" in field_names
+
+    # Verify round-trip encoding/decoding
+    buffer = Buffer(child_typedef.encoded)
+    decoded_typedef = decode_typedef(buffer, resolver)
+    assert len(decoded_typedef.fields) == 4
+
+
+def test_inheritance_shadowed_fields():
+    """Test TypeDef encoding/decoding when child shadows parent fields."""
+    fory = Fory(xlang=True)
+    fory.register(ParentClass, namespace="example", typename="ParentClass")
+    fory.register(ChildWithShadowedField, namespace="example", typename="ChildWithShadowedField")
+
+    resolver = fory.type_resolver
+
+    # Encode the child class with shadowed field
+    typedef = encode_typedef(resolver, ChildWithShadowedField)
+
+    # Should have: name (from parent), value (from parent), name (shadowed), extra
+    # But with deduplication, we should only have unique field entries
+    field_names = [f.name for f in typedef.fields]
+
+    # Verify 'name' appears only once (deduplicated)
+    assert field_names.count("name") == 1, f"Expected 'name' to appear once, but found {field_names.count('name')} times"
+    assert "value" in field_names
+    assert "extra" in field_names
+
+    # Verify round-trip
+    buffer = Buffer(typedef.encoded)
+    decoded_typedef = decode_typedef(buffer, resolver)
+
+    decoded_field_names = [f.name for f in decoded_typedef.fields]
+    assert decoded_field_names.count("name") == 1
+
+
+def test_multilevel_inheritance():

Review Comment:
   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



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@fory.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fory.apache.org
For additional commands, e-mail: commits-help@fory.apache.org


Re: [PR] chore(python): handle duplicate field names (TODO for typedef encoding) [fory]

Posted by "chaokunyang (via GitHub)" <gi...@apache.org>.
chaokunyang commented on PR #3384:
URL: https://github.com/apache/fory/pull/3384#issuecomment-3940031574

   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


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@fory.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fory.apache.org
For additional commands, e-mail: commits-help@fory.apache.org


Re: [PR] chore(python): handle duplicate field names [fory]

Posted by "chaokunyang (via GitHub)" <gi...@apache.org>.
chaokunyang commented on code in PR #3384:
URL: https://github.com/apache/fory/pull/3384#discussion_r2867629685


##########
python/pyfory/tests/test_duplicate_field_names.py:
##########
@@ -0,0 +1,50 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+"""
+Tests for field shadowing in inheritance.
+"""
+
+from dataclasses import dataclass
+from pyfory import Fory
+
+
+@dataclass
+class Parent:
+    name: str
+    value: int
+
+
+@dataclass
+class ChildWithShadow(Parent):
+    name: str  # Shadows Parent.name
+    extra: float
+
+
+def test_shadowed_fields_serialization():
+    """Serialization with shadowed and inherited fields."""
+    fory = Fory(xlang=True)
+    fory.register(Parent, namespace="test", typename="Parent")
+    fory.register(ChildWithShadow, namespace="test", typename="ChildWithShadow")
+
+    obj = ChildWithShadow(name="shadowed", value=10, extra=3.14)

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



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@fory.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fory.apache.org
For additional commands, e-mail: commits-help@fory.apache.org


Re: [PR] chore(python): handle duplicate field names [fory]

Posted by "chaokunyang (via GitHub)" <gi...@apache.org>.
chaokunyang merged PR #3384:
URL: https://github.com/apache/fory/pull/3384


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@fory.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fory.apache.org
For additional commands, e-mail: commits-help@fory.apache.org


Re: [PR] chore(python): handle duplicate field names [fory]

Posted by "chaokunyang (via GitHub)" <gi...@apache.org>.
chaokunyang commented on code in PR #3384:
URL: https://github.com/apache/fory/pull/3384#discussion_r2867241888


##########
python/pyfory/tests/test_duplicate_field_names.py:
##########
@@ -0,0 +1,277 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+"""
+Tests for inheritance in TypeDef implementation.
+"""
+
+from dataclasses import dataclass
+from pyfory.buffer import Buffer
+from pyfory.meta.typedef_encoder import encode_typedef
+from pyfory.meta.typedef_decoder import decode_typedef
+from pyfory import Fory
+
+
+@dataclass
+class ParentClass:
+    """Parent class with some fields."""
+
+    name: str
+    value: int
+
+
+@dataclass
+class ChildClass(ParentClass):
+    """Child class that inherits from ParentClass."""
+
+    description: str
+    count: int
+
+
+@dataclass
+class ChildWithShadowedField(ParentClass):
+    """Child class that shadows a parent field name."""
+
+    name: str  # Shadows ParentClass.name
+    extra: float
+
+
+@dataclass
+class GrandchildClass(ChildClass):
+    """Grandchild class for multi-level inheritance."""
+
+    level: int
+
+
+@dataclass
+class GrandchildWithShadowedFields(ChildClass):
+    """Grandchild that shadows fields from multiple levels."""
+
+    name: str  # Shadows ParentClass.name
+    value: int  # Shadows ParentClass.value
+    description: str  # Shadows ChildClass.description
+
+
+def test_inheritance_basic():
+    """Test TypeDef encoding/decoding with basic inheritance."""
+    fory = Fory(xlang=True)
+    fory.register(ParentClass, namespace="example", typename="ParentClass")
+    fory.register(ChildClass, namespace="example", typename="ChildClass")
+
+    resolver = fory.type_resolver
+
+    # Test parent class
+    parent_typedef = encode_typedef(resolver, ParentClass)
+    assert len(parent_typedef.fields) == 2
+    field_names = [f.name for f in parent_typedef.fields]
+    assert "name" in field_names
+    assert "value" in field_names
+
+    # Test child class - should have all fields from parent plus its own
+    child_typedef = encode_typedef(resolver, ChildClass)
+    assert len(child_typedef.fields) == 4
+    field_names = [f.name for f in child_typedef.fields]
+    assert "name" in field_names
+    assert "value" in field_names
+    assert "description" in field_names
+    assert "count" in field_names
+
+    # Verify round-trip encoding/decoding
+    buffer = Buffer(child_typedef.encoded)
+    decoded_typedef = decode_typedef(buffer, resolver)
+    assert len(decoded_typedef.fields) == 4
+
+
+def test_inheritance_shadowed_fields():
+    """Test TypeDef encoding/decoding when child shadows parent fields."""
+    fory = Fory(xlang=True)
+    fory.register(ParentClass, namespace="example", typename="ParentClass")
+    fory.register(ChildWithShadowedField, namespace="example", typename="ChildWithShadowedField")
+
+    resolver = fory.type_resolver
+
+    # Encode the child class with shadowed field
+    typedef = encode_typedef(resolver, ChildWithShadowedField)
+
+    # Should have: name (from parent), value (from parent), name (shadowed), extra
+    # But with deduplication, we should only have unique field entries
+    field_names = [f.name for f in typedef.fields]
+
+    # Verify 'name' appears only once (deduplicated)
+    assert field_names.count("name") == 1, f"Expected 'name' to appear once, but found {field_names.count('name')} times"
+    assert "value" in field_names
+    assert "extra" in field_names
+
+    # Verify round-trip
+    buffer = Buffer(typedef.encoded)
+    decoded_typedef = decode_typedef(buffer, resolver)
+
+    decoded_field_names = [f.name for f in decoded_typedef.fields]
+    assert decoded_field_names.count("name") == 1
+
+
+def test_multilevel_inheritance():
+    """Test TypeDef with multi-level inheritance."""
+    fory = Fory(xlang=True)
+    fory.register(ParentClass, namespace="example", typename="ParentClass")
+    fory.register(ChildClass, namespace="example", typename="ChildClass")
+    fory.register(GrandchildClass, namespace="example", typename="GrandchildClass")
+
+    resolver = fory.type_resolver
+
+    # Grandchild should have all fields from the hierarchy
+    typedef = encode_typedef(resolver, GrandchildClass)
+
+    field_names = [f.name for f in typedef.fields]
+    assert "name" in field_names  # from ParentClass
+    assert "value" in field_names  # from ParentClass
+    assert "description" in field_names  # from ChildClass
+    assert "count" in field_names  # from ChildClass
+    assert "level" in field_names  # from GrandchildClass
+
+    # Should have exactly 5 fields
+    assert len(typedef.fields) == 5
+
+    # Verify round-trip
+    buffer = Buffer(typedef.encoded)
+    decoded_typedef = decode_typedef(buffer, resolver)
+    assert len(decoded_typedef.fields) == 5
+
+
+def test_multilevel_inheritance_with_shadowing():
+    """Test TypeDef with multi-level inheritance and field shadowing."""
+    fory = Fory(xlang=True)
+    fory.register(ParentClass, namespace="example", typename="ParentClass")
+    fory.register(ChildClass, namespace="example", typename="ChildClass")
+    fory.register(
+        GrandchildWithShadowedFields,
+        namespace="example",
+        typename="GrandchildWithShadowedFields",
+    )
+
+    resolver = fory.type_resolver
+
+    # Grandchild shadows: name (from Parent), value (from Parent), description (from Child)
+    typedef = encode_typedef(resolver, GrandchildWithShadowedFields)
+
+    field_names = [f.name for f in typedef.fields]
+
+    # Each shadowed field should appear only once
+    assert field_names.count("name") == 1
+    assert field_names.count("value") == 1
+    assert field_names.count("description") == 1
+    assert "count" in field_names  # from ChildClass, not shadowed
+
+    # Verify round-trip
+    buffer = Buffer(typedef.encoded)
+    decoded_typedef = decode_typedef(buffer, resolver)
+
+    decoded_field_names = [f.name for f in decoded_typedef.fields]
+    assert decoded_field_names.count("name") == 1
+    assert decoded_field_names.count("value") == 1
+    assert decoded_field_names.count("description") == 1
+
+
+def test_no_duplicate_fields_in_typedef():
+    """Ensure typedef never contains duplicate field names."""
+
+    @dataclass
+    class Base:
+        x: int
+
+    @dataclass
+    class Mid(Base):
+        y: int
+
+    @dataclass
+    class Child(Mid):
+        x: int
+        y: int
+
+    fory = Fory(xlang=True)
+    fory.register(Base, namespace="example", typename="Base")
+    fory.register(Mid, namespace="example", typename="Mid")
+    fory.register(Child, namespace="example", typename="Child")
+
+    resolver = fory.type_resolver
+
+    typedef = encode_typedef(resolver, Child)
+
+    field_names = [f.name for f in typedef.fields]
+
+    assert len(field_names) == len(set(field_names)), f"Duplicate fields found: {field_names}"
+
+
+def test_shadowing_with_different_type():
+    """Ensure shadowed field uses child's type, not parent's."""
+
+    @dataclass
+    class Parent:
+        value: int
+
+    @dataclass
+    class Child(Parent):
+        value: str  # override type
+
+    fory = Fory(xlang=True)
+    fory.register(Parent, namespace="example", typename="Parent")
+    fory.register(Child, namespace="example", typename="Child")
+
+    resolver = fory.type_resolver
+
+    typedef = encode_typedef(resolver, Child)
+
+    # just ensure only one exists
+    assert sum(f.name == "value" for f in typedef.fields) == 1
+
+
+def test_shadowing_uses_child_definition():
+    """Ensure shadowed field uses child's definition, not parent's."""
+
+    @dataclass
+    class Parent:
+        name: str
+
+    @dataclass
+    class Child(Parent):
+        name: str  # shadow
+
+    fory = Fory(xlang=True)
+    fory.register(Parent, namespace="example", typename="Parent")
+    fory.register(Child, namespace="example", typename="Child")
+
+    resolver = fory.type_resolver
+
+    typedef = encode_typedef(resolver, Child)
+
+    field_map = {f.name: f for f in typedef.fields}
+
+    assert "name" in field_map
+
+    # Critical assertion
+    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__":

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



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@fory.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fory.apache.org
For additional commands, e-mail: commits-help@fory.apache.org


Re: [PR] chore(python): handle duplicate field names (TODO for typedef encoding) [fory]

Posted by "eyad-hazem-elmorsy (via GitHub)" <gi...@apache.org>.
eyad-hazem-elmorsy commented on PR #3384:
URL: https://github.com/apache/fory/pull/3384#issuecomment-3940133091

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


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@fory.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fory.apache.org
For additional commands, e-mail: commits-help@fory.apache.org


Re: [PR] chore(python): handle duplicate field names [fory]

Posted by "chaokunyang (via GitHub)" <gi...@apache.org>.
chaokunyang commented on code in PR #3384:
URL: https://github.com/apache/fory/pull/3384#discussion_r2867241888


##########
python/pyfory/tests/test_duplicate_field_names.py:
##########
@@ -0,0 +1,277 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+"""
+Tests for inheritance in TypeDef implementation.
+"""
+
+from dataclasses import dataclass
+from pyfory.buffer import Buffer
+from pyfory.meta.typedef_encoder import encode_typedef
+from pyfory.meta.typedef_decoder import decode_typedef
+from pyfory import Fory
+
+
+@dataclass
+class ParentClass:
+    """Parent class with some fields."""
+
+    name: str
+    value: int
+
+
+@dataclass
+class ChildClass(ParentClass):
+    """Child class that inherits from ParentClass."""
+
+    description: str
+    count: int
+
+
+@dataclass
+class ChildWithShadowedField(ParentClass):
+    """Child class that shadows a parent field name."""
+
+    name: str  # Shadows ParentClass.name
+    extra: float
+
+
+@dataclass
+class GrandchildClass(ChildClass):
+    """Grandchild class for multi-level inheritance."""
+
+    level: int
+
+
+@dataclass
+class GrandchildWithShadowedFields(ChildClass):
+    """Grandchild that shadows fields from multiple levels."""
+
+    name: str  # Shadows ParentClass.name
+    value: int  # Shadows ParentClass.value
+    description: str  # Shadows ChildClass.description
+
+
+def test_inheritance_basic():
+    """Test TypeDef encoding/decoding with basic inheritance."""
+    fory = Fory(xlang=True)
+    fory.register(ParentClass, namespace="example", typename="ParentClass")
+    fory.register(ChildClass, namespace="example", typename="ChildClass")
+
+    resolver = fory.type_resolver
+
+    # Test parent class
+    parent_typedef = encode_typedef(resolver, ParentClass)
+    assert len(parent_typedef.fields) == 2
+    field_names = [f.name for f in parent_typedef.fields]
+    assert "name" in field_names
+    assert "value" in field_names
+
+    # Test child class - should have all fields from parent plus its own
+    child_typedef = encode_typedef(resolver, ChildClass)
+    assert len(child_typedef.fields) == 4
+    field_names = [f.name for f in child_typedef.fields]
+    assert "name" in field_names
+    assert "value" in field_names
+    assert "description" in field_names
+    assert "count" in field_names
+
+    # Verify round-trip encoding/decoding
+    buffer = Buffer(child_typedef.encoded)
+    decoded_typedef = decode_typedef(buffer, resolver)
+    assert len(decoded_typedef.fields) == 4
+
+
+def test_inheritance_shadowed_fields():
+    """Test TypeDef encoding/decoding when child shadows parent fields."""
+    fory = Fory(xlang=True)
+    fory.register(ParentClass, namespace="example", typename="ParentClass")
+    fory.register(ChildWithShadowedField, namespace="example", typename="ChildWithShadowedField")
+
+    resolver = fory.type_resolver
+
+    # Encode the child class with shadowed field
+    typedef = encode_typedef(resolver, ChildWithShadowedField)
+
+    # Should have: name (from parent), value (from parent), name (shadowed), extra
+    # But with deduplication, we should only have unique field entries
+    field_names = [f.name for f in typedef.fields]
+
+    # Verify 'name' appears only once (deduplicated)
+    assert field_names.count("name") == 1, f"Expected 'name' to appear once, but found {field_names.count('name')} times"
+    assert "value" in field_names
+    assert "extra" in field_names
+
+    # Verify round-trip
+    buffer = Buffer(typedef.encoded)
+    decoded_typedef = decode_typedef(buffer, resolver)
+
+    decoded_field_names = [f.name for f in decoded_typedef.fields]
+    assert decoded_field_names.count("name") == 1
+
+
+def test_multilevel_inheritance():
+    """Test TypeDef with multi-level inheritance."""
+    fory = Fory(xlang=True)
+    fory.register(ParentClass, namespace="example", typename="ParentClass")
+    fory.register(ChildClass, namespace="example", typename="ChildClass")
+    fory.register(GrandchildClass, namespace="example", typename="GrandchildClass")
+
+    resolver = fory.type_resolver
+
+    # Grandchild should have all fields from the hierarchy
+    typedef = encode_typedef(resolver, GrandchildClass)
+
+    field_names = [f.name for f in typedef.fields]
+    assert "name" in field_names  # from ParentClass
+    assert "value" in field_names  # from ParentClass
+    assert "description" in field_names  # from ChildClass
+    assert "count" in field_names  # from ChildClass
+    assert "level" in field_names  # from GrandchildClass
+
+    # Should have exactly 5 fields
+    assert len(typedef.fields) == 5
+
+    # Verify round-trip
+    buffer = Buffer(typedef.encoded)
+    decoded_typedef = decode_typedef(buffer, resolver)
+    assert len(decoded_typedef.fields) == 5
+
+
+def test_multilevel_inheritance_with_shadowing():
+    """Test TypeDef with multi-level inheritance and field shadowing."""
+    fory = Fory(xlang=True)
+    fory.register(ParentClass, namespace="example", typename="ParentClass")
+    fory.register(ChildClass, namespace="example", typename="ChildClass")
+    fory.register(
+        GrandchildWithShadowedFields,
+        namespace="example",
+        typename="GrandchildWithShadowedFields",
+    )
+
+    resolver = fory.type_resolver
+
+    # Grandchild shadows: name (from Parent), value (from Parent), description (from Child)
+    typedef = encode_typedef(resolver, GrandchildWithShadowedFields)
+
+    field_names = [f.name for f in typedef.fields]
+
+    # Each shadowed field should appear only once
+    assert field_names.count("name") == 1
+    assert field_names.count("value") == 1
+    assert field_names.count("description") == 1
+    assert "count" in field_names  # from ChildClass, not shadowed
+
+    # Verify round-trip
+    buffer = Buffer(typedef.encoded)
+    decoded_typedef = decode_typedef(buffer, resolver)
+
+    decoded_field_names = [f.name for f in decoded_typedef.fields]
+    assert decoded_field_names.count("name") == 1
+    assert decoded_field_names.count("value") == 1
+    assert decoded_field_names.count("description") == 1
+
+
+def test_no_duplicate_fields_in_typedef():
+    """Ensure typedef never contains duplicate field names."""
+
+    @dataclass
+    class Base:
+        x: int
+
+    @dataclass
+    class Mid(Base):
+        y: int
+
+    @dataclass
+    class Child(Mid):
+        x: int
+        y: int
+
+    fory = Fory(xlang=True)
+    fory.register(Base, namespace="example", typename="Base")
+    fory.register(Mid, namespace="example", typename="Mid")
+    fory.register(Child, namespace="example", typename="Child")
+
+    resolver = fory.type_resolver
+
+    typedef = encode_typedef(resolver, Child)
+
+    field_names = [f.name for f in typedef.fields]
+
+    assert len(field_names) == len(set(field_names)), f"Duplicate fields found: {field_names}"
+
+
+def test_shadowing_with_different_type():
+    """Ensure shadowed field uses child's type, not parent's."""
+
+    @dataclass
+    class Parent:
+        value: int
+
+    @dataclass
+    class Child(Parent):
+        value: str  # override type
+
+    fory = Fory(xlang=True)
+    fory.register(Parent, namespace="example", typename="Parent")
+    fory.register(Child, namespace="example", typename="Child")
+
+    resolver = fory.type_resolver
+
+    typedef = encode_typedef(resolver, Child)
+
+    # just ensure only one exists
+    assert sum(f.name == "value" for f in typedef.fields) == 1
+
+
+def test_shadowing_uses_child_definition():
+    """Ensure shadowed field uses child's definition, not parent's."""
+
+    @dataclass
+    class Parent:
+        name: str
+
+    @dataclass
+    class Child(Parent):
+        name: str  # shadow
+
+    fory = Fory(xlang=True)
+    fory.register(Parent, namespace="example", typename="Parent")
+    fory.register(Child, namespace="example", typename="Child")
+
+    resolver = fory.type_resolver
+
+    typedef = encode_typedef(resolver, Child)
+
+    field_map = {f.name: f for f in typedef.fields}
+
+    assert "name" in field_map
+
+    # Critical assertion
+    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__":

Review Comment:
   I think we also need e2e serialization tests instead of only typedef tests



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@fory.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fory.apache.org
For additional commands, e-mail: commits-help@fory.apache.org


Re: [PR] chore(python): handle duplicate field names [fory]

Posted by "eyad-hazem-elmorsy (via GitHub)" <gi...@apache.org>.
eyad-hazem-elmorsy commented on PR #3384:
URL: https://github.com/apache/fory/pull/3384#issuecomment-3977416672

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


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@fory.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fory.apache.org
For additional commands, e-mail: commits-help@fory.apache.org


Re: [PR] chore(python): handle duplicate field names (TODO for typedef encoding) [fory]

Posted by "eyad-hazem-elmorsy (via GitHub)" <gi...@apache.org>.
eyad-hazem-elmorsy commented on PR #3384:
URL: https://github.com/apache/fory/pull/3384#issuecomment-3940080000

   I found that data class 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


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@fory.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fory.apache.org
For additional commands, e-mail: commits-help@fory.apache.org