Skip to content

Commit 85dc171

Browse files
Changes to validate_docstring script to be able to check all docstrings at once (#22408)
1 parent f6e1a0a commit 85dc171

File tree

2 files changed

+366
-219
lines changed

2 files changed

+366
-219
lines changed

scripts/tests/test_validate_docstrings.py

+95-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import string
22
import random
3+
import io
34
import pytest
45
import numpy as np
56

@@ -531,28 +532,36 @@ def _import_path(self, klass=None, func=None):
531532

532533
@capture_stderr
533534
def test_good_class(self):
534-
assert validate_one(self._import_path(
535-
klass='GoodDocStrings')) == 0
535+
errors = validate_one(self._import_path(
536+
klass='GoodDocStrings'))['errors']
537+
assert isinstance(errors, list)
538+
assert not errors
536539

537540
@capture_stderr
538541
@pytest.mark.parametrize("func", [
539542
'plot', 'sample', 'random_letters', 'sample_values', 'head', 'head1',
540543
'contains', 'mode'])
541544
def test_good_functions(self, func):
542-
assert validate_one(self._import_path(
543-
klass='GoodDocStrings', func=func)) == 0
545+
errors = validate_one(self._import_path(
546+
klass='GoodDocStrings', func=func))['errors']
547+
assert isinstance(errors, list)
548+
assert not errors
544549

545550
@capture_stderr
546551
def test_bad_class(self):
547-
assert validate_one(self._import_path(
548-
klass='BadGenericDocStrings')) > 0
552+
errors = validate_one(self._import_path(
553+
klass='BadGenericDocStrings'))['errors']
554+
assert isinstance(errors, list)
555+
assert errors
549556

550557
@capture_stderr
551558
@pytest.mark.parametrize("func", [
552559
'func', 'astype', 'astype1', 'astype2', 'astype3', 'plot', 'method'])
553560
def test_bad_generic_functions(self, func):
554-
assert validate_one(self._import_path( # noqa:F821
555-
klass='BadGenericDocStrings', func=func)) > 0
561+
errors = validate_one(self._import_path( # noqa:F821
562+
klass='BadGenericDocStrings', func=func))['errors']
563+
assert isinstance(errors, list)
564+
assert errors
556565

557566
@pytest.mark.parametrize("klass,func,msgs", [
558567
# Summary tests
@@ -594,7 +603,82 @@ def test_bad_generic_functions(self, func):
594603
marks=pytest.mark.xfail)
595604
])
596605
def test_bad_examples(self, capsys, klass, func, msgs):
597-
validate_one(self._import_path(klass=klass, func=func)) # noqa:F821
598-
err = capsys.readouterr().err
606+
result = validate_one(self._import_path(klass=klass, func=func)) # noqa:F821
599607
for msg in msgs:
600-
assert msg in err
608+
assert msg in ' '.join(result['errors'])
609+
610+
611+
class ApiItems(object):
612+
@property
613+
def api_doc(self):
614+
return io.StringIO('''
615+
.. currentmodule:: itertools
616+
617+
Itertools
618+
---------
619+
620+
Infinite
621+
~~~~~~~~
622+
623+
.. autosummary::
624+
625+
cycle
626+
count
627+
628+
Finite
629+
~~~~~~
630+
631+
.. autosummary::
632+
633+
chain
634+
635+
.. currentmodule:: random
636+
637+
Random
638+
------
639+
640+
All
641+
~~~
642+
643+
.. autosummary::
644+
645+
seed
646+
randint
647+
''')
648+
649+
@pytest.mark.parametrize('idx,name', [(0, 'itertools.cycle'),
650+
(1, 'itertools.count'),
651+
(2, 'itertools.chain'),
652+
(3, 'random.seed'),
653+
(4, 'random.randint')])
654+
def test_item_name(self, idx, name):
655+
result = list(validate_docstrings.get_api_items(self.api_doc))
656+
assert result[idx][0] == name
657+
658+
@pytest.mark.parametrize('idx,func', [(0, 'cycle'),
659+
(1, 'count'),
660+
(2, 'chain'),
661+
(3, 'seed'),
662+
(4, 'randint')])
663+
def test_item_function(self, idx, func):
664+
result = list(validate_docstrings.get_api_items(self.api_doc))
665+
assert callable(result[idx][1])
666+
assert result[idx][1].__name__ == func
667+
668+
@pytest.mark.parametrize('idx,section', [(0, 'Itertools'),
669+
(1, 'Itertools'),
670+
(2, 'Itertools'),
671+
(3, 'Random'),
672+
(4, 'Random')])
673+
def test_item_section(self, idx, section):
674+
result = list(validate_docstrings.get_api_items(self.api_doc))
675+
assert result[idx][2] == section
676+
677+
@pytest.mark.parametrize('idx,subsection', [(0, 'Infinite'),
678+
(1, 'Infinite'),
679+
(2, 'Finite'),
680+
(3, 'All'),
681+
(4, 'All')])
682+
def test_item_subsection(self, idx, subsection):
683+
result = list(validate_docstrings.get_api_items(self.api_doc))
684+
assert result[idx][3] == subsection

0 commit comments

Comments
 (0)