-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
[Bug]: Python Runtime Type Introspection Mishandles PEP 585 Generics #23366
Comments
Hi @hmc-cs-mdrissi, can you share how exactly it fails? Do you get a helpful error? I'm not sure we can work on this in the short-term, but if the error is confusing we might at least do a quick change to make the error message more helpful. |
The full error message is,
With key line being |
Got it thanks. We could certainly do better by just raising an error in normalization stating that PEP 585 typehints are not currently supported. That could be a first step and should be pretty low effort, could be a good task for someone wanting to learn about our typehint infrastructure. |
PR is merged, design doc is available at https://docs.google.com/document/d/1KgCnYlXDPi2SHtjuzkztUMpFfwgop29WishCI4dHZ30/edit?usp=sharing |
Is PEP-585 supposed to be fully handled now? I noticed that imports from Example: from collections.abc import Iterator
import apache_beam as beam
def add_range(n: int) -> Iterator[int]:
for i in range(n):
yield i
with beam.Pipeline() as p:
numbers = p | "Create" >> beam.Create([1, 2, 3, 4, 5])
numbers = numbers | beam.FlatMap(add_range)
_ = numbers | beam.combiners.Count.PerElement() | beam.Map(print
|
@jrmccluskey Thank you for the quick reply! Looking forward to the next release 👍 |
What happened?
All of these print False. This is because is_consistent_with/normalize type machinery in beam does not handle tuple vs typing.Tuple/dict vs typing.Dict/list vs typing.List/etc equivalents. This leads to beam's runtime type checking crashing on valid code like,
Here add_parity returns tuple, but GroupByKey expects Tuple. python 3.9+ recommends using tuple and some tools will even automatically replace Tuple -> tuple. So at moment workarounds are either stick with typing.Tuple or disable beam runtime type checking.
An example of assumption of typing.X generics that fails for pep 585 generics is here.
getattr(x, '__module__', None) == 'typing'
is not true for builtin generic equivalents as they come from there original source which may be various modules (most of basic ones are 'builtins'). There may be other code that needs changes astuple[int, str] != Tuple[int, str]
(equivalent but cpython they are handled by different types technically).Issue Priority
Priority: 2
Issue Component
Component: sdk-py-core
The text was updated successfully, but these errors were encountered: