This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: typing: get_type_hints can't handle stringified annotations with leading spaces
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.11, Python 3.10, Python 3.9
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: AlexWaygood, GBeauregard, JelleZijlstra, eric.smith, gvanrossum, kj, sobolevn
Priority: normal Keywords:

Created on 2022-01-27 20:04 by GBeauregard, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (5)
msg411914 - (view) Author: Gregory Beauregard (GBeauregard) * Date: 2022-01-27 20:04
```
class C:
    a: " ClassVar[int]" = 3
get_type_hints(C, globals())  # SyntaxError: Forward reference must be an expression -- got ' ClassVar[int]'

```

I discovered while investigating the viability of moving dataclasses.py to using typing.py's internal type introspection tools that it can't handle stringified annotations with leading spaces.

This is covered in dataclasses unit tests: https://github.com/python/cpython/blob/26b0482393a313e3bda364a35e7417e9db52c1c4/Lib/test/test_dataclasses.py#L3033
The relevant failing code in typing.py is here: https://github.com/python/cpython/blob/26b0482393a313e3bda364a35e7417e9db52c1c4/Lib/typing.py#L671

Leading spaces are treated like indention so there's a syntax error.

This would be trivial to fix by adding an lstrip() to the compilation code, but it's not clear to me this should be considered a bug. Should this be left as-is, or changed? I'm happy to submit a patch if there's agreement it's a bug.
msg411915 - (view) Author: Jelle Zijlstra (JelleZijlstra) * (Python committer) Date: 2022-01-27 20:09
I vote we don't change this. PEP 484 says:

> The string literal should contain a valid Python expression (i.e., compile(lit, '', 'eval') should be a valid code object) and it should evaluate without errors once the module has been fully loaded. 

But compile() fails for leading whitespace.

I also don't see a real use case for this. The dataclasses tests were presumably just added for completeness. If there's evidence this is a common pattern in the wild maybe we can reconsider.
msg411920 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2022-01-27 20:28
The dataclasses tests were in fact just added for completeness. I'd be okay with changing dataclasses to reject leading spaces.
msg411925 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2022-01-27 21:03
We should not accept leading spaces.--
--Guido (mobile)
msg411931 - (view) Author: Alex Waygood (AlexWaygood) * (Python triager) Date: 2022-01-27 21:57
I agree with Jelle and Guido
History
Date User Action Args
2022-04-11 14:59:55adminsetgithub: 90710
2022-01-27 22:50:26gvanrossumsetstatus: open -> closed
resolution: not a bug
stage: resolved
2022-01-27 21:57:59AlexWaygoodsetmessages: + msg411931
2022-01-27 21:03:54gvanrossumsetmessages: + msg411925
2022-01-27 20:28:28eric.smithsetmessages: + msg411920
2022-01-27 20:09:55JelleZijlstrasetnosy: + gvanrossum, sobolevn, kj, AlexWaygood
2022-01-27 20:09:30JelleZijlstrasetmessages: + msg411915
2022-01-27 20:04:08GBeauregardcreate