Skip to content

Commit fe72f7d

Browse files
Merge pull request #246 from phenobarbital/new-exports
fix the _repr_ to avoid errors with a field fails to be converted int…
2 parents f9992ed + 70e8bb6 commit fe72f7d

File tree

3 files changed

+70
-3
lines changed

3 files changed

+70
-3
lines changed

datamodel/models.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,15 @@ def column(self, name: str) -> Field:
155155
return self.__columns__[name]
156156

157157
def __repr__(self) -> str:
158-
f_repr = ", ".join(f"{f.name}={getattr(self, f.name)}" for f in fields(self))
159-
return f"{self.__class__.__name__}({f_repr})"
158+
field_strs = []
159+
for field_name in self.__fields__:
160+
try:
161+
value = getattr(self, field_name)
162+
except AttributeError:
163+
# If this field doesn't exist on the instance, ignore it
164+
continue
165+
field_strs.append(f"{field_name}={value!r}")
166+
return f"{self.__class__.__name__}({', '.join(field_strs)})"
160167

161168
def pop(self, key: str, default: Any = _MISSING_TYPE) -> Any:
162169
"""

datamodel/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
'simple library based on python +3.8 to use Dataclass-syntax'
77
'for interacting with Data'
88
)
9-
__version__ = '0.10.5'
9+
__version__ = '0.10.6'
1010
__copyright__ = 'Copyright (c) 2020-2024 Jesus Lara'
1111
__author__ = 'Jesus Lara'
1212
__author_email__ = '[email protected]'

examples/test_dynamic.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
from typing import Optional, Any
2+
from dataclasses import InitVar
3+
from datamodel import BaseModel, Column
4+
from datamodel.exceptions import ValidationError
5+
6+
class Identity(BaseModel):
7+
"""Identity.
8+
9+
Describe an Authenticated Entity on Navigator.
10+
"""
11+
12+
id: Any = Column(required=True)
13+
auth_method: str = None
14+
access_token: Optional[str] = None
15+
enabled: bool = Column(required=True, default=True)
16+
data: InitVar = {}
17+
is_authenticated: bool = Column(equired=False, default=False)
18+
userdata: dict = Column(required=False, default_factory=dict)
19+
20+
def __post_init__(self, data): # pylint: disable=W0221
21+
self.userdata = data
22+
for key, value in data.items():
23+
self.create_field(key, value)
24+
25+
def set(self, name: str, value: Any) -> None:
26+
# alias for "create_field"
27+
self.create_field(name, value)
28+
29+
class Meta:
30+
strict = False
31+
frozen = False
32+
33+
34+
class BaseUser(Identity):
35+
name: str = Column(required=True)
36+
email: str = Column(required=True)
37+
38+
# Test the creation of dynamic fields on Datamodel:
39+
if __name__ == '__main__':
40+
data = {
41+
'id': 12345,
42+
'name': 'John Doe',
43+
'email': '[email protected]',
44+
'age': 30,
45+
'is_active': True,
46+
'magic': 'Navigator',
47+
'address': {
48+
'street': '123 Main St',
49+
'city': 'Anytown',
50+
'state': 'NY',
51+
'zip': '10001'
52+
}
53+
}
54+
try:
55+
user = BaseUser(data)
56+
print(user)
57+
except ValidationError as e:
58+
print(e.payload)
59+
except Exception as e:
60+
print(e)

0 commit comments

Comments
 (0)