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.get_type_hints not working with forward-declaration and decorated functions #82019

Closed
netbnd mannequin opened this issue Aug 13, 2019 · 16 comments
Closed

typing.get_type_hints not working with forward-declaration and decorated functions #82019

netbnd mannequin opened this issue Aug 13, 2019 · 16 comments
Labels
3.7 (EOL) end of life easy stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@netbnd
Copy link
Mannequin

netbnd mannequin commented Aug 13, 2019

BPO 37838
Nosy @gvanrossum, @ilevkivskyi, @miss-islington, @nanjekyejoannah, @benedwards14
PRs
  • bpo-37838: get_type_hints for wrapped functions with forward reference #17126
  • [3.8] bpo-37838: get_type_hints for wrapped functions with forward reference (GH-17126) #17324
  • [3.7] bpo-37838: get_type_hints for wrapped functions with forward reference (GH-17126) #17325
  • Files
  • typing_check_wrapped.zip: Sample code with working and non working cases is attached to reproduce the issue.
  • 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 2019-11-21.17:47:38.989>
    created_at = <Date 2019-08-13.10:42:59.139>
    labels = ['3.7', 'easy', 'type-bug', 'library']
    title = 'typing.get_type_hints not working with forward-declaration and decorated functions'
    updated_at = <Date 2019-11-21.17:47:38.988>
    user = 'https://bugs.python.org/netbnd'

    bugs.python.org fields:

    activity = <Date 2019-11-21.17:47:38.988>
    actor = 'levkivskyi'
    assignee = 'none'
    closed = True
    closed_date = <Date 2019-11-21.17:47:38.989>
    closer = 'levkivskyi'
    components = ['Library (Lib)']
    creation = <Date 2019-08-13.10:42:59.139>
    creator = 'netbnd'
    dependencies = []
    files = ['48540']
    hgrepos = []
    issue_num = 37838
    keywords = ['patch', 'newcomer friendly']
    message_count = 16.0
    messages = ['349542', '349565', '349580', '349581', '349594', '355477', '355501', '355502', '355585', '355643', '355747', '356390', '356470', '357184', '357187', '357188']
    nosy_count = 6.0
    nosy_names = ['gvanrossum', 'levkivskyi', 'miss-islington', 'nanjekyejoannah', 'netbnd', 'benedwards14']
    pr_nums = ['17126', '17324', '17325']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue37838'
    versions = ['Python 3.6', 'Python 3.7']

    @netbnd
    Copy link
    Mannequin Author

    netbnd mannequin commented Aug 13, 2019

    When decorating a function and using a forward declaration as type hint, the typing.get_type_hints function does not work anymore, since it cannot find the forward declared name in the namespace. After debugging I think, the typing.get_type_hints function is actually using the namespace of the decorator instead of the decorated function.

    When using a normal class type (no forward declaration) everything works fine and also when not using any decorator it works like expected.

    As a workaround, one could pass the local namespace to typing.get_type_hints. However in normal usecases this function is used for runtime typechecking in a deep call hierarchy. So one would normally not have access to the right local namespace, only to the function object itself.

    However there is another easy workaround. At least when using the functool.wraps method to create a function decorator. The decorated functions has a "__wrapped__" attribute, which references the original function. When using "typing.get_type_hints(function.__wrapped__)" instead of "typing.get_type_hints(function)", it works like expected. So maybe this could be built in into the get_type_hints method.

    @netbnd netbnd mannequin added 3.7 (EOL) end of life stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Aug 13, 2019
    @gvanrossum
    Copy link
    Member

    Using __wrapped__ if present sounds like a good idea. Ivan, what do you think?

    Netzeband, have you considered submitting a PR with the necessary changes (and tests and docs)?

    @ilevkivskyi
    Copy link
    Member

    Using __wrapped__ if present sounds like a good idea.

    Yeah, I like this idea, this will likely cover most use cases (since most people actually do use @wraps).

    @gvanrossum
    Copy link
    Member

    Maybe we should tag this issue "newcomer friendly"? It seems a pretty open
    and shut case.

    @netbnd
    Copy link
    Mannequin Author

    netbnd mannequin commented Aug 13, 2019

    Thanks for your feedback. I can create a pull-request. However, I did it never before for the Python-Lib, so I first have to setup the correct environment for that and ensure, that I can run all test-cases.

    Since I'm on vacation soon, it could take several weeks. If someone wants to solve the issue earlier, please don't hesitate.

    I will write it in this issue ticket, as soon as I finished the PR.

    @benedwards14
    Copy link
    Mannequin

    benedwards14 mannequin commented Oct 27, 2019

    Hey Netzeband, are you still working on this, or would it be ok for me to have a go?

    If all you needed was help with a PR let me know, i have some experience?

    @nanjekyejoannah
    Copy link
    Contributor

    @benedwards14 Feel free to open a PR. @netbnd said anyone can take on the issue since he may take long to revert with a PR.

    @nanjekyejoannah
    Copy link
    Contributor

    Actually there is a patch. So please @netbnd please open a PR https://github.com/python/cpython

    @netbnd
    Copy link
    Mannequin Author

    netbnd mannequin commented Oct 28, 2019

    Hello,

    I'm very sorry, I was not able to find the time to finish the patch in the quality I wanted to have. So @benedwards14 please feel free to provide a pull request.

    Best regards,
    André

    @benedwards14
    Copy link
    Mannequin

    benedwards14 mannequin commented Oct 29, 2019

    Thanks Netzeband,

    Will get on this.

    Ben

    @netbnd
    Copy link
    Mannequin Author

    netbnd mannequin commented Oct 31, 2019

    Thanks a lot!

    @ilevkivskyi
    Copy link
    Member

    The PR was linked by mistake (it is for a different issue), so I unlinked it.

    @benedwards14
    Copy link
    Mannequin

    benedwards14 mannequin commented Nov 12, 2019

    Have opened a PR, let me know if there is anything that needs fixing

    @ilevkivskyi
    Copy link
    Member

    New changeset 0aca3a3 by Ivan Levkivskyi (benedwards14) in branch 'master':
    bpo-37838: get_type_hints for wrapped functions with forward reference (GH-17126)
    0aca3a3

    @miss-islington
    Copy link
    Contributor

    New changeset 30e5bd8 by Miss Islington (bot) in branch '3.7':
    bpo-37838: get_type_hints for wrapped functions with forward reference (GH-17126)
    30e5bd8

    @miss-islington
    Copy link
    Contributor

    New changeset 9458c5c by Miss Islington (bot) in branch '3.8':
    bpo-37838: get_type_hints for wrapped functions with forward reference (GH-17126)
    9458c5c

    @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.7 (EOL) end of life easy stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    4 participants