zhan7236 opened a new pull request, #3059:
URL: https://github.com/apache/fory/pull/3059

   ## Why?
   
   Python Fory lacks support for `typing.Union` types, which prevents 
cross-language serialization compatibility with other languages like C++, Java, 
and Rust that already support union/variant types. This limitation was reported 
in issue #3029.
   
   Without Union type support, Python users cannot:
   - Serialize union types to communicate with other languages
   - Use type annotations like `Union[int, str]` in dataclass fields
   - Leverage the full xlang serialization capabilities
   
   ## What does this PR do?
   
   This PR implements complete Union type support for Python Fory by:
   
   1. **Added TypeId constants** to `python/pyfory/type.py`:
      - `TypeId.UNION = 38` - for tagged union types
      - `TypeId.NONE = 39` - for empty/unit values
   
   2. **Implemented UnionSerializer** in `python/pyfory/_serializer.py`:
      - Serializes Union types by writing variant index + value
      - Supports both Python mode (no type info) and xlang mode (with type info)
      - Properly dispatches to alternative type serializers
      - Handles Optional types (`Union[T, None]`) by filtering out NoneType
   
   3. **Updated type detection** in `python/pyfory/_registry.py`:
      - Automatically detects `typing.Union` types using `typing.get_origin()`
      - Creates appropriate UnionSerializer instances
      - Handles edge cases (empty unions, single-type unions)
   
   4. **Exported UnionSerializer** from `python/pyfory/serializer.py`:
      - Available in both Cython and pure Python modes
   
   5. **Added comprehensive tests** in `python/pyfory/tests/test_union.py`:
      - Basic types (int, str, float)
      - Collection types (list, dict)
      - Optional types
      - Nested unions in dataclasses
      - Cross-language serialization
   
   The implementation follows the same binary protocol as C++ variant 
serialization:
   1. Write variant index (varuint32)
   2. In xlang mode, write type info for the active alternative
   3. Write the value using the alternative's serializer
   
   ## Related issues
   
   - Fixes #3029
   
   ## Does this PR introduce any user-facing change?
   
   - [x] Does this PR introduce any public API change?
     - **Yes**: Adds support for `typing.Union` types in serialization. Users 
can now use Union types in their code and they will be automatically 
serialized/deserialized.
     - This is backward compatible - existing code continues to work.
   
   - [x] Does this PR introduce any binary protocol compatibility change?
     - **No**: The implementation uses the existing TypeId.UNION (38) and 
TypeId.NONE (39) defined in the specification.
     - The binary protocol follows the same format as C++/Java/Rust 
implementations.
     - Existing serialized data is not affected.
   
   ## Benchmark
   
   This PR does not have a performance impact on existing functionality:
   - Union type handling only activates when Union types are actually used
   - The implementation uses efficient type checking via `isinstance()`
   - Variant index is encoded using varuint32 (compact encoding)
   - No changes to existing serializers or hot paths
   
   For Union types specifically, the overhead is minimal:
   - One varuint32 write/read for the variant index
   - One type dispatch (same as normal polymorphic serialization)
   - No additional allocations or copies


-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to