Coverage for app/utils/typing.py: 75%

20 statements  

« prev     ^ index     » next       coverage.py v7.9.2, created at 2026-02-19 12:47 +0000

1from typing import TypeAlias 

2from typing import cast # noqa: F401 # type: ignore[import] # Used in other files for type casting 

3from typing import Optional # noqa: F401 # type: ignore[import] # Used in other files for optional types (FASTAPI optional parameters) 

4from typing import List # noqa: F401 # type: ignore[import] # Used in other files for list types 

5from neo4j.time import DateTime 

6 

7JSONScalar: TypeAlias = str | int | float | bool | None 

8JSONValue: TypeAlias = JSONScalar | list["JSONValue"] | dict[str, "JSONValue"] 

9JSONObject: TypeAlias = dict[str, JSONValue] 

10 

11def serialize_value(v: object) -> JSONValue: 

12 if isinstance(v, dict): 

13 dct = cast(dict[str, object], v) 

14 return {k: serialize_value(val) for k, val in dct.items()} 

15 

16 if isinstance(v, list): 

17 lst = cast(List[object], v) 

18 return [serialize_value(x) for x in lst] 

19 

20 if isinstance(v, DateTime): 

21 return v.to_native().isoformat() 

22 

23 if isinstance(v, (str, int, float, bool)) or v is None: 

24 return v 

25 

26 raise TypeError(f"Unsupported type for JSON serialization: {type(v)}")