Skip to content

Commit 6354953

Browse files
committed
Only build docstrings in the string renderer when there is content
1 parent 64506f2 commit 6354953

File tree

3 files changed

+45
-29
lines changed

3 files changed

+45
-29
lines changed

src/classify/renderers/string.py

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,44 @@
11
from ..library import Class
22

33

4-
def to_string(structure: Class) -> str:
5-
indent = " " * 4
6-
7-
def attributes(attributes):
8-
attrs = []
9-
for name, definitions in attributes.items():
10-
value = definitions[-1].value
11-
attrs.append(f"{indent}{name} = {value}\n")
12-
return "".join(attrs)
13-
14-
def declaration(name, parents):
15-
parents = ", ".join([p.__name__ for p in parents])
16-
return f"class {name}({parents}):"
17-
18-
def docstring(docstring):
19-
quotes = f'{indent}"""\n'
20-
lines = docstring.split("\n")
21-
block = "".join([f"{indent}{line}\n" for line in lines])
22-
return f"{quotes}{block}{quotes}"
23-
24-
def methods(methods):
25-
content = ""
26-
for definitions in methods.values():
27-
for d in definitions:
28-
lines = d.code.split("\n")[:-1]
29-
for line in lines:
30-
content += f"{indent}{line[4:]}\n"
31-
content += "\n"
32-
return content
4+
indent = " " * 4
5+
6+
7+
def attributes(attributes):
8+
attrs = []
9+
for name, definitions in attributes.items():
10+
value = definitions[-1].value
11+
attrs.append(f"{indent}{name} = {value}\n")
12+
return "".join(attrs)
13+
14+
15+
def declaration(name, parents):
16+
parents = ", ".join([p.__name__ for p in parents])
17+
return f"class {name}({parents}):"
18+
19+
20+
def docstring(docstring):
21+
if not docstring:
22+
return ""
3323

24+
quotes = f'{indent}"""\n'
25+
lines = docstring.split("\n")
26+
block = "".join([f"{indent}{line}\n" for line in lines])
27+
return f"{quotes}{block}{quotes}"
28+
29+
30+
def methods(methods):
31+
content = ""
32+
for definitions in methods.values():
33+
for d in definitions:
34+
lines = d.code.split("\n")[:-1]
35+
for line in lines:
36+
content += f"{indent}{line[4:]}\n"
37+
content += "\n"
38+
return content
39+
40+
41+
def to_string(structure: Class) -> str:
3442
content = declaration(structure.name, structure.parents)
3543
content += "\n"
3644
content += docstring(structure.docstring) if docstring else ""

tests/renderers/test_string.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from classify.renderers.string import docstring
2+
3+
4+
def test_docstring():
5+
assert docstring("") == ""

tests/test_library.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,15 @@
1313
DummyClass,
1414
[
1515
"__init__",
16+
"class_method",
17+
"class_only_method",
1618
"four",
1719
"my_cached_prop",
1820
"my_dj_cached_prop",
1921
"my_property",
2022
"one",
2123
"some_attribute",
24+
"static_method",
2225
"two",
2326
],
2427
),

0 commit comments

Comments
 (0)