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.

classification
Title: inspect.signature() raises RuntimeError on failed to resolve the default argument value
Type: behavior Stage: patch review
Components: Library (Lib) Versions: Python 3.11, Python 3.10, Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: gvanrossum, hongweipeng, miss-islington, tkomiya
Priority: normal Keywords: patch

Created on 2021-02-03 15:04 by tkomiya, last changed 2022-04-11 14:59 by admin.

Files
File name Uploaded Description Edit
inspect.patch tkomiya, 2021-02-03 15:04
Pull Requests
URL Status Linked Edit
PR 30285 merged hongweipeng, 2021-12-29 06:48
PR 30765 merged miss-islington, 2022-01-21 21:24
PR 30766 merged miss-islington, 2022-01-21 21:24
Messages (5)
msg386212 - (view) Author: Komiya Takeshi (tkomiya) * Date: 2021-02-03 15:04
inspect.signature() raises RuntimeError on failed to resolve the default argument value. For example, it fails to inspect a subclass of io.BufferedReader:

Example:
```
import inspect
import io


class MyBufferedReader(io.BufferedReader):
    """buffer reader class."""


inspect.signature(MyBufferedReader)
```

Result:
```
Traceback (most recent call last):
  File "/Users/tkomiya/.pyenv/versions/3.9.1/lib/python3.9/inspect.py", line 2042, in wrap_value
    value = eval(s, module_dict)
  File "<string>", line 1, in <module>
NameError: name 'DEFAULT_BUFFER_SIZE' is not defined

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/tkomiya/.pyenv/versions/3.9.1/lib/python3.9/inspect.py", line 2045, in wrap_value
    value = eval(s, sys_module_dict)
  File "<string>", line 1, in <module>
NameError: name 'DEFAULT_BUFFER_SIZE' is not defined

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/tkomiya/work/tmp/doc/example.py", line 9, in <module>
    inspect.signature(MyBufferedReader)
  File "/Users/tkomiya/.pyenv/versions/3.9.1/lib/python3.9/inspect.py", line 3130, in signature
    return Signature.from_callable(obj, follow_wrapped=follow_wrapped)
  File "/Users/tkomiya/.pyenv/versions/3.9.1/lib/python3.9/inspect.py", line 2879, in from_callable
    return _signature_from_callable(obj, sigcls=cls,
  File "/Users/tkomiya/.pyenv/versions/3.9.1/lib/python3.9/inspect.py", line 2397, in _signature_from_callable
    return _signature_fromstr(sigcls, obj, text_sig)
  File "/Users/tkomiya/.pyenv/versions/3.9.1/lib/python3.9/inspect.py", line 2095, in _signature_fromstr
    p(name, default)
  File "/Users/tkomiya/.pyenv/versions/3.9.1/lib/python3.9/inspect.py", line 2077, in p
    default_node = RewriteSymbolics().visit(default_node)
  File "/Users/tkomiya/.pyenv/versions/3.9.1/lib/python3.9/ast.py", line 407, in visit
    return visitor(node)
  File "/Users/tkomiya/.pyenv/versions/3.9.1/lib/python3.9/inspect.py", line 2069, in visit_Name
    return wrap_value(node.id)
  File "/Users/tkomiya/.pyenv/versions/3.9.1/lib/python3.9/inspect.py", line 2047, in wrap_value
    raise RuntimeError()
RuntimeError
```


In my investigation, inspect.signature() tries to evaluate the default argument value of the class constructor. But it fails to evaluate because of the 2nd argument of the constructor takes a constant; `DEFAULT_BUFFER_SIZE`, but it is not found on the current context.

I think it would be better to search the constants for the modules of the base classes. I just made a simple patch to resolve this bug.
msg386213 - (view) Author: Komiya Takeshi (tkomiya) * Date: 2021-02-03 15:07
FWIW, I succeeded to inspect the class with importing the constant from the base module as a workaround:

```
import inspect
import io

from io import DEFAULT_BUFFER_SIZE


class MyBufferedReader(io.BufferedReader):
    """buffer reader class."""


inspect.signature(MyBufferedReader)
```
msg411188 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2022-01-21 21:24
New changeset 881a763cfe07ef4a5806ec78f13a9bc99e8909dc by Weipeng Hong in branch 'main':
bpo-43118: Fix bug in inspect.signature around 'base.__text_signature__' (GH-30285)
https://github.com/python/cpython/commit/881a763cfe07ef4a5806ec78f13a9bc99e8909dc
msg411196 - (view) Author: miss-islington (miss-islington) Date: 2022-01-21 22:06
New changeset 9e3ff821dac05e8fde030ec83bd988f3eba66065 by Miss Islington (bot) in branch '3.9':
bpo-43118: Fix bug in inspect.signature around 'base.__text_signature__' (GH-30285)
https://github.com/python/cpython/commit/9e3ff821dac05e8fde030ec83bd988f3eba66065
msg411268 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2022-01-22 17:28
New changeset 83aef4d34022f293336f606dba8598cc7ac8f9f2 by Miss Islington (bot) in branch '3.10':
bpo-43118: Fix bug in inspect.signature around 'base.__text_signature__' (GH-30285) (#30765)
https://github.com/python/cpython/commit/83aef4d34022f293336f606dba8598cc7ac8f9f2
History
Date User Action Args
2022-04-11 14:59:41adminsetgithub: 87284
2022-01-22 17:28:57gvanrossumsetmessages: + msg411268
2022-01-21 22:06:42miss-islingtonsetmessages: + msg411196
2022-01-21 21:24:45miss-islingtonsetpull_requests: + pull_request28952
2022-01-21 21:24:41miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request28951
2022-01-21 21:24:40gvanrossumsetnosy: + gvanrossum
messages: + msg411188
2022-01-18 23:08:46iritkatrielsettype: behavior
versions: + Python 3.11, - Python 3.6, Python 3.7, Python 3.8
2021-12-29 06:48:51hongweipengsetnosy: + hongweipeng

pull_requests: + pull_request28499
stage: patch review
2021-02-03 15:07:05tkomiyasetmessages: + msg386213
2021-02-03 15:04:12tkomiyacreate