Skip to content

Commit fc2fa36

Browse files
Merge pull request #243 from phenobarbital/new-exports
New export: adaptive cards
2 parents 6843d91 + 6002c2b commit fc2fa36

File tree

12 files changed

+556
-108
lines changed

12 files changed

+556
-108
lines changed

datamodel/abstract.py

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
import contextlib
22
import logging
3-
from typing import Optional, Any, List, Dict, get_args, get_origin, ClassVar
3+
from typing import (
4+
Optional,
5+
Any,
6+
List,
7+
Dict,
8+
Union,
9+
get_args,
10+
get_origin,
11+
ClassVar
12+
)
413
from types import GenericAlias
514
from collections import OrderedDict
615
from collections.abc import Callable
@@ -59,9 +68,10 @@ def _dc_method_setattr_(self, name: str, value: Any) -> None:
5968
This version separates the known-field assignment (with optional validation)
6069
from the “extra field” assignment and uses a helper to perform conversion/validation.
6170
"""
62-
# Ensure that the __values__ dict is present.
63-
if not hasattr(self, '__values__'):
64-
object.__setattr__(self, '__values__', {})
71+
if (name.startswith('__') and name.endswith('__')):
72+
# or check explicitly if name in ('__values__', '__valid__', ...)
73+
object.__setattr__(self, name, value)
74+
return
6575

6676
# Check whether we are assigning to a known field.
6777
if name in self.__fields__:
@@ -84,8 +94,6 @@ def _dc_method_setattr_(self, name: str, value: Any) -> None:
8494
# For extra attributes, store them as usual.
8595
# (Note: here we “neutralize” any callable value to None if needed.)
8696
object.__setattr__(self, name, None if callable(value) else value)
87-
if name == '__values__':
88-
return
8997

9098
# If the field isn’t known yet:
9199
if name not in self.__fields__:
@@ -245,6 +253,7 @@ def _initialize_fields(attrs, annotations, strict):
245253
_type_category = 'dataclass'
246254
elif _is_typing or _is_alias: # noqa
247255
if df.origin is not None and (df.origin is list and df.args):
256+
df._inner_targs = df.args
248257
df._inner_type = args[0]
249258
df._inner_origin = get_origin(df._inner_type)
250259
df._typing_args = get_args(df._inner_type)
@@ -254,11 +263,19 @@ def _initialize_fields(attrs, annotations, strict):
254263
except (TypeError, KeyError):
255264
df._encoder_fn = None
256265
if origin is list:
257-
inner_type = args[0]
266+
df._inner_targs = df.args
267+
df._inner_type = args[0]
258268
try:
259-
df._encoder_fn = encoders[inner_type]
269+
df._encoder_fn = encoders[df._inner_type]
260270
except (TypeError, KeyError):
261271
df._encoder_fn = None
272+
elif origin is Union:
273+
df._inner_targs = df.args
274+
df._inner_type = args[0]
275+
df._inner_is_dc = is_dataclass(df._inner_type)
276+
df._inner_priv = is_primitive(df._inner_type)
277+
df._inner_origin = get_origin(df._inner_type)
278+
df._typing_args = get_args(df._inner_type)
262279
_type_category = 'typing'
263280
elif isclass(_type):
264281
_type_category = 'class'
@@ -341,10 +358,10 @@ def _initialize_fields(attrs, annotations, strict):
341358
# Set additional attributes:
342359
dc.__columns__ = cols
343360
dc.__fields__ = list(_columns)
344-
dc.__values__ = {}
345361
dc.__encoder__ = JSONContent
346362
dc.__valid__ = False
347-
dc.__errors__ = None
363+
dc.__errors__ = {}
364+
dc.__values__ = {}
348365
dc.__frozen__ = strict
349366
dc.__initialised__ = False
350367
dc.__field_types__ = _types

datamodel/base.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,11 @@ def __post_init__(self) -> None:
4747
Hint: please check the "payload" attribute in the exception.""",
4848
payload=errors
4949
)
50-
self.__errors__ = errors
51-
object.__setattr__(self, "__valid__", False)
50+
object.__setattr__(self, '__errors__', errors)
51+
object.__setattr__(self, '__valid__', False)
5252
else:
53-
object.__setattr__(self, "__valid__", True)
53+
object.__setattr__(self, '__valid__', True)
54+
return
5455

5556
@classmethod
5657
def register_parser(

0 commit comments

Comments
 (0)