Skip to content

Sphinx throws errors when using function in __getattr__ override #8917

Closed

Description

@maehster

Describe the bug
When using anonymous functions in getattr sphinx will raise an error:

Handler <function record_typehints at 0x7f3626d18790> for event 'autodoc-process-signature' threw an exception (exception: 'function' object has no attribute 'get')

To Reproduce

def fun():
    pass

class Tmp:

    def __call__(self, name, content):
        print(name, content)

    def __getattr__(self, item):
        return fun

a = Tmp()
"""asdfasdf"""
cd docs && \
make clean && \
sphinx-apidoc --force -e -o ./source ../<package> && \
make html

Expected behavior
No error.

Environment info

  • Python version: [3.8.5]

Sphinx==3.4.3
sphinx-rtd-theme==0.5.1
sphinxcontrib-applehelp==1.0.2
sphinxcontrib-devhelp==1.0.2
sphinxcontrib-htmlhelp==1.0.3
sphinxcontrib-jsmath==1.0.1
sphinxcontrib-qthelp==1.0.3
sphinxcontrib-serializinghtml==1.1.4

extensions = ['sphinx.ext.todo', 'sphinx.ext.viewcode', 'sphinx.ext.autodoc', 'sphinx.ext.napoleon']

Activity

  1. added a commit that references this issue on Feb 23, 2021
    3da4e5d
  2. tk0miya commented on Feb 23, 2021

    @tk0miya
    Member

    Thank you for reporting. I posted #8923 to fix the warning. But it could not build the document for the function because inspect.signature() raises an error for the function-like object:

    $  python
    Python 3.9.1 (default, Dec 12 2020, 22:52:49)
    [Clang 11.0.3 (clang-1103.0.32.62)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import example
    >>> dir(example)
    ['Tmp', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'a', 'fun']
    >>> example.a
    <example.Tmp object at 0x10d3e3ee0>
    >>> import inspect
    >>> inspect.signature(example.a)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      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 2290, in _signature_from_callable
        raise TypeError(
    TypeError: unexpected object <function fun at 0x10d3f71f0> in __signature__ attribute
    

    To resolve the error, please set None to a.__signature__.

  3. added this to the 4.0.0 milestone on Feb 23, 2021
  4. tk0miya commented on Feb 23, 2021

    @tk0miya
    Member

    Note: Internally, the warning is emitted because the given function returns an invalid value from its __globals__ attribute. So you can avoid the error by setting an empty dictionary to the function-like object additionally.

  5. maehster commented on Feb 25, 2021

    @maehster
    Author

    Thank you very much for the fast response and support :) I will try this and let you know if I have further questions.

  6. added 2 commits that reference this issue on Feb 25, 2021
    4f072ee
    529386f
  7. added a commit that references this issue on Mar 11, 2021
    3134085
  8. locked as resolved and limited conversation to collaborators on Jul 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions