New submission from Komiya Takeshi <i.tkom...@gmail.com>:

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.

----------
components: Library (Lib)
files: inspect.patch
keywords: patch
messages: 386212
nosy: i.tkomiya
priority: normal
severity: normal
status: open
title: inspect.signature() raises RuntimeError on failed to resolve the default 
argument value
versions: Python 3.10, Python 3.6, Python 3.7, Python 3.8, Python 3.9
Added file: https://bugs.python.org/file49788/inspect.patch

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue43118>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to