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

get_annotation_class_name() not always returning string #220

Open
cas-- opened this issue Feb 15, 2022 · 1 comment
Open

get_annotation_class_name() not always returning string #220

cas-- opened this issue Feb 15, 2022 · 1 comment

Comments

@cas--
Copy link

cas-- commented Feb 15, 2022

We encountered the following error while setting a return type which mocked in Sphinx config using a custom Mock object

Handler <function process_docstring at 0x7f6c16c8ec10> for event 'autodoc-process-docstring' threw an exception (exception: getattr(): attribute name must be string)

The actual exception is raised in get_annotation_args in this line due to a class_name not being str:

original = getattr(sys.modules[module], class_name)

Looking further into PR #145 was opened to track this issue as they also found that get_annotation_class_name is not always retuning a str but it seems was never fixed.

A quick example to show how the custom Mock does not return str for __qualname__ or _name attributes:

>>> class Mock:
    __all__ = []
    def __init__(self, *args, **kwargs):
        pass
    def __call__(self, *args, **kwargs):
        return ''
    @classmethod
    def __getattr__(cls, name):
        return Mock()
    def __add__(self, other):
        return other
    def __or__(self, __):
        return Mock()
>> Mock()
<__main__.Mock object at 0x7f22528baf40>
>>> m =Mock()
>>> m.__qualname__
<class '__main__.__qualname__'>
>>> m._name
<class '__main__._name'>
>>> getattr(m, m.__qualname__)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: getattr(): attribute name must be string

We can workaround the issue by not using the custom mock any more but it is still an inconvenience

cas-- added a commit to cas--/Deluge that referenced this issue Feb 15, 2022
If a libtorrent return type was specified e.g.

   def get_lt_status(self) -> 'lt.torrent_status'

Even as a string autodoc_typehints module would raise and error:

    Handler <function process_docstring at 0x7f6c16c8ec10> for event 'autodoc-process-docstring' threw an exception (exception: getattr(): attribute name must be string)

This was a result of using a custom mock in Sphinx autodoc config and
this Mock object name or qualname returns an object instead of str.

Testing with putting modules in autodoc_mock_imports again showed no
issues so removing custom mock

Ref: tox-dev/sphinx-autodoc-typehints#220
@gaborbernat
Copy link
Member

A PR to fix this would be welcome 👍

KtlTheBest pushed a commit to KtlTheBest/deluge that referenced this issue Feb 25, 2022
If a libtorrent return type was specified e.g.

   def get_lt_status(self) -> 'lt.torrent_status'

Even as a string autodoc_typehints module would raise and error:

    Handler <function process_docstring at 0x7f6c16c8ec10> for event 'autodoc-process-docstring' threw an exception (exception: getattr(): attribute name must be string)

This was a result of using a custom mock in Sphinx autodoc config and
this Mock object name or qualname returns an object instead of str.

Testing with putting modules in autodoc_mock_imports again showed no
issues so removing custom mock

Ref: tox-dev/sphinx-autodoc-typehints#220
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants