Abacn commented on PR #40081:
URL: https://github.com/apache/beam/pull/40081#issuecomment-5626444738
Newly added test failing on Python3.10-3.12 but passing on Python3.13,3.14.
Root cause: a CPython behavior change between 3.12 and 3.13 in `issubclass()`
when dealing with `types.GenericAlias` (e.g. `tuple[int, str]`):
1. **`issubclass` difference on `GenericAlias`**:
- In **Python <= 3.12**: `issubclass(tuple[int, str], tuple)` returned
`False` directly without raising an exception.
- In **Python >= 3.13**: CPython changed `issubclass` so that passing a
`types.GenericAlias` raises `TypeError: issubclass() arg 1 must be a class`.
2. **Impact on
[_safe_issubclass](sdks/python/apache_beam/typehints/native_type_compatibility.py#L125-L138)**:
-
[_safe_issubclass](sdks/python/apache_beam/typehints/native_type_compatibility.py#L125-L138)
previously only fell back to inspecting `derived.__origin__` inside its
`except (TypeError, AttributeError):` block.
- On Python 3.13, `issubclass` raised `TypeError`, triggering the
`__origin__` check which returned `True`.
- On Python <= 3.12, `issubclass` simply returned `False`, so
`_safe_issubclass(tuple[...], tuple)` evaluated to `False`.
3. **Consequence during Schema Translation**:
- In [_safe_issubclass(type_, Sequence) and not _safe_issubclass(type_,
(str, tuple))](sdks/python/apache_beam/typehints/schemas.py#L388-L410):
- `_safe_issubclass(tuple[...], Sequence)` was `True` (because
`typing.Sequence.__subclasscheck__` raises `TypeError` across all Python
versions, triggering the fallback).
- `_safe_issubclass(tuple[...], (str, tuple))` returned `False` on
Python <= 3.12.
- As a result, Python <= 3.12 treated `tuple[...]` as an arbitrary
`Sequence` and translated it into `ArrayType` (which decodes to a `list` rather
than a `tuple`).
--
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]