Skip to content

Commit df7a1d4

Browse files
committed
Support examples in hf-doc-builder docs
Extracting examples from HF doc-builder did not work because they are indented after extraction like Example: ```python ... ``` which caused the language check to fail, since it did not take into account that the code block could still be preceded by whitespace. Incidentally, this also uncovered a bug in testing the README markdown, since this requires `memory=True`, as the README itself describes.
1 parent e4a97fd commit df7a1d4

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

mktestdocs/__main__.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def exec_python(source):
5050
register_executor("python", exec_python)
5151

5252

53-
def get_codeblock_members(*classes):
53+
def get_codeblock_members(*classes, lang="python"):
5454
"""
5555
Grabs the docstrings of any methods of any classes that are passed in.
5656
"""
@@ -61,7 +61,7 @@ def get_codeblock_members(*classes):
6161
for name, member in inspect.getmembers(cl):
6262
if member.__doc__:
6363
results.append(member)
64-
return [m for m in results if len(grab_code_blocks(m.__doc__)) > 0]
64+
return [m for m in results if len(grab_code_blocks(m.__doc__, lang=lang)) > 0]
6565

6666

6767
def check_codeblock(block, lang="python"):
@@ -76,7 +76,7 @@ def check_codeblock(block, lang="python"):
7676
"""
7777
first_line = block.split("\n")[0]
7878
if lang:
79-
if first_line[3:] != lang:
79+
if first_line.lstrip()[3:] != lang:
8080
return ""
8181
return "\n".join(block.split("\n")[1:])
8282

@@ -104,12 +104,14 @@ def grab_code_blocks(docstring, lang="python"):
104104
block += line + "\n"
105105
return [textwrap.dedent(c) for c in codeblocks if c != ""]
106106

107+
107108
def format_docstring(docstring):
108109
"""Formats docstring to be able to successfully go through dedent."""
109110
if docstring[:1] != "\n":
110111
return f"\n {docstring}"
111112
return docstring
112113

114+
113115
def check_docstring(obj, lang=""):
114116
"""
115117
Given a function, test the contents of the docstring.

tests/test_class.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,27 @@ def hello(self):
6060
"""
6161
return self.name
6262

63+
def hfdocs_style(self, value):
64+
"""
65+
Returns value
66+
67+
Example:
68+
69+
```python
70+
from dinosaur import Dinosaur
71+
72+
dino = Dinosaur()
73+
assert dino.a(1) == 1
74+
```
75+
"""
76+
return value
77+
6378

6479
members = get_codeblock_members(Dinosaur)
6580

6681

6782
def test_grab_methods():
68-
assert len(get_codeblock_members(Dinosaur)) == 4
83+
assert len(get_codeblock_members(Dinosaur)) == 5
6984

7085

7186
@pytest.mark.parametrize("obj", members, ids=lambda d: d.__qualname__)

tests/test_mktestdocs.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
from mktestdocs import check_md_file
44

5+
56
def test_readme(monkeypatch):
67
test_dir = pathlib.Path(__file__).parent
78
fpath = test_dir.parent / "README.md"
89
monkeypatch.chdir(test_dir)
910

10-
check_md_file(fpath=fpath)
11+
check_md_file(fpath=fpath, memory=True)

0 commit comments

Comments
 (0)