From 24418f5bcb0e4a7d303705db5af48fc4ff9153ee Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Sun, 14 Feb 2021 19:11:39 +0100 Subject: [PATCH] [System] Models: Do not export optional, not set, parameters. --- flaschengeist/models/__init__.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/flaschengeist/models/__init__.py b/flaschengeist/models/__init__.py index 14a36fb..59bb5b5 100644 --- a/flaschengeist/models/__init__.py +++ b/flaschengeist/models/__init__.py @@ -1,4 +1,6 @@ +import sys import datetime + from sqlalchemy.types import DateTime, TypeDecorator @@ -7,6 +9,15 @@ class ModelSerializeMixin: Ignores private and protected members as well as members marked as not to publish (name ends with _) """ + def __is_optional(self, param): + if sys.version_info < (3, 8): + return False + + import typing + if typing.get_origin(self.__class__.__annotations__[param]) is typing.Union and \ + typing.get_args(self.__class__.__annotations__[param])[1] is None: + return getattr(self, param) is None + def serialize(self): """Serialize class to dict Returns: @@ -15,7 +26,7 @@ class ModelSerializeMixin: d = { param: getattr(self, param) for param in self.__class__.__annotations__ - if not param.startswith("_") and not param.endswith("_") + if not param.startswith("_") and not param.endswith("_") and not self.__is_optional(param) } if len(d) == 1: key, value = d.popitem()