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.

Author GBeauregard
Recipients GBeauregard, JelleZijlstra, eric.smith
Date 2022-01-27.20:04:08
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <[email protected]>
In-reply-to
Content
```
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.
History
Date User Action Args
2022-01-27 20:04:08GBeauregardsetrecipients: + GBeauregard, eric.smith, JelleZijlstra
2022-01-27 20:04:08GBeauregardsetmessageid: <[email protected]>
2022-01-27 20:04:08GBeauregardlinkissue46552 messages
2022-01-27 20:04:08GBeauregardcreate