You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It would be awesome for the following code to work (written in 3.12 syntax, but would be great in <=3.11 syntax too)
T = TypeVar("T")
@dataclass.dataclass()
class NeatOutputClass[T]:
data: T
class ReusableTransform[T](beam.PTransform):
def __init__(self, value_extractor: Callable[[Any], T):
# This is a bit contrived, but not unreasonable for a transform acting
# on slightly different data. Could be done as a generic DoFn too.
self.value_extractor = value_extractor
def expand(self, pcoll: beam.PCollection) -> NeatOutputClass[T]:
# Overly simplified for simple reproducibility.
return pcoll | beam.Map(lambda x: self.value_extractor(x))
But unfortunately this yields TypeError: Subscripted generics cannot be used with class and instance checks
I believe this runs into a similar problem as #33189, the output of NeatOutputClass[T] doesn't play nice with our type hinting infrastructure. I'm not sure I've ever seen someone try to do a fully generic PTransform definition before, you may be able to get away with a similar solution as outlined in that issue - #33189 (comment)
The .with_output_types() call to parameterize NeatOutputClass in some way might work, although I'm not 100% sure on if it would be happy.
What would you like to happen?
It would be awesome for the following code to work (written in 3.12 syntax, but would be great in <=3.11 syntax too)
But unfortunately this yields
TypeError: Subscripted generics cannot be used with class and instance checks
Full Stack Trace
Issue Priority
Priority: 2 (default / most feature requests should be filed as P2)
Issue Components
The text was updated successfully, but these errors were encountered: