Skip to content

Commit aa9e8ad

Browse files
Merge pull request #258 from phenobarbital/new-exports
fix compatibility for python 3.10
2 parents 47465e8 + 80ce8b3 commit aa9e8ad

3 files changed

Lines changed: 22 additions & 7 deletions

File tree

datamodel/models.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import annotations
22
import contextlib
3-
from typing import Any, Dict, get_args
3+
from typing import Any, Dict, Optional, get_args
44
from enum import Enum, EnumMeta
55
# Dataclass
66
import inspect
@@ -210,10 +210,13 @@ def to_dict(
210210
self,
211211
remove_nulls: bool = False,
212212
convert_enums: bool = False,
213-
as_values: bool = False
213+
as_values: bool = False,
214+
exclude: Optional[set] = None
214215
) -> dict[str, Any]:
216+
exclude = exclude or set()
217+
exclude.add('_pgoutput') # Always exclude _pgoutput
215218
if as_values:
216-
return self.__collapse_as_values__(remove_nulls, convert_enums, as_values)
219+
return self.__collapse_as_values__(remove_nulls, convert_enums, as_values, exclude)
217220
d = as_dict(self, dict_factory=dict)
218221
if convert_enums:
219222
d = self.__convert_enums__(d)
@@ -226,12 +229,21 @@ def __collapse_as_values__(
226229
self,
227230
remove_nulls: bool = False,
228231
convert_enums: bool = False,
229-
as_values: bool = False
232+
as_values: bool = False,
233+
exclude: Optional[set] = None
230234
) -> dict[str, Any]:
231235
"""Recursively converts any BaseModel instances to their primary key value."""
232236
out = {}
233237
_fields = self.columns()
234238
for name, field in _fields.items():
239+
if name in exclude:
240+
continue
241+
# Skip internal or error fields
242+
if name.startswith('__') or name == '__errors__':
243+
continue
244+
# Skip fields marked for exclusion
245+
if field.metadata.get('exclude', False):
246+
continue
235247
# datatype = field.type
236248
value = getattr(self, name)
237249
if value is None and remove_nulls:

datamodel/parsers/json.pyx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ from cpython.object cimport (
2525
from dataclasses import _MISSING_TYPE, MISSING, InitVar
2626
from typing import Any, Union
2727
from decimal import Decimal
28-
from enum import Enum, EnumType
28+
from enum import Enum
29+
try:
30+
from enum import EnumType
31+
except ImportError:
32+
from enum import EnumMeta as EnumType
2933
import orjson
3034
from ..exceptions cimport ParserError
3135
from ..fields import Field
@@ -71,7 +75,6 @@ cdef inline bint is_objid(object obj):
7175
ORJSON_DEFAULT_OPTIONS = (
7276
orjson.OPT_SERIALIZE_NUMPY |
7377
orjson.OPT_UTC_Z
74-
# orjson.OPT_NON_STR_KEYS
7578
)
7679

7780

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.17'
9+
__version__ = '0.10.19'
1010
__copyright__ = 'Copyright (c) 2020-2024 Jesus Lara'
1111
__author__ = 'Jesus Lara'
1212
__author_email__ = 'jesuslarag@gmail.com'

0 commit comments

Comments
 (0)