You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Sphinx throws errors when using function in __getattr__ override #8917
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
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__.
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.
Activity
Fix sphinx-doc#8917: autodoc: Raises a warning if function has wrong …
tk0miya commentedon Feb 23, 2021
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:To resolve the error, please set
Nonetoa.__signature__.tk0miya commentedon Feb 23, 2021
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.maehster commentedon Feb 25, 2021
Thank you very much for the fast response and support :) I will try this and let you know if I have further questions.
Fix sphinx-doc#8917: autodoc: Raises a warning if function has wrong …
Merge pull request #8923 from tk0miya/8917_warning_for_wrong___globals__
Master.patch (#1)