Skip to content

typing: get_type_hints can't handle stringified annotations with leading spaces #90710

Description

@GBeauregard
BPO 46552
Nosy @gvanrossum, @ericvsmith, @JelleZijlstra, @sobolevn, @Fidget-Spinner, @AlexWaygood, @GBeauregard

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2022-01-27.22:50:26.308>
created_at = <Date 2022-01-27.20:04:08.321>
labels = ['type-bug', '3.9', '3.10', '3.11', 'invalid', 'library']
title = "typing: get_type_hints can't handle stringified annotations with leading spaces"
updated_at = <Date 2022-01-27.22:50:26.307>
user = 'https://github.com/GBeauregard'

bugs.python.org fields:

activity = <Date 2022-01-27.22:50:26.307>
actor = 'gvanrossum'
assignee = 'none'
closed = True
closed_date = <Date 2022-01-27.22:50:26.308>
closer = 'gvanrossum'
components = ['Library (Lib)']
creation = <Date 2022-01-27.20:04:08.321>
creator = 'GBeauregard'
dependencies = []
files = []
hgrepos = []
issue_num = 46552
keywords = []
message_count = 5.0
messages = ['411914', '411915', '411920', '411925', '411931']
nosy_count = 7.0
nosy_names = ['gvanrossum', 'eric.smith', 'JelleZijlstra', 'sobolevn', 'kj', 'AlexWaygood', 'GBeauregard']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue46552'
versions = ['Python 3.9', 'Python 3.10', 'Python 3.11']

Activity

  1. GBeauregard commented on Jan 27, 2022

    GBeauregardmannequin
    MannequinAuthor
    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:

    ' typing.ClassVar[str]',

    The relevant failing code in typing.py is here:
    code = compile(arg, '<string>', 'eval')

    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.

  2. added
    3.11only security fixes
    stdlibStandard Library Python modules in the Lib/ directory
    type-bugAn unexpected behavior, bug, or error
    on Jan 27, 2022
  3. JelleZijlstra commented on Jan 27, 2022

    @JelleZijlstra
    Member

    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.

  4. ericvsmith commented on Jan 27, 2022

    @ericvsmith
    Member

    The dataclasses tests were in fact just added for completeness. I'd be okay with changing dataclasses to reject leading spaces.

  5. gvanrossum commented on Jan 27, 2022

    @gvanrossum
    Member

    We should not accept leading spaces.--
    --Guido (mobile)

  6. AlexWaygood commented on Jan 27, 2022

    @AlexWaygood
    Member

    I agree with Jelle and Guido

  7. transferred this issue fromon Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.10 (EOL)end of life3.11only security fixes3.9 (EOL)end of lifestdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions