Starting with Python 3.10, the form returned by inspect.signature () seems to be based on typing.get_type_hints ().

As I noticed today, the Python 3.10 inspect module now uses typing.get_type_hints () to interpret types. The corresponding commit is hereis.

This seems to be a fix related to lazy evaluation of annotations, which is enabled by default from Python 3.10. Since the type annotation information packed in ʻobj.annotationsbecomes a character string, it may be judged that it is appropriate to leave it totyping.get_type_hints (). With this change, the type information of ʻinspect.signature (), which simply referred to ʻobj.annotations`, is switched to the analysis result that goes one step further.

For example, Python 3.9 treats the following hello () function argument name as a str type.

$ python3.9
Python 3.9.0 (default, Oct 24 2020, 15:41:29)
[Clang 11.0.3 (clang-1103.0.32.59)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> def hello(name: str = None):
...     print("Hello ", name)
...
>>> import inspect
>>> inspect.signature(hello)
<Signature (name: str = None)>

On the other hand, in Python 3.10 (under development) it is considered to be of type ʻOptional [str]. Seeing that the default argument is None`, it returns a better type.

$ python3.10
Python 3.10.0a1+ (heads/master:805ef73, Oct 24 2020, 15:07:19)
[Clang 11.0.3 (clang-1103.0.32.59)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> def hello(name: str = None):
...     print("Hello ", name)
...
>>> import inspect
>>> inspect.signature(hello)
<Signature (name: Optional[str] = None)>

By the way, if the name cannot be resolved at runtime (such as when typing.TYPE_CHECKING is not imported at runtime), ʻobj.__ signature__` is still referenced, so the interpretation result may change depending on the annotation.

$ python3.10
Python 3.10.0a1+ (heads/master:805ef73, Oct 24 2020, 15:07:19)
[Clang 11.0.3 (clang-1103.0.32.59)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> def hello(name: Unknown, age: int = None): pass
...
>>> import inspect
>>> inspect.signature(hello)
<Signature (name: 'Unknown', age: 'int' = None)>

In this case, the ʻUnknown type cannot be resolved, so the type of the argument ʻage is not ʻOptional [int], but just ʻint.

Recommended Posts

Starting with Python 3.10, the form returned by inspect.signature () seems to be based on typing.get_type_hints ().
Introduction to Python with Atom (on the way)
Change the string to be replaced according to the matched string by replacing with Python regular expression
How to switch the configuration file to be read by Python
Save images on the web to Drive with Python (Colab)
[Python] I tried to visualize the night on the Galactic Railroad with WordCloud!
Add 95% confidence intervals on both sides to the diagram with Python / Matplotlib
Use python on Raspberry Pi 3 to light the LED with switch control!
I tried with the top 100 PyPI packages> I tried to graph the packages installed on Python
Address to the bug that node.surface cannot be obtained with python3 + mecab
Python 3.9 dict merge (`|`) seems to be useful
Download files on the web with Python
The road to compiling to Python 3 with Thrift
GAE --With Python, rotate the image based on the rotation information of EXIF and upload it to Cloud Storage.
[Python] How to save images on the Web at once with Beautiful Soup
It became possible to take screenshots by starting the smartphone (Android terminal) application with Python using Appium on Windows 10 and automating the screen transition operation.
I tried to make the weather forecast on the official line by referring to the weather forecast bot of "Dialogue system made with python".
The easiest way to synthesize speech with python
Try to solve the man-machine chart with Python
Specify the Python executable to use with virtualenv
Say hello to the world with Python with IntelliJ
[Road to Intermediate] Python seems to be all objects
The easiest way to use OpenCV with python
[Python] There seems to be something called __dict__
Connect to MySQL with Python on Raspberry Pi
Run the output code with tkinter, saying "A, pretending to be B" in python
System trade starting with Python3: Bio-health stocks to look at in the new Corona
Make it easy to install the ROS2 development environment with pip install on Python venv
Think about how to program Python on the iPad
Try to solve the programming challenge book with python3
[Introduction to Python] How to iterate with the range function?
I tried to implement Minesweeper on terminal with python
Try to solve the internship assignment problem with Python
The first algorithm to learn with Python: FizzBuzz problem
I tried to touch the CSV file with Python
Steps to install the latest Python on your Mac
I tried to solve the soma cube with python
Yum command to access MySQL with Python 3 on Linux
[Python] How to specify the download location with youtube-dl
Introduction to Mathematics Starting with Python Study Memo Vol.1
Make a breakpoint on the c layer with python
Read the xml file by referring to the Python tutorial
Convert the image in .zip to PDF with Python
I want to inherit to the back with python dataclass
Information for controlling the motor with Python on RaspberryPi
I want to AWS Lambda with Python on Mac!
Specify MinGW as the compiler to use with Python
I tried to solve the problem with Python Vol.1
[Python] How to rewrite the table style with python-pptx [python-pptx]
How to enjoy Python on Android !! Programming on the go !!
How to deal with the phenomenon that Python (Jupyter notebook) executed on WSL becomes Aborted
python> Is it possible to make in-line comments?> It seems that it has to be on multiple lines
I made a class to get the analysis result by MeCab in ndarray with python
How to calculate space background X-ray radiation (CXB) with python by specifying the flux range
How to know the number of GPUs from python ~ Notes on using multiprocessing with pytorch ~
What seems to be a template of the standard input part of the competition pro in python3
Try to import to the database by manipulating ShapeFile of national land numerical information with Python