Skip to content

Commit 9a7fe3f

Browse files
committed
Wrap simple classes in a type/class
Make it easier to get the class' module and/or name.
1 parent 2485329 commit 9a7fe3f

File tree

5 files changed

+48
-17
lines changed

5 files changed

+48
-17
lines changed

src/classify/library.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
@frozen
3131
class Attribute:
3232
name: str
33-
defining_class: str
33+
defining_class: "SimpleClass"
3434
value: Any
3535

3636

@@ -47,6 +47,19 @@ class Class:
4747
properties: list = Factory(list)
4848

4949

50+
@frozen
51+
class SimpleClass:
52+
name: str
53+
module: str
54+
55+
@staticmethod
56+
def from_class(klass):
57+
return SimpleClass(
58+
name=klass.__name__,
59+
module=klass.__module__,
60+
)
61+
62+
5063
@frozen
5164
class Line:
5265
start: int
@@ -65,7 +78,7 @@ class Member[C]:
6578
class Method:
6679
name: str
6780
docstring: str
68-
defining_class: str
81+
defining_class: SimpleClass
6982
arguments: str
7083
code: str
7184
lines: Line
@@ -79,7 +92,7 @@ def build_attributes(members: list[Member]) -> Generator[Attribute, None, None]:
7992

8093
yield Attribute(
8194
name=member.name,
82-
defining_class=member.cls.__name__,
95+
defining_class=SimpleClass.from_class(member.cls),
8396
value=member.obj,
8497
)
8598

@@ -111,7 +124,7 @@ def build_methods(members: list[Member]) -> Generator[Method, None, None]:
111124
yield Method(
112125
name=member.name,
113126
docstring=pydoc.getdoc(member.obj),
114-
defining_class=member.cls.__name__,
127+
defining_class=SimpleClass.from_class(member.cls),
115128
arguments=arguments,
116129
code="".join(lines),
117130
lines=Line(start=start_line, total=len(lines)),
@@ -151,11 +164,13 @@ def classify[C](obj: type[C]) -> Class:
151164
for method in build_methods(instance_methods):
152165
methods[method.name].append(method)
153166

167+
ancestors = [SimpleClass.from_class(c) for c in mro[:-1]]
168+
154169
return Class(
155170
name=obj.__name__,
156171
module=obj.__module__,
157172
docstring=pydoc.getdoc(obj),
158-
ancestors=[k.__name__ for k in mro[:-1]],
173+
ancestors=ancestors,
159174
parents=get_parents(obj),
160175
attributes=dict(sorted(attributes.items())),
161176
classes=sorted(classes),

src/classify/renderers/string.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def methods(methods: dict[str, list[Method]], indent) -> str:
5454
for definitions in methods.values():
5555
for i, method in enumerate(definitions):
5656
if len(definitions) > 1 and i == 0:
57-
content += f"{indent}# Defined on: {method.defining_class}\n"
57+
content += f"{indent}# Defined on: {method.defining_class.name}\n"
5858
lines = method.code.split("\n")[:-1]
5959
for line in lines:
6060
# TODO: dedent code at source so defined indent isn't tied to

src/classify/templates/class.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ <h2>Attributes</h2>
3131
{{ name }} = {{ attribute.value|attribute }}
3232
</code>
3333
</td>
34-
<td>{{ attribute.defining_class }}</td>
34+
<td>{{ attribute.defining_class.name }}.{{ attribute.defining_class.name }}</td>
3535
</tr>
3636
{% endfor %}
3737
{% endfor %}
@@ -55,7 +55,7 @@ <h2>Methods</h2>
5555
{% for name, declarations in klass.methods.items() %}
5656
{% for declaration in declarations %}
5757
<div class="method">
58-
<h3>def {{ name }}{{ declaration.arguments }}: [{{ declaration.defining_class }}]</h3>
58+
<h3>def {{ name }}{{ declaration.arguments }}: [{{ declaration.defining_class.name }}]</h3>
5959
<p>{{ declaration.docstring|e }}</p>
6060
<p>Found on lines {{ declaration.lines.start }} to {{ declaration.lines.start+declaration.lines.total }} of {{ declaration.file }}</p>
6161
<pre>{{ declaration.code }}</pre>

tests/conftest.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import pytest
22

3-
from classify.library import Attribute, Class, Line, Method
3+
from classify.library import Attribute, Class, Line, Method, SimpleClass
44

55

66
def inner_class(name):
@@ -14,7 +14,7 @@ def inner_class(name):
1414
"abc": [
1515
Attribute(
1616
name="abc",
17-
defining_class=name,
17+
defining_class=SimpleClass(name=name, module=""),
1818
value="123",
1919
)
2020
]
@@ -25,10 +25,12 @@ def inner_class(name):
2525

2626

2727
def method(name, **kwargs):
28+
defining_class = SimpleClass(name=kwargs.get("defining_class", ""), module="")
29+
2830
return Method(
2931
name=name,
3032
docstring=kwargs.get("docstring", ""),
31-
defining_class=kwargs.get("defining_class", ""),
33+
defining_class=defining_class,
3234
arguments=kwargs.get("arguments", ""),
3335
code=kwargs.get("code", ""),
3436
lines=Line(start=42, total=7),
@@ -49,8 +51,10 @@ def dummy_class():
4951
],
5052
methods={
5153
"one": [
52-
method("one", defining_class="ParentClass"),
53-
method("one", defining_class="MyClass"),
54+
method(
55+
"one", defining_class=SimpleClass(name="ParentClass", module="")
56+
),
57+
method("one", defining_class=SimpleClass(name="MyClass", module="")),
5458
]
5559
},
5660
)

tests/renderers/test_string.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import pytest
22

3-
from classify.library import Attribute
3+
from classify.library import Attribute, SimpleClass
44
from classify.renderers.string import attributes, docstring
55

66

@@ -12,15 +12,27 @@ class MyClass:
1212
("attr", "expected"),
1313
[
1414
(
15-
Attribute(name="my_var", defining_class="MyClass", value="test"),
15+
Attribute(
16+
name="my_var",
17+
defining_class=SimpleClass(name="MyClass", module=""),
18+
value="test",
19+
),
1620
'my_var = "test"\n',
1721
),
1822
(
19-
Attribute(name="my_var", defining_class="MyClass", value=MyClass),
23+
Attribute(
24+
name="my_var",
25+
defining_class=SimpleClass(name="MyClass", module=""),
26+
value=MyClass,
27+
),
2028
"my_var = MyClass\n",
2129
),
2230
(
23-
Attribute(name="my_var", defining_class="MyClass", value=7),
31+
Attribute(
32+
name="my_var",
33+
defining_class=SimpleClass(name="MyClass", module=""),
34+
value=7,
35+
),
2436
"my_var = 7\n",
2537
),
2638
],

0 commit comments

Comments
 (0)