Python typehints: support forward references
- 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
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