Skip to content
This repository was archived by the owner on Jan 19, 2025. It is now read-only.

Commit 6093dcf

Browse files
feat: api command should list attributes of classes (#1112)
Closes #1107. ### Summary of Changes added an attribute ´instance_attribute´ to the class 'class'. ### Testing Instructions run `package-parser api -p sklearn -o ./out` and compare the output with the original file that the class contains. `package-parser annotations -a ../data/api/sklearn__api.json -u ../data/usages/sklearn_usages_counts.json -o ./out/annotations.json` can verify that no error occur if the file is loaded. Co-authored-by: Lars Reimann <[email protected]>
1 parent 08f5440 commit 6093dcf

File tree

2 files changed

+6
-0
lines changed

2 files changed

+6
-0
lines changed

package-parser/package_parser/processing/api/_ast_visitor.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ def leave_module(self, _: astroid.Module) -> None:
133133

134134
def enter_classdef(self, class_node: astroid.ClassDef) -> None:
135135
qname = class_node.qname()
136+
instance_attributes = list(class_node.instance_attrs)
136137

137138
decorators: Optional[astroid.Decorators] = class_node.decorators
138139
if decorators is not None:
@@ -149,6 +150,7 @@ def enter_classdef(self, class_node: astroid.ClassDef) -> None:
149150
is_public=self.is_public(class_node.name, qname),
150151
reexported_by=self.reexported.get(qname, []),
151152
documentation=self.documentation_parser.get_class_documentation(class_node),
153+
instance_attributes=instance_attributes,
152154
)
153155
self.__declaration_stack.append(class_)
154156

package-parser/package_parser/processing/api/model/_api.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ def from_json(json: Any) -> Class:
206206
description=json.get("description", ""),
207207
full_docstring=json.get("docstring", ""),
208208
),
209+
json.get("instance_attributes", []),
209210
)
210211

211212
for method_id in json["methods"]:
@@ -222,6 +223,7 @@ def __init__(
222223
is_public: bool,
223224
reexported_by: list[str],
224225
documentation: ClassDocumentation,
226+
instance_attributes: list[str],
225227
) -> None:
226228
self.id: str = id_
227229
self.qname: str = qname
@@ -231,6 +233,7 @@ def __init__(
231233
self.is_public: bool = is_public
232234
self.reexported_by: list[str] = reexported_by
233235
self.documentation: ClassDocumentation = documentation
236+
self.instance_attributes = instance_attributes
234237

235238
@property
236239
def name(self) -> str:
@@ -251,6 +254,7 @@ def to_json(self) -> Any:
251254
"reexported_by": self.reexported_by,
252255
"description": self.documentation.description,
253256
"docstring": self.documentation.full_docstring,
257+
"instance_attributes": self.instance_attributes,
254258
}
255259

256260

0 commit comments

Comments
 (0)