Message391434
`typing.get_type_hints` does not accept type annotations (in string capsulated form) with leading whitespaces.
```
>>> def foo(arg) -> ' str': ...
>>> typing.get_type_hints(foo)
Traceback (most recent call last):
File "/usr/local/lib/python3.9/typing.py", line 519, in __init__
code = compile(arg, '<string>', 'eval')
File "<string>", line 1
str
IndentationError: unexpected indent
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.9/typing.py", line 1448, in get_type_hints
value = ForwardRef(value)
File "/usr/local/lib/python3.9/typing.py", line 521, in __init__
raise SyntaxError(f"Forward reference must be an expression -- got {arg!r}")
SyntaxError: Forward reference must be an expression -- got ' str'
```
When elaborating on this issue, an inconsistency of hevaiour between ``eval`` function call and ``compile`` in ``'eval'`` mode was also noticed, where the former accepts leading whitespaces and the latter does not.
However, as discussed in https://bugs.python.org/issue41887 , I would then propose manually ``lstrip`` whitespaces from the type annotations:
```
519c519
< code = compile(arg.lstrip(' \t'), '<string>', 'eval')
---
> code = compile(arg, '<string>', 'eval')
``` |
|
Date |
User |
Action |
Args |
2021-04-20 13:03:19 | jarryshaw | set | recipients:
+ jarryshaw |
2021-04-20 13:03:19 | jarryshaw | set | messageid: <1618923799.18.0.0357006810909.issue43893@roundup.psfhosted.org> |
2021-04-20 13:03:19 | jarryshaw | link | issue43893 messages |
2021-04-20 13:03:19 | jarryshaw | create | |
|