[issue42921] Inferred Optional type of wrapper function arguments

Joseph Perez report at bugs.python.org
Wed Jan 13 02:48:07 EST 2021


New submission from Joseph Perez <joperez at hotmail.fr>:

`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)

```python
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 (https://github.com/python/typing/issues/275).

----------
messages: 385005
nosy: joperez
priority: normal
severity: normal
status: open
title: Inferred Optional type of wrapper function arguments
type: behavior

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue42921>
_______________________________________


More information about the Python-bugs-list mailing list