InsightSoftwareConsortium / InsightSoftwareConsortium/ITK
typing.get_type_hints() on wrapped filters fails on Python 3.14 (itkTemplate | TypeAliasType)
- Dominant language
- C++
- Stars
- 1.7k
- Forks
- 748
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 64
Description
`typing.get_type_hints()` on any wrapped filter raises `TypeError` under Python 3.14, so `MedianImageFilterTest.py` fails.
## Reproduce
Python 3.14.6, ITK 6.0.0 (main), macOS arm64, `ITK_WRAP_PYTHON=ON`:
```python
import itk, typing
typing.get_type_hints(itk.median_image_filter, globalns={"itk": itk})
```
```
TypeError: unsupported operand type(s) for |: 'itkTemplate' and 'typing.TypeAliasType'
```
The same failure occurs for `itk.binary_threshold_image_filter`, so it is not
specific to one filter.
## Where it bites in the test suite
`Modules/Filtering/Smoothing/wrapping/test/MedianImageFilterTest.py:58` calls
`get_type_hints()` directly, so `PythonMedianImageFilterTest` fails:
```
File ".../MedianImageFilterTest.py", line 58, in
type_hints = get_type_hints(itk.median_image_filter, globalns={"itk": itk})
File ".../python3.14/typing.py", line 2441, in get_type_hints
value = _eval_type(value, globalns, localns, (), format=format, owner=obj, prefer_fwd_module=True)
File ".../python3.14/typing.py", line 490, in _eval_type
return functools.reduce(operator.or_, ev_args)
TypeError: unsupported operand type(s) for |: 'itkTemplate' and 'typing.TypeAliasType'
```
## Cause
Python 3.14's `typing._eval_type` evaluates a union annotation by reducing its
arguments with `operator.or_`. The generated annotations mix `itkTemplate`
instances with `typing.TypeAliasType` values, and `itkTemplate` does not
implement `__or__` / `__ror__` for that operand, so the reduction raises.
Earlier Python versions did not take this path, which is why the test passes
there.
## Possible directions
- Give `itkTemplate` `__or__` / `__ror__` returning `typing.Union[...]`, so the
reduction Python 3.14 performs succeeds.
- Or emit the annotations as `typing.Union[...]` rather than `X | Y`, so no
runtime `|` is evaluated.
Happy to prepare a patch once the preferred direction is clear.
Contributor guide
Research direction
Start with Modules/Filtering/Smoothing/wrapping/test/MedianImageFilterTest.py:58 and reproduce the get_type_hints() failure on Python 3.14 using the wrapped filter entry points. Then inspect how itkTemplate annotations are generated or evaluated and compare the proposed directions. Done means get_type_hints() works for wrapped filters such as median_image_filter and binary_threshold_image_filter, and MedianImageFilterTest.py passes.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- testing
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 52/100