Add types to dump/load interface
Committed abd60b
--- a/web/util.py
+++ b/web/util.py
import easyuri
-def dump(payload, **kwargs):
+def dump(payload, **kwargs) -> dict | list:
path = kwargs.pop("path", None)
output = JSONEncoder(**kwargs).encode(payload)
- if path is None:
- return output
- with pathlib.Path(path).open("w") as fp:
- fp.write(output)
+ if path:
+ with pathlib.Path(path).open("w") as fp:
+ fp.write(output)
+ return output
-def load(payload=None, path=None):
+def load(payload=None, path=None) -> dict | list:
if path:
with pathlib.Path(path).open("r") as fp:
return json.load(fp)
elif payload:
return json.loads(payload)
+ raise RuntimeError("must provide payload or path")
class JSONEncoder(json.JSONEncoder):