Skip to content

Commit

Permalink
[Docs] Remove custom mock to fix autodoc typing errors
Browse files Browse the repository at this point in the history
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
  • Loading branch information
cas-- authored and marik committed Feb 25, 2022
1 parent 0351ef8 commit c8b81c3
Showing 1 changed file with 1 addition and 33 deletions.
34 changes: 1 addition & 33 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,45 +219,13 @@

# Autodoc section
# ---------------
class Mock:

__all__ = []

def __init__(self, *args, **kwargs):
pass

def __call__(self, *args, **kwargs):
return ''

@classmethod
def __getattr__(cls, name):
if name in ('__file__', '__path__', 'xdg_config_home'):
return '/dev/null'
elif name[0] == name[0].upper():
mock_type = type(name, (), {})
mock_type.__module__ = __name__
return mock_type
else:
return Mock()

def __add__(self, other):
return other

def __or__(self, __):
return Mock()


# Use custom mock as autodoc_mock_imports fails to handle these modules.
MOCK_MODULES = ['deluge._libtorrent', 'xdg', 'xdg.BaseDirectory']

for mod_name in MOCK_MODULES:
sys.modules[mod_name] = Mock()

# Must add these for autodoc to import packages successfully
builtins.__dict__['_'] = lambda x: x
builtins.__dict__['_n'] = lambda s, p, n: s if n == 1 else p

autodoc_mock_imports = [
'deluge._libtorrent',
'twisted',
'rencode',
'OpenSSL',
Expand Down

0 comments on commit c8b81c3

Please sign in to comment.