Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

typing: forward references don't understand special type forms #90697

Closed
GBeauregard mannequin opened this issue Jan 26, 2022 · 8 comments
Closed

typing: forward references don't understand special type forms #90697

GBeauregard mannequin opened this issue Jan 26, 2022 · 8 comments
Labels
3.9 only security fixes 3.10 only security fixes 3.11 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@GBeauregard
Copy link
Mannequin

GBeauregard mannequin commented Jan 26, 2022

BPO 46539
Nosy @gvanrossum, @JelleZijlstra, @miss-islington, @sobolevn, @Fidget-Spinner, @AlexWaygood, @GBeauregard
PRs
  • bpo-46539: Pass status of special typeforms to forward references #30926
  • [3.9] bpo-46539: Pass status of special typeforms to forward references (GH-30926) #30946
  • [3.10] bpo-46539: Pass status of special typeforms to forward references (GH-30926) #30947
  • 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.23:11:52.568>
    created_at = <Date 2022-01-26.20:30:38.627>
    labels = ['type-bug', 'library', '3.9', '3.10', '3.11']
    title = "typing: forward references don't understand special type forms"
    updated_at = <Date 2022-01-27.23:11:52.567>
    user = 'https://github.com/GBeauregard'

    bugs.python.org fields:

    activity = <Date 2022-01-27.23:11:52.567>
    actor = 'AlexWaygood'
    assignee = 'none'
    closed = True
    closed_date = <Date 2022-01-27.23:11:52.568>
    closer = 'AlexWaygood'
    components = ['Library (Lib)']
    creation = <Date 2022-01-26.20:30:38.627>
    creator = 'GBeauregard'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 46539
    keywords = ['patch']
    message_count = 8.0
    messages = ['411790', '411792', '411794', '411795', '411797', '411828', '411896', '411897']
    nosy_count = 7.0
    nosy_names = ['gvanrossum', 'JelleZijlstra', 'miss-islington', 'sobolevn', 'kj', 'AlexWaygood', 'GBeauregard']
    pr_nums = ['30926', '30946', '30947']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue46539'
    versions = ['Python 3.9', 'Python 3.10', 'Python 3.11']

    @GBeauregard
    Copy link
    Mannequin Author

    GBeauregard mannequin commented Jan 26, 2022

    Consider the following code on 3.11 main:

    from typing import Annotated, ClassVar, get_type_hints
    
    class DC:
        a: ClassVar[int] = 3
        b: ClassVar["int"] = 3
        c: "ClassVar[int]" = 3
        d: Annotated[ClassVar[int], (2, 5)] = 3
        e: Annotated[ClassVar["int"], (2, 5)] = 3
        f: "Annotated[ClassVar[int], (2, 5)]" = 3
    
    class DC_Special_ForwardRef:
        g: Annotated["ClassVar[int]", (] = 3
    
    # OK
    assert get_type_hints(DC, globals(), locals()) == {
        "a": ClassVar[int],
        "b": ClassVar[int],
        "c": ClassVar[int],
        "d": ClassVar[int],
        "e": ClassVar[int],
        "f": ClassVar[int],
    }
    
    # TypeError: typing.ClassVar[int] is not valid as type argument
    get_type_hints(DC_Special_ForwardRef, globals(), locals())
    

    Currently, the Annotated["ClassVar[int]", (2, 5)] annotation raises at runtime when get_type_hints is called, but all the other forward reference annotations are okay.

    My understanding is this is because when typing._type_check runs on a type where special forms are allowed it's possible for the typing._type_convert it calls it itself run a typing._type_check on contained forward references. However, if that forward reference was itself a special form then it's possible to get an error because typing._type_check doesn't pass on that special forms are allowed.

    I have drafted a patch to pass on this information. This will become important in the future as more special forms are allowed to wrap each other, such as allowing Final and ClassVar to nest each other in dataclasses, or when Required and NotRequired land. In the future we may also want to reconsider runtime restrictions on special forms in typing.py entirely and instead choose to leave this to type checkers.

    Is my analysis/patch approach okay? Forward references can be tricky. Should we be discussing runtime restrictions in typing.py more generally in the future?

    @GBeauregard GBeauregard mannequin added 3.9 only security fixes 3.10 only security fixes 3.11 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Jan 26, 2022
    @GBeauregard
    Copy link
    Mannequin Author

    GBeauregard mannequin commented Jan 26, 2022

    typo: the line is g: Annotated["ClassVar[int]", (2, 5)] = 3 in the code sample. Somehow I left it at only ( for the annotation instead of (2,5)

    @gvanrossum
    Copy link
    Member

    I wonder if this is at all similar to bpo-41370.

    @GBeauregard
    Copy link
    Mannequin Author

    GBeauregard mannequin commented Jan 26, 2022

    I did try the proposed patch in bpo-41370 and verified it didn't resolve the issue so I'm not certain they strictly overlap, but I also haven't had time to fully digest the underlying issues in bpo-41370 yet.

    I think it does have relevance for changes we want to make for dataclasses re: Annotated, though: https://bugs.python.org/issue46511

    @gvanrossum
    Copy link
    Member

    Agree it's not the same issue, but there's similarity -- both are due to putting a stringized annotation (presumably a forward ref) somewhere inside another construct. whether it's list["N"] or Annotated["ClassVar[int]"].

    @gvanrossum
    Copy link
    Member

    New changeset ced5005 by Gregory Beauregard in branch 'main':
    bpo-46539: Pass status of special typeforms to forward references (GH-30926)
    ced5005

    @miss-islington
    Copy link
    Contributor

    New changeset 3757703 by Miss Islington (bot) in branch '3.9':
    bpo-46539: Pass status of special typeforms to forward references (GH-30926)
    3757703

    @miss-islington
    Copy link
    Contributor

    New changeset bfcb414 by Miss Islington (bot) in branch '3.10':
    bpo-46539: Pass status of special typeforms to forward references (GH-30926)
    bfcb414

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.9 only security fixes 3.10 only security fixes 3.11 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants