Skip to content

[Feature Request]: Support TypedDict hint in Apache Beam. #24704

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

Open
1 of 15 tasks
tvalentyn opened this issue Dec 19, 2022 · 5 comments
Open
1 of 15 tasks

[Feature Request]: Support TypedDict hint in Apache Beam. #24704

tvalentyn opened this issue Dec 19, 2022 · 5 comments

Comments

@tvalentyn
Copy link
Contributor

tvalentyn commented Dec 19, 2022

What would you like to happen?

CoGroupByKey produces Dict[K, Union[...]], which is narrower than TypedDict. If the user follows the CoGroupByKey with a ParDo where the function takes TypeDict as input, e.g. ParDo(fn(element: TypedDict)), then the type check in Beam fails.

Users having types like:

myTypeDict = TypedDict(
    'myTypeDict', {
        'field_1': List[Field1Type],
        'field_2': List[Field2Type],
    })

are using less specific hints like myLessSpecificDict = Dict(str, Union[List[Field1Type], List[Field2Type]]) in order to satisfy beams typing.

We could have a first-class typehint TypeDict, which may be easier once we deprecate Python 3.7.

Issue Priority

Priority: 3 (nice-to-have improvement)

Issue Components

  • Component: Python SDK
  • Component: Java SDK
  • Component: Go SDK
  • Component: Typescript SDK
  • Component: IO connector
  • Component: Beam examples
  • Component: Beam playground
  • Component: Beam katas
  • Component: Website
  • Component: Spark Runner
  • Component: Flink Runner
  • Component: Samza Runner
  • Component: Twister2 Runner
  • Component: Hazelcast Jet Runner
  • Component: Google Cloud Dataflow Runner
@AnandInguva
Copy link
Contributor

Update the issue title?

@tvalentyn tvalentyn changed the title [Feature Request]: [Feature Request]: Support TypeDict hint in Apache Beam. Dec 20, 2022
@tvalentyn
Copy link
Contributor Author

Done, thanks.

@bfontaine
Copy link

bfontaine commented Mar 24, 2023

Why have specific code for TypedDict when you can use the more generic Mapping, which accepts not only TypedDict, but also any kind of dict?

Note: there’s a typo in the issue title: it should be "TypedDict", not "TypeDict".

@aaronsarna
Copy link

The value of TypedDict over Mapping is if the different keys are expected to have different values. For example, let's say I did the following:

 to_group = {
  'ints': pcoll1,  # PCollection of (str, int)
  'strs': pcoll2,  # PCollection of (str, str)
}
grouped = to_group | beam.CoGroupByKey()
something = grouped | beam.MapTuple(map_grouped)

If using Mapping, the method would have to start:

def map_grouped(key: str, values: Mapping[str, Iterable[str | int]]):
  ints = values['ints']  # type: Iterable[int]
  strs = values['strs']  # type: Iterable[str]
  ...

since the type checker can't tell which key corresponds with which value type.

If I could instead have a TypedDict, it could be a bit cleaner:

class GroupedValues(TypedDict):
  ints: Iterable[int]
  strs: Iterable[str]

def map_grouped(key: str, values: GroupedValues):
  ints = values['ints']
  strs = values['strs']

and the type checker needs no additional help.

@tvalentyn tvalentyn changed the title [Feature Request]: Support TypeDict hint in Apache Beam. [Feature Request]: Support TypedDict hint in Apache Beam. Jul 24, 2023
@tvalentyn
Copy link
Contributor Author

cc: @jrmccluskey

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

No branches or pull requests

4 participants