From 06514e913b0048e929160564efa657e94942d9f6 Mon Sep 17 00:00:00 2001 From: Eric Froemling Date: Thu, 27 May 2021 12:53:46 -0500 Subject: [PATCH] moved IOAttrs defaults to class level --- tools/efro/dataclassio.py | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/tools/efro/dataclassio.py b/tools/efro/dataclassio.py index fd829f58..8c6afa39 100644 --- a/tools/efro/dataclassio.py +++ b/tools/efro/dataclassio.py @@ -72,15 +72,28 @@ class Codec(Enum): class IOAttrs: """For specifying io behavior in annotations.""" + storagename: Optional[str] = None + store_default: bool = True + whole_days: bool = False + whole_hours: bool = False + def __init__(self, - storagename: str = None, - store_default: bool = True, - whole_days: bool = False, - whole_hours: bool = False): - self.storagename = storagename - self.store_default = store_default - self.whole_days = whole_days - self.whole_hours = whole_hours + storagename: Optional[str] = storagename, + store_default: bool = store_default, + whole_days: bool = whole_days, + whole_hours: bool = whole_hours): + + # Only store values that differ from class defaults to keep + # our instances nice and lean. + cls = type(self) + if storagename != cls.storagename: + self.storagename = storagename + if store_default != cls.store_default: + self.store_default = store_default + if whole_days != cls.whole_days: + self.whole_days = whole_days + if whole_hours != cls.whole_hours: + self.whole_hours = whole_hours def validate_for_field(self, cls: Type, field: dataclasses.Field) -> None: """Ensure the IOAttrs instance is ok to use with the provided field."""