Skip to content

Commit

Permalink
[Python]Support pipe operator as Union (PEP -604) (#24106)
Browse files Browse the repository at this point in the history
Fixes #21972
  • Loading branch information
AnandInguva authored Nov 15, 2022
1 parent 526e7a5 commit 08d5f72
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import collections
import logging
import sys
import types
import typing

from apache_beam.typehints import typehints
Expand Down Expand Up @@ -176,6 +177,14 @@ def convert_to_beam_type(typ):
Raises:
ValueError: The type was malformed.
"""
# Convert `int | float` to typing.Union[int, float]
# pipe operator as Union and types.UnionType are introduced
# in Python 3.10.
# GH issue: https://github.com/apache/beam/issues/21972
if (sys.version_info.major == 3 and
sys.version_info.minor >= 10) and (isinstance(typ, types.UnionType)):
typ = typing.Union[typ]

if isinstance(typ, typing.TypeVar):
# This is a special case, as it's not parameterized by types.
# Also, identity must be preserved through conversion (i.e. the same
Expand Down
5 changes: 5 additions & 0 deletions sdks/python/apache_beam/typehints/typehints.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@

import copy
import logging
import sys
import types
import typing
from collections import abc

Expand Down Expand Up @@ -384,6 +386,9 @@ def validate_composite_type_param(type_param, error_msg_prefix):
not isinstance(type_param, tuple(possible_classes)) and
type_param is not None and
getattr(type_param, '__module__', None) != 'typing')
if sys.version_info.major == 3 and sys.version_info.minor >= 10:
if isinstance(type_param, types.UnionType):
is_not_type_constraint = False
is_forbidden_type = (
isinstance(type_param, type) and type_param in DISALLOWED_PRIMITIVE_TYPES)

Expand Down
10 changes: 10 additions & 0 deletions sdks/python/apache_beam/typehints/typehints_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1599,6 +1599,16 @@ def expand(self, pcoll: typing.Any) -> typehints.Any:
self.assertEqual(th.input_types, ((typehints.Any, ), {}))
self.assertEqual(th.input_types, ((typehints.Any, ), {}))

def test_pipe_operator_as_union(self):
# union types can be written using pipe operator from Python 3.10.
# https://peps.python.org/pep-0604/
if sys.version_info.major == 3 and sys.version_info.minor >= 10:
type_a = int | float # pylint: disable=unsupported-binary-operation
type_b = typing.Union[int, float]
self.assertEqual(
native_type_compatibility.convert_to_beam_type(type_a),
native_type_compatibility.convert_to_beam_type(type_b))


if __name__ == '__main__':
unittest.main()

0 comments on commit 08d5f72

Please sign in to comment.