apache / apache/beam

Python typehints: support forward references

Open
#19,954 0 comments 0 reactions 0 assignees View on GitHub
bug core P3 python types
Dominant language
Java
Stars
8.7k
Forks
4.7k
Avg merge
1d 20h
Merged PRs (30d)
196

Description

Typehints may be given as string literals: https://www.python.org/dev/peps/pep-0484/#forward-references
These are currently not evaluated and result in errors.
Example 1:
```

def test_typed_callable_string_hints(self):
def do_fn(element: 'int') -> 'typehints.List[str]':

return [[str(element)] * 2]

result = [1, 2] | beam.ParDo(do_fn)
self.assertEqual([['1',
'1'], ['2', '2']], sorted(result))

```

This results in:
```

> return issubclass(sub, base)
E TypeError: issubclass() arg 2 must be a class or tuple of
classes

typehints.py:1168: TypeError

```

Example 2:
```

def test_typed_dofn_string_hints(self):
class MyDoFn(beam.DoFn):
def process(self, element:
'int') -> 'typehints.List[str]':
return [[str(element)] * 2]

result = [1, 2] | beam.ParDo(MyDoFn())

self.assertEqual([['1', '1'], ['2', '2']], sorted(result))

```

This results in:
```

> raise ValueError('%s is not iterable' % type_hint)
E ValueError: typehints.List[str] is
not iterable

typehints.py:1194: ValueError

```

where the non-iterable entity the error refers to is a string literal ("typehints.List[str]").

Imported from Jira [BEAM-8487](https://issues.apache.org/jira/browse/BEAM-8487). Original Jira may contain additional context.
Reported by: udim.

Contributor guide

Open the contributing guide

Research direction

Start in typehints.py around the failures at lines 1168 and 1194, using the two typed callable and DoFn examples as reproduction cases. Trace how string-literal annotations are handled, then confirm both examples work without the shown TypeError or ValueError.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
developer-experience
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.