Skip to content

Inferred Optional type of wrapper function arguments #775

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

Closed
wyfo opened this issue Jan 12, 2021 · 2 comments
Closed

Inferred Optional type of wrapper function arguments #775

wyfo opened this issue Jan 12, 2021 · 2 comments

Comments

@wyfo
Copy link

wyfo commented Jan 12, 2021

typing.get_type_hints gives a different result for a wrapper created with functools.wraps in case of inferred Optional arguments (when the default value of the argument is None)

from functools import wraps
from typing import get_type_hints

def foo(bar: int = None): ...

@wraps(foo)
def foo2(*args, **kwargs): ...

print(get_type_hints(foo))  # {'bar': typing.Optional[int]}
print(get_type_hints(foo2))  # {'bar': <class 'int'>}

This is because get_type_hints use the defauts of the wrapper (foo2) and not those of the wrapped function (foo).
This is not consistent with some other tools like inspect.signature which gives the same signature (and thus same default argument) for the wrapped function and its wrapper.

I think this case has simply been forgotten in the resolution of https://bugs.python.org/issue37838 (fixing get_type_hints not taking wraps in account at all), because inferred Optional is a kind deprecated feature (#275).

@gvanrossum
Copy link
Member

Regardless of the merits of the issue, I think this is the wrong tracker — typing.py issues should be filed in bpo.

@wyfo
Copy link
Author

wyfo commented Jan 13, 2021

Sorry, i will use the right tracker.

@wyfo wyfo closed this as completed Jan 13, 2021
wyfo added a commit to wyfo/apischema that referenced this issue May 22, 2021
See python/typing#775 about get_type_hints not giving Optional type to wrapped function with None-defaulted parameters
wyfo added a commit to wyfo/apischema that referenced this issue May 22, 2021
See python/typing#775 about get_type_hints not giving Optional type to wrapped function with None-defaulted parameters
francomattar pushed a commit to francomattar/apischema-benchmark-django-expert that referenced this issue Sep 16, 2022
See python/typing#775 about get_type_hints not giving Optional type to wrapped function with None-defaulted parameters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants