Skip to content

Commit

Permalink
extract inner function
Browse files Browse the repository at this point in the history
  • Loading branch information
AA-Turner committed Jul 28, 2023
1 parent 8f96360 commit 61a444c
Showing 1 changed file with 26 additions and 33 deletions.
59 changes: 26 additions & 33 deletions tests/test_ext_autodoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,42 +283,26 @@ def foo3(self, d='\n'):
'(b, c=42, *d, **e)'


def check_attrgetter_using(app):
from target import Class
from target.inheritance import Derived
def _assert_getter_works(app, directive, objtype, name, attrs=(), **kw):
getattr_spy = []

directive = make_directive_bridge(app.env)

def assert_getter_works(objtype, name, obj, attrs=[], **kw):
getattr_spy = []

def special_getattr(obj, name, *defargs):
if name in attrs:
getattr_spy.append((obj, name))
return None
return getattr(obj, name, *defargs)
app.add_autodoc_attrgetter(type, special_getattr)
def _special_getattr(obj, attr_name, *defargs):
if attr_name in attrs:
getattr_spy.append((obj, attr_name))
return None
return getattr(obj, attr_name, *defargs)

del getattr_spy[:]
inst = app.registry.documenters[objtype](directive, name)
inst.generate(**kw)

hooked_members = [s[1] for s in getattr_spy]
documented_members = [s[1] for s in processed_signatures]
for attr in attrs:
fullname = '.'.join((name, attr))
assert attr in hooked_members
assert fullname not in documented_members, \
'%r was not hooked by special_attrgetter function' % fullname
app.add_autodoc_attrgetter(type, _special_getattr)

with catch_warnings(record=True):
directive.genopt['members'] = ALL
directive.genopt['inherited_members'] = False
print(directive.genopt)
assert_getter_works('class', 'target.Class', Class, ['meth'])
getattr_spy.clear()
app.registry.documenters[objtype](directive, name).generate(**kw)

directive.genopt['inherited_members'] = True
assert_getter_works('class', 'target.inheritance.Derived', Derived, ['inheritedmeth'])
hooked_members = {s[1] for s in getattr_spy}
documented_members = {s[1] for s in processed_signatures}
for attr in attrs:
fullname = '.'.join((name, attr))
assert attr in hooked_members
assert fullname not in documented_members, f'{fullname!r} not intercepted'


@pytest.mark.sphinx('html', testroot='ext-autodoc')
Expand All @@ -334,7 +318,16 @@ def test_autodoc_process_signature_typing_generic(app):
'',
]

check_attrgetter_using(app)
directive = make_directive_bridge(app.env)
directive.genopt['members'] = ALL

directive.genopt['inherited_members'] = False
with catch_warnings(record=True):
_assert_getter_works(app, directive, 'class', 'target.Class', ['meth'])

directive.genopt['inherited_members'] = True
with catch_warnings(record=True):
_assert_getter_works(app, directive, 'class', 'target.inheritance.Derived', ['inheritedmeth'])


def test_autodoc_process_signature_typehints(app):
Expand Down

0 comments on commit 61a444c

Please sign in to comment.