Skip to content

Commit 3cccf27

Browse files
authored
fix: Fix loading namespace packages from JSON
Issue-407: #407 PR-413: #413
1 parent 92a23d4 commit 3cccf27

2 files changed

Lines changed: 27 additions & 2 deletions

File tree

src/griffe/_internal/encoders.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,12 @@ def _attach_parent_to_exprs(obj: Class | Function | Attribute | TypeAlias, paren
192192

193193

194194
def _load_module(obj_dict: dict[str, Any]) -> Module:
195+
filepath = obj_dict.get("filepath")
196+
if filepath is not None:
197+
filepath = [*map(Path, filepath)] if isinstance(filepath, list) else Path(filepath)
195198
module = Module(
196199
name=obj_dict["name"],
197-
filepath=Path(obj_dict["filepath"]) if "filepath" in obj_dict else None,
200+
filepath=filepath,
198201
docstring=_load_docstring(obj_dict),
199202
runtime=obj_dict.get("runtime", True),
200203
analysis=obj_dict.get("analysis"),

tests/test_encoders.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,17 @@
88
import pytest
99
from jsonschema import ValidationError, validate
1010

11-
from griffe import Attribute, Class, Function, GriffeLoader, Kind, Module, Object, temporary_visited_module
11+
from griffe import (
12+
Attribute,
13+
Class,
14+
Function,
15+
GriffeLoader,
16+
Kind,
17+
Module,
18+
Object,
19+
temporary_inspected_package,
20+
temporary_visited_module,
21+
)
1222

1323

1424
def test_minimal_data_is_enough() -> None:
@@ -35,6 +45,18 @@ def test_minimal_data_is_enough() -> None:
3545
Function.from_json(minimal)
3646

3747

48+
def test_namespace_packages() -> None:
49+
"""Test support for namespace packages.
50+
51+
Namespace packages are a bit special as they have no `__init__.py` file.
52+
"""
53+
with temporary_inspected_package("namespace_package", init=False) as package:
54+
dump_options = {"indent": 2, "sort_keys": True}
55+
as_json = package.as_json(full=True, **dump_options)
56+
from_json = Object.from_json(as_json)
57+
assert from_json.as_json(full=True, **dump_options) == as_json
58+
59+
3860
@pytest.mark.parametrize(
3961
"symbol",
4062
[

0 commit comments

Comments
 (0)