Skip to content

Commit

Permalink
Fix more tests?
Browse files Browse the repository at this point in the history
  • Loading branch information
AA-Turner committed Jan 19, 2025
1 parent 273da74 commit 142014e
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 8 deletions.
49 changes: 42 additions & 7 deletions tests/test_extensions/test_ext_autodoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1146,6 +1146,7 @@ def test_autodoc_descriptor(app):
'',
' .. py:property:: Class.prop',
' :module: target.descriptor',
' :canonical: target.descriptor.prop',
'',
' Property.',
'',
Expand Down Expand Up @@ -1522,6 +1523,7 @@ def _node(
*,
args: str,
indent: int,
canonical: bool = False,
**options: Any,
) -> list[str]:
prefix = indent * ' '
Expand All @@ -1535,8 +1537,10 @@ def rst_option(name: str, value: Any) -> str:
'',
f'{prefix}.. py:{role}:: {name}{args}',
f'{prefix}{tab}:module: {self.module}',
*itertools.starmap(rst_option, options.items()),
]
if canonical:
lines.append(f'{prefix}{tab}:canonical: {self.module}.{name}')
lines += itertools.starmap(rst_option, options.items())
if doc:
lines.extend(['', f'{prefix}{tab}{doc}'])
lines.append('')
Expand All @@ -1550,11 +1554,20 @@ def entry(
role: str,
args: str = '',
indent: int = 3,
canonical: bool = False,
**rst_options: Any,
) -> list[str]:
"""Get the RST lines for a named attribute, method, etc."""
qualname = f'{self.name}.{entry_name}'
return self._node(role, qualname, doc, args=args, indent=indent, **rst_options)
return self._node(
role,
qualname,
doc,
args=args,
indent=indent,
canonical=canonical,
**rst_options,
)

def preamble_lookup(
self, doc: str, *, indent: int = 0, **options: Any
Expand Down Expand Up @@ -1619,15 +1632,37 @@ def method(
*flags: str,
args: str = '()',
indent: int = 3,
canonical: bool = False,
) -> list[str]:
rst_options = dict.fromkeys(flags, '')
return self.entry(
name, doc, role='method', args=args, indent=indent, **rst_options
name,
doc,
role='method',
args=args,
indent=indent,
canonical=canonical,
**rst_options,
)

def member(self, name: str, value: Any, doc: str, *, indent: int = 3) -> list[str]:
def member(
self,
name: str,
value: Any,
doc: str,
*,
indent: int = 3,
canonical: bool = False,
) -> list[str]:
rst_options = {'value': repr(value)}
return self.entry(name, doc, role='attribute', indent=indent, **rst_options)
return self.entry(
name,
doc,
role='attribute',
indent=indent,
canonical=canonical,
**rst_options,
)


@pytest.fixture
Expand Down Expand Up @@ -1691,8 +1726,8 @@ def test_enum_class_with_data_type(app, autodoc_enum_options):
actual = do_autodoc(app, 'class', fmt.target, options)
assert list(actual) == [
*fmt.preamble_lookup('this is enum class'),
*fmt.entry('dtype', 'docstring', role='property'),
*fmt.method('isupper', 'inherited'),
*fmt.entry('dtype', 'docstring', role='property', canonical=True),
*fmt.method('isupper', 'inherited', canonical=True),
*fmt.method('say_goodbye', 'docstring', 'classmethod'),
*fmt.method('say_hello', 'docstring'),
*fmt.member('x', 'x', ''),
Expand Down
1 change: 1 addition & 0 deletions tests/test_extensions/test_ext_autodoc_autoattribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ def test_autoattribute_GenericAlias(app):
'',
'.. py:attribute:: Class.T',
' :module: target.genericalias',
' :canonical: typing.List',
'',
' A list of int',
'',
Expand Down
5 changes: 5 additions & 0 deletions tests/test_extensions/test_ext_autodoc_autoclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,20 +219,23 @@ def test_properties(app):
'',
' .. py:property:: Foo.prop1',
' :module: target.properties',
' :canonical: target.properties.prop1',
' :type: int',
'',
' docstring',
'',
'',
' .. py:property:: Foo.prop1_with_type_comment',
' :module: target.properties',
' :canonical: target.properties.prop1_with_type_comment',
' :type: int',
'',
' docstring',
'',
'',
' .. py:property:: Foo.prop2',
' :module: target.properties',
' :canonical: target.properties.prop2',
' :classmethod:',
' :type: int',
'',
Expand All @@ -241,6 +244,7 @@ def test_properties(app):
'',
' .. py:property:: Foo.prop2_with_type_comment',
' :module: target.properties',
' :canonical: target.properties.prop2_with_type_comment',
' :classmethod:',
' :type: int',
'',
Expand Down Expand Up @@ -526,6 +530,7 @@ def test_autoattribute_TypeVar_module_level(app):
'',
'.. py:class:: Class.T1',
' :module: target.typevar',
' :canonical: target.typevar.T1',
'',
' T1',
'',
Expand Down
1 change: 1 addition & 0 deletions tests/test_extensions/test_ext_autodoc_autodata.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ def test_autodata_GenericAlias(app):
'',
'.. py:data:: T',
' :module: target.genericalias',
' :canonical: typing.List',
'',
' A list of int',
'',
Expand Down
3 changes: 2 additions & 1 deletion tests/test_extensions/test_ext_autodoc_autofunction.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def test_methoddescriptor(app):
'',
'.. py:function:: __add__(self, value, /)',
' :module: builtins.int',
' :canonical: builtins.int.int.__add__',
' :canonical: builtins.int.__add__',
'',
' Return self+value.',
'',
Expand Down Expand Up @@ -197,6 +197,7 @@ def test_synchronized_coroutine(app):
'',
'.. py:function:: sync_func()',
' :module: target.coroutine',
' :canonical: target.coroutine._other_coro_func',
'',
]

Expand Down
4 changes: 4 additions & 0 deletions tests/test_extensions/test_ext_autodoc_autoproperty.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def test_properties(app):
'',
'.. py:property:: Foo.prop1',
' :module: target.properties',
' :canonical: target.properties.prop1',
' :type: int',
'',
' docstring',
Expand All @@ -32,6 +33,7 @@ def test_class_properties(app):
'',
'.. py:property:: Foo.prop2',
' :module: target.properties',
' :canonical: target.properties.prop2',
' :classmethod:',
' :type: int',
'',
Expand All @@ -49,6 +51,7 @@ def test_properties_with_type_comment(app):
'',
'.. py:property:: Foo.prop1_with_type_comment',
' :module: target.properties',
' :canonical: target.properties.prop1_with_type_comment',
' :type: int',
'',
' docstring',
Expand All @@ -65,6 +68,7 @@ def test_class_properties_with_type_comment(app):
'',
'.. py:property:: Foo.prop2_with_type_comment',
' :module: target.properties',
' :canonical: target.properties.prop2_with_type_comment',
' :classmethod:',
' :type: int',
'',
Expand Down
17 changes: 17 additions & 0 deletions tests/test_extensions/test_ext_autodoc_configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ def test_autodoc_inherit_docstrings_for_inherited_members(app):
'',
' .. py:method:: Derived.inheritedclassmeth()',
' :module: target.inheritance',
' :canonical: target.inheritance.Base.inheritedclassmeth',
' :classmethod:',
'',
' Inherited class method.',
Expand All @@ -341,6 +342,7 @@ def test_autodoc_inherit_docstrings_for_inherited_members(app):
'',
' .. py:method:: Derived.inheritedstaticmeth(cls)',
' :module: target.inheritance',
' :canonical: target.inheritance.Base.inheritedstaticmeth',
' :staticmethod:',
'',
' Inherited static method.',
Expand All @@ -361,10 +363,12 @@ def test_autodoc_inherit_docstrings_for_inherited_members(app):
' :classmethod:',
'',
' Inherited class method.',
' :canonical: target.inheritance.Base.inheritedclassmeth',
'',
'',
' .. py:method:: Derived.inheritedstaticmeth(cls)',
' :module: target.inheritance',
' :canonical: target.inheritance.Base.inheritedstaticmeth',
' :staticmethod:',
'',
' Inherited static method.',
Expand Down Expand Up @@ -421,12 +425,14 @@ def test_autodoc_docstring_signature(app):
'',
' .. py:property:: DocstringSig.prop1',
' :module: target',
' :canonical: target.prop1',
'',
' First line of docstring',
'',
'',
' .. py:property:: DocstringSig.prop2',
' :module: target',
' :canonical: target.prop2',
'',
' First line of docstring',
' Second line of docstring',
Expand Down Expand Up @@ -480,13 +486,15 @@ def test_autodoc_docstring_signature(app):
'',
'',
' .. py:property:: DocstringSig.prop1',
' :canonical: target.prop1',
' :module: target',
'',
' DocstringSig.prop1(self)',
' First line of docstring',
'',
'',
' .. py:property:: DocstringSig.prop2',
' :canonical: target.prop2',
' :module: target',
'',
' First line of docstring',
Expand Down Expand Up @@ -653,6 +661,7 @@ def test_mocked_module_imports(app):
'',
'.. py:data:: Alias',
' :module: target.need_mocks',
' :canonical: missing_package2.missing_module2.Class',
'',
' docstring',
'',
Expand All @@ -665,6 +674,7 @@ def test_mocked_module_imports(app):
'',
' .. py:attribute:: TestAutodoc.Alias',
' :module: target.need_mocks',
' :canonical: missing_package2.missing_module2.Class',
'',
' docstring',
'',
Expand Down Expand Up @@ -771,11 +781,13 @@ def test_autodoc_typehints_signature(app):
'',
' .. py:property:: Math.path',
' :module: target.typehints',
' :canonical: target.typehints.path',
f' :type: ~{type_ppp}',
'',
'',
' .. py:property:: Math.prop',
' :module: target.typehints',
' :canonical: target.typehints.prop',
' :type: int',
'',
'',
Expand Down Expand Up @@ -896,10 +908,12 @@ def test_autodoc_typehints_none(app):
'',
' .. py:property:: Math.path',
' :module: target.typehints',
' :canonical: target.typehints.path',
'',
'',
' .. py:property:: Math.prop',
' :module: target.typehints',
' :canonical: target.typehints.prop',
'',
'',
'.. py:class:: NewAnnotation(i)',
Expand Down Expand Up @@ -1585,11 +1599,13 @@ def test_autodoc_typehints_format_fully_qualified(app):
'',
' .. py:property:: Math.path',
' :module: target.typehints',
' :canonical: target.typehints.path',
f' :type: {type_ppp}',
'',
'',
' .. py:property:: Math.prop',
' :module: target.typehints',
' :canonical: target.typehints.prop',
' :type: int',
'',
'',
Expand Down Expand Up @@ -1663,6 +1679,7 @@ def test_autodoc_typehints_format_fully_qualified_for_generic_alias(app):
'',
'.. py:data:: L',
' :module: target.genericalias',
' :canonical: typing.List',
'',
' A list of Class',
'',
Expand Down
5 changes: 5 additions & 0 deletions tests/test_extensions/test_ext_autodoc_preserve_defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,30 +53,35 @@ def test_preserve_defaults(app):
'',
' .. py:property:: MultiLine.prop1',
' :module: target.preserve_defaults',
' :canonical: target.preserve_defaults.prop1',
'',
' docstring',
'',
'',
' .. py:property:: MultiLine.prop2',
' :module: target.preserve_defaults',
' :canonical: target.preserve_defaults.prop2',
'',
' docstring',
'',
'',
' .. py:property:: MultiLine.prop3',
' :module: target.preserve_defaults',
' :canonical: target.preserve_defaults.prop3',
'',
' docstring',
'',
'',
' .. py:property:: MultiLine.prop4',
' :module: target.preserve_defaults',
' :canonical: target.preserve_defaults.prop4',
'',
' docstring',
'',
'',
' .. py:property:: MultiLine.prop5',
' :module: target.preserve_defaults',
' :canonical: target.preserve_defaults.prop5',
'',
' docstring',
'',
Expand Down

0 comments on commit 142014e

Please sign in to comment.