mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Merge pull request #7862 from tk0miya/7844_relative_module_name
Fix #7844: autodoc: Failed to detect module when relative module name given
This commit is contained in:
commit
3ad8547c7c
2
CHANGES
2
CHANGES
@ -16,6 +16,8 @@ Features added
|
|||||||
Bugs fixed
|
Bugs fixed
|
||||||
----------
|
----------
|
||||||
|
|
||||||
|
* #7844: autodoc: Failed to detect module when relative module name given
|
||||||
|
|
||||||
Testing
|
Testing
|
||||||
--------
|
--------
|
||||||
|
|
||||||
|
@ -977,19 +977,29 @@ class ModuleLevelDocumenter(Documenter):
|
|||||||
if path:
|
if path:
|
||||||
stripped = path.rstrip('.')
|
stripped = path.rstrip('.')
|
||||||
modname, qualname = split_full_qualified_name(stripped)
|
modname, qualname = split_full_qualified_name(stripped)
|
||||||
|
if modname is None:
|
||||||
|
# if documenting a toplevel object without explicit module,
|
||||||
|
# it can be contained in another auto directive ...
|
||||||
|
modname = self.env.temp_data.get('autodoc:module')
|
||||||
|
# ... or in the scope of a module directive
|
||||||
|
if not modname:
|
||||||
|
modname = self.env.ref_context.get('py:module')
|
||||||
|
|
||||||
|
if modname:
|
||||||
|
stripped = ".".join([modname, stripped])
|
||||||
|
modname, qualname = split_full_qualified_name(stripped)
|
||||||
|
|
||||||
if qualname:
|
if qualname:
|
||||||
parents = qualname.split(".")
|
parents = qualname.split(".")
|
||||||
else:
|
else:
|
||||||
parents = []
|
parents = []
|
||||||
|
else:
|
||||||
if modname is None:
|
|
||||||
# if documenting a toplevel object without explicit module,
|
|
||||||
# it can be contained in another auto directive ...
|
|
||||||
modname = self.env.temp_data.get('autodoc:module')
|
modname = self.env.temp_data.get('autodoc:module')
|
||||||
# ... or in the scope of a module directive
|
parents = []
|
||||||
|
|
||||||
if not modname:
|
if not modname:
|
||||||
modname = self.env.ref_context.get('py:module')
|
modname = self.env.ref_context.get('py:module')
|
||||||
# ... else, it stays None, which means invalid
|
|
||||||
return modname, parents + [base]
|
return modname, parents + [base]
|
||||||
|
|
||||||
|
|
||||||
@ -1016,18 +1026,24 @@ class ClassLevelDocumenter(Documenter):
|
|||||||
if mod_cls is None:
|
if mod_cls is None:
|
||||||
return None, []
|
return None, []
|
||||||
|
|
||||||
try:
|
modname, qualname = split_full_qualified_name(mod_cls)
|
||||||
modname, qualname = split_full_qualified_name(mod_cls)
|
if modname is None:
|
||||||
parents = qualname.split(".") if qualname else []
|
# if documenting a toplevel object without explicit module,
|
||||||
except ImportError:
|
# it can be contained in another auto directive ...
|
||||||
parents = mod_cls.split(".")
|
|
||||||
|
|
||||||
# if the module name is still missing, get it like above
|
|
||||||
if not modname:
|
|
||||||
modname = self.env.temp_data.get('autodoc:module')
|
modname = self.env.temp_data.get('autodoc:module')
|
||||||
if not modname:
|
# ... or in the scope of a module directive
|
||||||
modname = self.env.ref_context.get('py:module')
|
if not modname:
|
||||||
# ... else, it stays None, which means invalid
|
modname = self.env.ref_context.get('py:module')
|
||||||
|
|
||||||
|
if modname:
|
||||||
|
stripped = ".".join([modname, mod_cls])
|
||||||
|
modname, qualname = split_full_qualified_name(stripped)
|
||||||
|
|
||||||
|
if qualname:
|
||||||
|
parents = qualname.split(".")
|
||||||
|
else:
|
||||||
|
parents = []
|
||||||
|
|
||||||
return modname, parents + [base]
|
return modname, parents + [base]
|
||||||
|
|
||||||
|
|
||||||
|
@ -634,7 +634,10 @@ def split_full_qualified_name(name: str) -> Tuple[str, str]:
|
|||||||
if not inspect.ismodule(value):
|
if not inspect.ismodule(value):
|
||||||
return ".".join(parts[:i]), ".".join(parts[i:])
|
return ".".join(parts[:i]), ".".join(parts[i:])
|
||||||
except ImportError:
|
except ImportError:
|
||||||
return ".".join(parts[:i - 1]), ".".join(parts[i - 1:])
|
if parts[:i - 1]:
|
||||||
|
return ".".join(parts[:i - 1]), ".".join(parts[i - 1:])
|
||||||
|
else:
|
||||||
|
return None, ".".join(parts)
|
||||||
except IndexError:
|
except IndexError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@ -121,15 +121,16 @@ def test_parse_name(app):
|
|||||||
verify('class', 'Base', ('test_ext_autodoc', ['Base'], None, None))
|
verify('class', 'Base', ('test_ext_autodoc', ['Base'], None, None))
|
||||||
|
|
||||||
# for members
|
# for members
|
||||||
directive.env.ref_context['py:module'] = 'foo'
|
directive.env.ref_context['py:module'] = 'sphinx.testing'
|
||||||
verify('method', 'util.SphinxTestApp.cleanup',
|
verify('method', 'util.SphinxTestApp.cleanup',
|
||||||
('foo', ['util', 'SphinxTestApp', 'cleanup'], None, None))
|
('sphinx.testing.util', ['SphinxTestApp', 'cleanup'], None, None))
|
||||||
directive.env.ref_context['py:module'] = 'util'
|
directive.env.ref_context['py:module'] = 'sphinx.testing.util'
|
||||||
directive.env.ref_context['py:class'] = 'Foo'
|
directive.env.ref_context['py:class'] = 'Foo'
|
||||||
directive.env.temp_data['autodoc:class'] = 'SphinxTestApp'
|
directive.env.temp_data['autodoc:class'] = 'SphinxTestApp'
|
||||||
verify('method', 'cleanup', ('util', ['SphinxTestApp', 'cleanup'], None, None))
|
verify('method', 'cleanup',
|
||||||
|
('sphinx.testing.util', ['SphinxTestApp', 'cleanup'], None, None))
|
||||||
verify('method', 'SphinxTestApp.cleanup',
|
verify('method', 'SphinxTestApp.cleanup',
|
||||||
('util', ['SphinxTestApp', 'cleanup'], None, None))
|
('sphinx.testing.util', ['SphinxTestApp', 'cleanup'], None, None))
|
||||||
|
|
||||||
|
|
||||||
def test_format_signature(app):
|
def test_format_signature(app):
|
||||||
@ -1881,6 +1882,43 @@ def test_overload(app):
|
|||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.sphinx('html', testroot='ext-autodoc')
|
||||||
|
def test_pymodule_for_ModuleLevelDocumenter(app):
|
||||||
|
app.env.ref_context['py:module'] = 'target'
|
||||||
|
actual = do_autodoc(app, 'class', 'classes.Foo')
|
||||||
|
assert list(actual) == [
|
||||||
|
'',
|
||||||
|
'.. py:class:: Foo()',
|
||||||
|
' :module: target.classes',
|
||||||
|
'',
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.sphinx('html', testroot='ext-autodoc')
|
||||||
|
def test_pymodule_for_ClassLevelDocumenter(app):
|
||||||
|
app.env.ref_context['py:module'] = 'target'
|
||||||
|
actual = do_autodoc(app, 'method', 'methods.Base.meth')
|
||||||
|
assert list(actual) == [
|
||||||
|
'',
|
||||||
|
'.. py:method:: Base.meth()',
|
||||||
|
' :module: target.methods',
|
||||||
|
'',
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.sphinx('html', testroot='ext-autodoc')
|
||||||
|
def test_pyclass_for_ClassLevelDocumenter(app):
|
||||||
|
app.env.ref_context['py:module'] = 'target.methods'
|
||||||
|
app.env.ref_context['py:class'] = 'Base'
|
||||||
|
actual = do_autodoc(app, 'method', 'meth')
|
||||||
|
assert list(actual) == [
|
||||||
|
'',
|
||||||
|
'.. py:method:: Base.meth()',
|
||||||
|
' :module: target.methods',
|
||||||
|
'',
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.sphinx('dummy', testroot='ext-autodoc')
|
@pytest.mark.sphinx('dummy', testroot='ext-autodoc')
|
||||||
def test_autodoc(app, status, warning):
|
def test_autodoc(app, status, warning):
|
||||||
app.builder.build_all()
|
app.builder.build_all()
|
||||||
|
Loading…
Reference in New Issue
Block a user