@@ -7,6 +7,7 @@ from .bases import META
77from .models import Condition
88from .type_def import ExplicitNullType , T
99from .utils .dict_helper import DictWithLowerStore
10+ from .utils .object_path import PathType
1011
1112
1213# A cached mapping of dataclass to the list of fields, as returned by
@@ -31,21 +32,18 @@ CLASS_TO_DUMPER: dict[type, type[AbstractDumper]] = {}
3132
3233# A cached mapping of a dataclass to each of its case-insensitive field names
3334# and load hook.
34- FIELD_NAME_TO_LOAD_PARSER : dict [
35- type , DictWithLowerStore [str , AbstractParser ]] = {}
35+ FIELD_NAME_TO_LOAD_PARSER : dict [type , DictWithLowerStore [str , AbstractParser ]] = {}
3636
3737# Since the dump process doesn't use Parsers currently, we use a sentinel
3838# mapping to confirm if we need to setup the dump config for a dataclass
3939# on an initial run.
4040IS_DUMP_CONFIG_SETUP : dict [type , bool ] = {}
4141
4242# A cached mapping, per dataclass, of JSON field to instance field name
43- JSON_FIELD_TO_DATACLASS_FIELD : dict [
44- type , dict [str , str | ExplicitNullType ]] = defaultdict (dict )
43+ JSON_FIELD_TO_DATACLASS_FIELD : dict [type , dict [str , str | ExplicitNullType ]] = defaultdict (dict )
4544
4645# A cached mapping, per dataclass, of instance field name to JSON path
47- DATACLASS_FIELD_TO_JSON_PATH : dict [
48- type , dict [str , list [str | int | bool | float ]]] = defaultdict (dict )
46+ DATACLASS_FIELD_TO_JSON_PATH : dict [type , dict [str , PathType ]] = defaultdict (dict )
4947
5048# A cached mapping, per dataclass, of instance field name to JSON field
5149DATACLASS_FIELD_TO_JSON_FIELD : dict [type , dict [str , str ]] = defaultdict (dict )
@@ -56,22 +54,20 @@ DATACLASS_FIELD_TO_SKIP_IF: dict[type, dict[str, Condition]] = defaultdict(dict)
5654# A mapping of dataclass name to its Meta initializer (defined in
5755# :class:`bases.BaseJSONWizardMeta`), which is only set when the
5856# :class:`JSONSerializable.Meta` is sub-classed.
59- META_INITIALIZER : dict [
60- str , Callable [[type [W ]], None ]] = {}
61-
57+ META_INITIALIZER : dict [str , Callable [[type [W ]], None ]] = {}
6258
6359# Mapping of dataclass to its Meta inner class, which will only be set when
6460# the :class:`JSONSerializable.Meta` is sub-classed.
6561_META : dict [type , META ] = {}
6662
6763
68- def dataclass_to_loader (cls : type ):
64+ def dataclass_to_loader (cls : type ) -> type [ AbstractLoader ] :
6965 """
7066 Returns the loader for a dataclass.
7167 """
7268
7369
74- def dataclass_to_dumper (cls : type ):
70+ def dataclass_to_dumper (cls : type ) -> type [ AbstractDumper ] :
7571 """
7672 Returns the dumper for a dataclass.
7773 """
@@ -89,13 +85,13 @@ def set_class_dumper(cls: type, dumper: type[AbstractDumper]):
8985 """
9086
9187
92- def json_field_to_dataclass_field (cls : type ):
88+ def json_field_to_dataclass_field (cls : type ) -> dict [ str , str | ExplicitNullType ] :
9389 """
9490 Returns a mapping of JSON field to dataclass field.
9591 """
9692
9793
98- def dataclass_field_to_json_path (cls : type ):
94+ def dataclass_field_to_json_path (cls : type ) -> dict [ str , PathType ] :
9995 """
10096 Returns a mapping of dataclass field to JSON path.
10197 """
@@ -151,7 +147,7 @@ def _setup_load_config_for_cls(cls_loader: type[AbstractLoader],
151147 """
152148
153149
154- def setup_dump_config_for_cls_if_needed (cls : type ):
150+ def setup_dump_config_for_cls_if_needed (cls : type ) -> None :
155151 """
156152 This function processes a class `cls` on an initial run, and sets up the
157153 dump process for `cls` by iterating over each dataclass field. For each
@@ -172,7 +168,7 @@ def setup_dump_config_for_cls_if_needed(cls: type):
172168 """
173169
174170
175- def call_meta_initializer_if_needed (cls : type [W ]):
171+ def call_meta_initializer_if_needed (cls : type [W ]) -> None :
176172 """
177173 Calls the Meta initializer when the inner :class:`Meta` is sub-classed.
178174 """
@@ -186,31 +182,31 @@ def get_meta(cls: type) -> META:
186182 """
187183
188184
189- def dataclass_fields (cls ) -> tuple [Field , ...]:
185+ def dataclass_fields (cls : type ) -> tuple [Field , ...]:
190186 """
191187 Cache the `dataclasses.fields()` call for each class, as overall that
192188 ends up around 5x faster than making a fresh call each time.
193189
194190 """
195191
196192
197- def dataclass_init_fields (cls ) -> tuple [Field , ...]:
193+ def dataclass_init_fields (cls : type ) -> tuple [Field , ...]:
198194 """Get only the dataclass fields that would be passed into the constructor."""
199195
200196
201- def dataclass_field_names (cls ) -> tuple [str , ...]:
197+ def dataclass_field_names (cls : type ) -> tuple [str , ...]:
202198 """Get the names of all dataclass fields"""
203199
204200
205- def dataclass_field_to_default (cls ) -> dict [str , Any ]:
201+ def dataclass_field_to_default (cls : type ) -> dict [str , Any ]:
206202 """Get default values for the (optional) dataclass fields."""
207203
208204
209- def is_builtin_class (cls ) :
205+ def is_builtin_class (cls : type ) -> bool :
210206 """Check if a class is a builtin in Python."""
211207
212208
213- def is_builtin (o : Any ):
209+ def is_builtin (o : Any ) -> bool :
214210 """Check if an object/singleton/class is a builtin in Python."""
215211
216212
@@ -227,7 +223,7 @@ def get_class_name(class_or_instance) -> str:
227223 """Return the fully qualified name of a class."""
228224
229225
230- def get_outer_class_name (inner_cls , default = None , raise_ = True ):
226+ def get_outer_class_name (inner_cls , default = None , raise_ : bool = True ) -> str :
231227 """
232228 Attempt to return the fully qualified name of the outer (enclosing) class,
233229 given a reference to the inner class.
@@ -239,11 +235,11 @@ def get_outer_class_name(inner_cls, default=None, raise_=True):
239235 """
240236
241237
242- def get_class (obj ) :
238+ def get_class (obj : Any ) -> type :
243239 """Get the class for an object `obj`"""
244240
245241
246- def is_subclass (obj , base_cls : type ) -> bool :
242+ def is_subclass (obj : Any , base_cls : type ) -> bool :
247243 """Check if `obj` is a sub-class of `base_cls`"""
248244
249245
0 commit comments