From f311c53c9fb1339b99b5e93825cea70d1a64475d Mon Sep 17 00:00:00 2001 From: Felix Stupp Date: Sun, 6 Aug 2023 16:41:57 +0200 Subject: [PATCH] Add common types for natively Json objects --- .../entertainment_decider/common/__init__.py | 14 +++++++++++ server/entertainment_decider/common/_types.py | 25 +++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 server/entertainment_decider/common/_types.py diff --git a/server/entertainment_decider/common/__init__.py b/server/entertainment_decider/common/__init__.py index b3d07e6..e87da5e 100644 --- a/server/entertainment_decider/common/__init__.py +++ b/server/entertainment_decider/common/__init__.py @@ -20,9 +20,23 @@ from ._string import ( from ._subprocess import ( call, ) +from ._types import ( + JsonContainer, + JsonLeaf, + JsonList, + JsonMapping, + JsonMappingKey, + JsonRepr, +) __all__ = [ + "JsonContainer", + "JsonLeaf", + "JsonList", + "JsonMapping", + "JsonMappingKey", + "JsonRepr", "all_same", "call", "date_to_datetime", diff --git a/server/entertainment_decider/common/_types.py b/server/entertainment_decider/common/_types.py new file mode 100644 index 0000000..57de9d8 --- /dev/null +++ b/server/entertainment_decider/common/_types.py @@ -0,0 +1,25 @@ +from __future__ import annotations + +from collections.abc import ( + Sequence, + Mapping, +) +from typing import ( + TypeAlias, +) + + +JsonMappingKey: TypeAlias = str | int | float +"""type for use in JSON mappings as key""" +JsonLeaf: TypeAlias = JsonMappingKey | bool | None +"""object natively mapping to JSON as values excluding containers""" + +JsonMapping: TypeAlias = Mapping[JsonMappingKey, "JsonRepr"] +"""mapping natively mapping to JSON""" +JsonList: TypeAlias = Sequence["JsonRepr"] +"""list natively mapping to JSON""" +JsonContainer: TypeAlias = JsonList | JsonMapping +"""container natively mapping to JSON""" + +JsonRepr: TypeAlias = JsonContainer | JsonLeaf +"""object natively mapping to JSON"""