This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Inferred Optional type of wrapper function arguments
Type: behavior Stage:
Components: Versions:
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: joperez
Priority: normal Keywords:

Created on 2021-01-13 07:48 by joperez, last changed 2022-04-11 14:59 by admin.

Messages (1)
msg385005 - (view) Author: Joseph Perez (joperez) * Date: 2021-01-13 07:48
`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): ...

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 (fixing `get_type_hints` not taking `wraps` in account at all), because inferred `Optional` is a kind deprecated feature (
Date User Action Args
2022-04-11 14:59:40adminsetgithub: 87087
2021-01-13 07:48:07joperezcreate