Skip to content

Commit

Permalink
Refactor test_attrgetter_using (#11293)
Browse files Browse the repository at this point in the history
Co-authored-by: Adam Turner <[email protected]>
  • Loading branch information
marxin and AA-Turner authored Jul 28, 2023
1 parent 99f9209 commit 2c0b81d
Showing 1 changed file with 26 additions and 28 deletions.
54 changes: 26 additions & 28 deletions tests/test_ext_autodoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,42 +403,40 @@ def document_members(self, all_members=False):
]


@pytest.mark.sphinx('html', testroot='ext-autodoc')
def test_attrgetter_using(app):
from target import Class
from target.inheritance import Derived

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'])

def assert_getter_works(objtype, name, obj, attrs=[], **kw):
getattr_spy = []
directive.genopt['inherited_members'] = True
with catch_warnings(record=True):
_assert_getter_works(app, directive, 'class', 'target.inheritance.Derived', ['inheritedmeth'])

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)

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

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
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)

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'])
app.add_autodoc_attrgetter(type, _special_getattr)

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 Down

0 comments on commit 2c0b81d

Please sign in to comment.