11import inspect
22from copy import deepcopy
3- from enum import Enum
43from typing import (
54 Any ,
65 Callable ,
2221 DuplicatedRegistrationError ,
2322 MappingError ,
2423)
24+ from .utils import is_enum , is_primitive , is_sequence , object_contains
2525
2626# Custom Types
2727S = TypeVar ("S" )
3030SpecFunction = Callable [[Type [T ]], Iterable [str ]]
3131FieldsMap = Optional [Dict [str , Any ]]
3232
33- __PRIMITIVE_TYPES = {int , float , complex , str , bytes , bytearray , bool }
34-
35-
36- def _is_sequence (obj : Any ) -> bool :
37- """Check if object implements `__iter__` method"""
38- return hasattr (obj , "__iter__" )
39-
40-
41- def _is_subscriptable (obj : Any ) -> bool :
42- """Check if object implements `__getitem__` method"""
43- return hasattr (obj , "__getitem__" )
44-
45-
46- def _object_contains (obj : Any , field_name : str ) -> bool :
47- return _is_subscriptable (obj ) and field_name in obj
48-
49-
50- def _is_primitive (obj : Any ) -> bool :
51- """Check if object type is primitive"""
52- return type (obj ) in __PRIMITIVE_TYPES
53-
54-
55- def _is_enum (obj : Any ) -> bool :
56- """Check if object type is enum"""
57- return issubclass (type (obj ), Enum )
58-
5933
6034def _try_get_field_value (
6135 field_name : str , original_obj : Any , custom_mapping : FieldsMap
@@ -64,7 +38,7 @@ def _try_get_field_value(
6438 return True , custom_mapping [field_name ] # type: ignore [index]
6539 if hasattr (original_obj , field_name ):
6640 return True , getattr (original_obj , field_name )
67- if _object_contains (original_obj , field_name ):
41+ if object_contains (original_obj , field_name ):
6842 return True , original_obj [field_name ]
6943 return False , None
7044
@@ -275,7 +249,7 @@ def _map_subobject(
275249 self , obj : S , _visited_stack : Set [int ], skip_none_values : bool = False
276250 ) -> Any :
277251 """Maps subobjects recursively"""
278- if _is_primitive (obj ) or _is_enum (obj ):
252+ if is_primitive (obj ) or is_enum (obj ):
279253 return obj
280254
281255 obj_id = id (obj )
@@ -290,7 +264,7 @@ def _map_subobject(
290264 else :
291265 _visited_stack .add (obj_id )
292266
293- if _is_sequence (obj ):
267+ if is_sequence (obj ):
294268 if isinstance (obj , dict ):
295269 result = {
296270 k : self ._map_subobject (
0 commit comments