Skip to content

Commit 59a0649

Browse files
authored
MAINT: a nicer way to detect a module is deprecated in DTModule (#135)
1 parent dd0c181 commit 59a0649

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

scpdt/plugin.py

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,22 @@ def pytest_collection_modifyitems(config, items):
155155
items[:] = unique_items
156156

157157

158+
def _is_deprecated(module):
159+
"""Detect if a module is deprecated (i.e., raises or warns on getattr)."""
160+
names = dir(module)
161+
if not names:
162+
return False
163+
164+
res = False
165+
try:
166+
getattr(module, names[0])
167+
res = False
168+
except DeprecationWarning:
169+
res = True
170+
171+
return res
172+
173+
158174
class DTModule(DoctestModule):
159175
"""
160176
This class extends the DoctestModule class provided by pytest.
@@ -187,6 +203,10 @@ def collect(self):
187203
else:
188204
raise
189205

206+
if _is_deprecated(module):
207+
# bail out early
208+
return
209+
190210
optionflags = dt_config.optionflags
191211

192212
# Plug in the custom runner: `PytestDTRunner`
@@ -202,15 +222,12 @@ def collect(self):
202222
if strategy == 'None':
203223
strategy = None
204224

205-
try:
206-
# NB: additional postprocessing in pytest_collection_modifyitems
207-
for test in find_doctests(module, strategy=strategy, name=module.__name__, config=dt_config):
208-
if test.examples: # skip empty doctests
209-
yield pydoctest.DoctestItem.from_parent(
210-
self, name=test.name, runner=runner, dtest=test
211-
)
212-
except:
213-
pass
225+
# NB: additional postprocessing in pytest_collection_modifyitems
226+
for test in find_doctests(module, strategy=strategy, name=module.__name__, config=dt_config):
227+
if test.examples: # skip empty doctests
228+
yield pydoctest.DoctestItem.from_parent(
229+
self, name=test.name, runner=runner, dtest=test
230+
)
214231

215232

216233
class DTTextfile(DoctestTextfile):

0 commit comments

Comments
 (0)