[issue43006] Changed behaviour of inspect.signature() in Python 3.10

Ken Jin report at bugs.python.org
Sat Jan 23 04:56:07 EST 2021


Ken Jin <kenjin4096 at gmail.com> added the comment:

> This is correct for typing.get_type_hints(), but *not* for inspect.signature(). I therefore suspect that this is an accidental side-effect from support for PEP-563 deferred evaluation of annotations.

Spot on about PEP 563 becoming the default being the culprit!

The what's new for Python 3.10 states that inspect.signature will try to resolve types https://docs.python.org/3.10/whatsnew/3.10.html#pep-563-postponed-evaluation-of-annotations-becomes-default.

For annotations, inspect.signature internally calls get_type_hints: https://github.com/python/cpython/blob/master/Lib/inspect.py#L2194

A simple solution I can think of is to add a flag to get_type_hints to disable converting to Optional[int] for inspect.signature.

I nosied some people involved in issue38605 so that they can give their opinions on this.

----------
nosy: +BTaskaya, gvanrossum, kj

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


More information about the Python-bugs-list mailing list