Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix naming for NewType and NamedTuple #3

Merged
merged 1 commit into from
Sep 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions sdks/python/apache_beam/runners/interactive/sql/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,7 @@ def pformat_namedtuple(schema: NamedTuple) -> str:
return '{}({})'.format(
schema.__name__,
', '.join([
'{}: {}'.format(k, v.__name__ if hasattr(v, '__name__') else repr(v))
for k,
'{}: {}'.format(k, repr(v)) for k,
v in schema.__annotations__.items()
]))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,12 @@ def convert_to_beam_type(typ):
# TODO(https://github.com/apache/beam/issues/19954): Currently unhandled.
_LOGGER.info('Converting string literal type hint to Any: "%s"', typ)
return typehints.Any
elif sys.version_info >= (3, 10) and isinstance(typ, typing.NewType): # pylint: disable=isinstance-second-argument-not-valid-type
# Special case for NewType, where, since Python 3.10, NewType is now a class
# rather than a function.
# TODO(https://github.com/apache/beam/issues/20076): Currently unhandled.
_LOGGER.info('Converting NewType type hint to Any: "%s"', typ)
Copy link
Owner

@AnandInguva AnandInguva Sep 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. Before this change in Python 3.10,

if NewType('Number', int) was passed, then the type would be apache_beam.typehints.native_type_compatibility_test.Number instead of Any right?. With this change, it would become Any

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct.

We still need to handle it fully in apache#20076, but that can be done outside of this 3.10 PR.

return typehints.Any
elif getattr(typ, '__module__', None) != 'typing':
# Only translate types from the typing module.
return typ
Expand Down