Skip to content

Commit

Permalink
refactor: Handle both lists and dicts for members when loading JSON d…
Browse files Browse the repository at this point in the history
…ata in preparation of v1

Issue-310: #310
  • Loading branch information
pawamoy committed Aug 14, 2024
1 parent 23da375 commit f89050c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
16 changes: 14 additions & 2 deletions src/_griffe/encoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,13 @@ def _attach_parent_to_exprs(obj: Class | Function | Attribute, parent: Module |

def _load_module(obj_dict: dict[str, Any]) -> Module:
module = Module(name=obj_dict["name"], filepath=Path(obj_dict["filepath"]), docstring=_load_docstring(obj_dict))
for module_member in obj_dict.get("members", []):
# YORE: Bump 2: Replace line with `members = obj_dict.get("members", {}).values()`.
members = obj_dict.get("members", [])
# YORE: Bump 2: Remove block.
if isinstance(members, dict):
members = members.values()

for module_member in members:
module.set_member(module_member.name, module_member)
_attach_parent_to_exprs(module_member, module)
module.labels |= set(obj_dict.get("labels", ()))
Expand All @@ -193,7 +199,13 @@ def _load_class(obj_dict: dict[str, Any]) -> Class:
decorators=_load_decorators(obj_dict),
bases=obj_dict["bases"],
)
for class_member in obj_dict.get("members", []):
# YORE: Bump 2: Replace line with `members = obj_dict.get("members", {}).values()`.
members = obj_dict.get("members", [])
# YORE: Bump 2: Remove block.
if isinstance(members, dict):
members = members.values()

for class_member in members:
class_.set_member(class_member.name, class_member)
_attach_parent_to_exprs(class_member, class_)
class_.labels |= set(obj_dict.get("labels", ()))
Expand Down
3 changes: 2 additions & 1 deletion src/_griffe/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -861,9 +861,10 @@ def as_dict(self, *, full: bool = False, **kwargs: Any) -> dict[str, Any]:
if self.docstring:
base["docstring"] = self.docstring

# doing this last for a prettier JSON dump
base["labels"] = self.labels
# YORE: Bump 1: Replace line with `base["members"] = {name: member.as_dict(full=full, **kwargs) for name, member in self.members.items()}`.
base["members"] = [member.as_dict(full=full, **kwargs) for member in self.members.values()]
# Also replace array and items by object and additionalProperties in docs/schema.json line 163.

return base

Expand Down

0 comments on commit f89050c

Please sign in to comment.