moved IOAttrs defaults to class level

This commit is contained in:
Eric Froemling 2021-05-27 12:53:46 -05:00
parent 2f2ae11d4f
commit 06514e913b
No known key found for this signature in database
GPG Key ID: 89C93F0F8D6D5A98

View File

@ -72,15 +72,28 @@ class Codec(Enum):
class IOAttrs: class IOAttrs:
"""For specifying io behavior in annotations.""" """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, def __init__(self,
storagename: str = None, storagename: Optional[str] = storagename,
store_default: bool = True, store_default: bool = store_default,
whole_days: bool = False, whole_days: bool = whole_days,
whole_hours: bool = False): whole_hours: bool = whole_hours):
self.storagename = storagename
self.store_default = store_default # Only store values that differ from class defaults to keep
self.whole_days = whole_days # our instances nice and lean.
self.whole_hours = whole_hours 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: def validate_for_field(self, cls: Type, field: dataclasses.Field) -> None:
"""Ensure the IOAttrs instance is ok to use with the provided field.""" """Ensure the IOAttrs instance is ok to use with the provided field."""