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

get_type_hints + Final + future annotations = TypeError #89329

Closed
pawamoy mannequin opened this issue Sep 10, 2021 · 7 comments
Closed

get_type_hints + Final + future annotations = TypeError #89329

pawamoy mannequin opened this issue Sep 10, 2021 · 7 comments
Labels
3.8 only security fixes 3.9 only security fixes 3.10 only security fixes 3.11 only security fixes type-bug An unexpected behavior, bug, or error

Comments

@pawamoy
Copy link
Mannequin

pawamoy mannequin commented Sep 10, 2021

BPO 45166
Nosy @ambv, @miss-islington, @sobolevn, @pawamoy
PRs
  • bpo-45166: fixes get_type_hints failure on Final #28279
  • [3.10] bpo-45166: fixes get_type_hints failure on Final (GH-28279) #28560
  • [3.9] bpo-45166: fixes get_type_hints failure on Final (GH-28279) #28561
  • 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 2021-09-25.09:32:02.812>
    created_at = <Date 2021-09-10.16:19:49.314>
    labels = ['type-bug', '3.8', '3.9', '3.10', '3.11']
    title = 'get_type_hints + Final + future annotations = TypeError'
    updated_at = <Date 2021-09-25.09:32:02.811>
    user = 'https://github.com/pawamoy'

    bugs.python.org fields:

    activity = <Date 2021-09-25.09:32:02.811>
    actor = 'lukasz.langa'
    assignee = 'none'
    closed = True
    closed_date = <Date 2021-09-25.09:32:02.812>
    closer = 'lukasz.langa'
    components = []
    creation = <Date 2021-09-10.16:19:49.314>
    creator = 'pawamoy'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 45166
    keywords = ['patch']
    message_count = 7.0
    messages = ['401590', '401592', '401594', '402610', '402612', '402613', '402614']
    nosy_count = 4.0
    nosy_names = ['lukasz.langa', 'miss-islington', 'sobolevn', 'pawamoy']
    pr_nums = ['28279', '28560', '28561']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue45166'
    versions = ['Python 3.8', 'Python 3.9', 'Python 3.10', 'Python 3.11']

    @pawamoy
    Copy link
    Mannequin Author

    pawamoy mannequin commented Sep 10, 2021

    This is my first issue on bugs.python.org, let me know if I can improve it in any way.

    ---

    Originally reported and investigated here: mkdocstrings/mkdocstrings#314

    # final.py
    from __future__ import annotations
    
    from typing import Final, get_type_hints
    
    name: Final[str] = "final"
    
    class Class:
        value: Final = 3000
    
    get_type_hints(Class)
    

    Run python final.py, and you'll get this traceback:

    Traceback (most recent call last):
      File "final.py", line 11, in <module>
        get_type_hints(Class)
      File "/.../lib/python3.8/typing.py", line 1232, in get_type_hints
        value = _eval_type(value, base_globals, localns)
      File "/.../lib/python3.8/typing.py", line 270, in _eval_type
        return t._evaluate(globalns, localns)
      File "/.../lib/python3.8/typing.py", line 517, in _evaluate
        self.__forward_value__ = _type_check(
      File "/.../lib/python3.8/typing.py", line 145, in _type_check
        raise TypeError(f"Plain {arg} is not valid as type argument")
    TypeError: Plain typing.Final is not valid as type argument
    

    Now comment the get_type_hints call, launch a Python interpreter, and enter these commands:

    >> import final
    >> from typing import get_type_hints
    >> get_type_hints(final)

    And you'll get this traceback:

    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/.../lib/python3.9/typing.py", line 1449, in get_type_hints
        value = _eval_type(value, globalns, localns)
      File "/.../lib/python3.9/typing.py", line 283, in _eval_type
        return t._evaluate(globalns, localns, recursive_guard)
      File "/.../lib/python3.9/typing.py", line 538, in _evaluate
        type_ =_type_check(
      File "/.../lib/python3.9/typing.py", line 149, in _type_check
        raise TypeError(f"{arg} is not valid as type argument")
    TypeError: typing.Final[str] is not valid as type argument
    

    I was able to replicate in 3.8, 3.9 and 3.10. The annotations future is not available in 3.7 or lower. Didn't try on 3.11.

    @pawamoy pawamoy mannequin added 3.10 only security fixes 3.8 only security fixes 3.9 only security fixes type-bug An unexpected behavior, bug, or error labels Sep 10, 2021
    @pawamoy
    Copy link
    Mannequin Author

    pawamoy mannequin commented Sep 10, 2021

    Just tried on 3.11-dev, it's affected as well.

    This issue seems related to https://bugs.python.org/issue41249.

    @pawamoy pawamoy mannequin added 3.11 only security fixes labels Sep 10, 2021
    @pawamoy
    Copy link
    Mannequin Author

    pawamoy mannequin commented Sep 10, 2021

    Oh, I see now that there is a python/typing/ repository on GitHub. Let me know if I should open there instead.

    @ambv
    Copy link
    Contributor

    ambv commented Sep 25, 2021

    New changeset 784905d by Nikita Sobolev in branch 'main':
    bpo-45166: fixes get_type_hints failure on Final (GH-28279)
    784905d

    @ambv
    Copy link
    Contributor

    ambv commented Sep 25, 2021

    New changeset d312b85 by Miss Islington (bot) in branch '3.10':
    bpo-45166: fixes get_type_hints failure on Final (GH-28279) (GH-28560)
    d312b85

    @ambv
    Copy link
    Contributor

    ambv commented Sep 25, 2021

    New changeset 1f08d16 by Łukasz Langa in branch '3.9':
    [3.9] bpo-45166: fixes get_type_hints failure on Final (GH-28279) (GH-28561)
    1f08d16

    @ambv
    Copy link
    Contributor

    ambv commented Sep 25, 2021

    Thanks for the report Timothee, and Nikita for the fix! ✨ 🍰 ✨

    @ambv ambv closed this as completed Sep 25, 2021
    @ambv ambv closed this as completed Sep 25, 2021
    @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.8 only security fixes 3.9 only security fixes 3.10 only security fixes 3.11 only security fixes type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant