Skip to content
5 changes: 4 additions & 1 deletion src/griffe/_internal/encoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,12 @@ def _attach_parent_to_exprs(obj: Class | Function | Attribute | TypeAlias, paren


def _load_module(obj_dict: dict[str, Any]) -> Module:
filepath = obj_dict.get("filepath")
if filepath is not None:
filepath = [*map(Path, filepath)] if isinstance(filepath, list) else Path(filepath)
module = Module(
name=obj_dict["name"],
filepath=Path(obj_dict["filepath"]) if "filepath" in obj_dict else None,
filepath=filepath,
docstring=_load_docstring(obj_dict),
runtime=obj_dict.get("runtime", True),
analysis=obj_dict.get("analysis"),
Expand Down
24 changes: 23 additions & 1 deletion tests/test_encoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,17 @@
import pytest
from jsonschema import ValidationError, validate

from griffe import Attribute, Class, Function, GriffeLoader, Kind, Module, Object, temporary_visited_module
from griffe import (
Attribute,
Class,
Function,
GriffeLoader,
Kind,
Module,
Object,
temporary_inspected_package,
temporary_visited_module,
)


def test_minimal_data_is_enough() -> None:
Expand All @@ -35,6 +45,18 @@ def test_minimal_data_is_enough() -> None:
Function.from_json(minimal)


def test_namespace_packages() -> None:
"""Test support for namespace packages.
Namespace packages are a bit special as they have no `__init__.py` file.
"""
with temporary_inspected_package("namespace_package", init=False) as package:
dump_options = {"indent": 2, "sort_keys": True}
as_json = package.as_json(full=True, **dump_options)
from_json = Object.from_json(as_json)
assert from_json.as_json(full=True, **dump_options) == as_json


@pytest.mark.parametrize(
"symbol",
[
Expand Down