Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

inspect.signature() raises RuntimeError on failed to resolve the default argument value #87284

Closed
tk0miya mannequin opened this issue Feb 3, 2021 · 7 comments
Closed
Labels
3.9 only security fixes 3.10 only security fixes 3.11 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@tk0miya
Copy link
Mannequin

tk0miya mannequin commented Feb 3, 2021

BPO 43118
Nosy @gvanrossum, @hongweipeng, @miss-islington, @tk0miya
PRs
  • bpo-43118: in inspect.signature,use 'base' to help parse 'base.__text_signature__' #30285
  • [3.10] bpo-43118: Fix bug in inspect.signature around 'base.__text_signature__' (GH-30285) #30765
  • [3.9] bpo-43118: Fix bug in inspect.signature around 'base.__text_signature__' (GH-30285) #30766
  • Files
  • inspect.patch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = None
    created_at = <Date 2021-02-03.15:04:12.206>
    labels = ['type-bug', 'library', '3.9', '3.10', '3.11']
    title = 'inspect.signature() raises RuntimeError on failed to resolve the default argument value'
    updated_at = <Date 2022-01-22.17:28:57.262>
    user = 'https://github.com/tk0miya'

    bugs.python.org fields:

    activity = <Date 2022-01-22.17:28:57.262>
    actor = 'gvanrossum'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2021-02-03.15:04:12.206>
    creator = 'tkomiya'
    dependencies = []
    files = ['49788']
    hgrepos = []
    issue_num = 43118
    keywords = ['patch']
    message_count = 5.0
    messages = ['386212', '386213', '411188', '411196', '411268']
    nosy_count = 4.0
    nosy_names = ['gvanrossum', 'hongweipeng', 'miss-islington', 'tkomiya']
    pr_nums = ['30285', '30765', '30766']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue43118'
    versions = ['Python 3.9', 'Python 3.10', 'Python 3.11']

    @tk0miya
    Copy link
    Mannequin Author

    tk0miya mannequin commented Feb 3, 2021

    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.

    @tk0miya tk0miya mannequin added 3.7 (EOL) end of life 3.8 only security fixes 3.10 only security fixes 3.9 only security fixes stdlib Python modules in the Lib dir labels Feb 3, 2021
    @tk0miya
    Copy link
    Mannequin Author

    tk0miya mannequin commented Feb 3, 2021

    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)
    

    @iritkatriel iritkatriel added 3.11 only security fixes type-bug An unexpected behavior, bug, or error and removed 3.7 (EOL) end of life 3.8 only security fixes labels Jan 18, 2022
    @gvanrossum
    Copy link
    Member

    New changeset 881a763 by Weipeng Hong in branch 'main':
    bpo-43118: Fix bug in inspect.signature around 'base.__text_signature__' (GH-30285)
    881a763

    @miss-islington
    Copy link
    Contributor

    New changeset 9e3ff82 by Miss Islington (bot) in branch '3.9':
    bpo-43118: Fix bug in inspect.signature around 'base.__text_signature__' (GH-30285)
    9e3ff82

    @gvanrossum
    Copy link
    Member

    New changeset 83aef4d by Miss Islington (bot) in branch '3.10':
    bpo-43118: Fix bug in inspect.signature around 'base.__text_signature__' (GH-30285) (bpo-30765)
    83aef4d

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    @zhuofeng6
    Copy link

    any question about this issue? if not, i think it is better to close it

    @gvanrossum
    Copy link
    Member

    Looks like this was fixed in 3.9, the repro doesn't raise there.

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.9 only security fixes 3.10 only security fixes 3.11 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    4 participants