Skip to content

typing.get_type_hints() on wrapped filters fails on Python 3.14 (itkTemplate | TypeAliasType) #6863

Description

@hjmjohnson

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:

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 <module>
    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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions