@@ -264,38 +264,78 @@ def _initialize_fields(attrs, annotations, strict):
264264 _type_category = 'primitive'
265265 elif origin == type :
266266 _type_category = 'type'
267+ elif _type is set or _type is frozenset :
268+ _type_category = 'set'
267269 elif _is_dc :
268270 _type_category = 'dataclass'
269271 elif _is_typing or _is_alias : # noqa
270272 if df .origin is not None and (df .origin is list and df .args ):
271273 df ._inner_targs = df .args
272- df ._inner_type = args [0 ]
274+ df ._inner_type = args [0 ] if args else Any
273275 df ._inner_origin = get_origin (df ._inner_type )
274276 df ._typing_args = get_args (df ._inner_type )
275277 df ._inner_is_dc = is_dataclass (df ._inner_type )
276278 try :
277279 df ._encoder_fn = encoders [df ._inner_type ]
278280 except (TypeError , KeyError ):
279281 df ._encoder_fn = None
282+ # Handle list type
280283 if origin is list :
281284 df ._inner_targs = df .args
282- df ._inner_type = args [0 ]
285+ df ._inner_type = args [0 ] if args else Any
283286 try :
284287 df ._encoder_fn = encoders [df ._inner_type ]
285288 except (TypeError , KeyError ):
286289 df ._encoder_fn = None
290+ # Handle set type
291+ elif origin is set :
292+ df ._inner_targs = df .args
293+ df ._inner_type = args [0 ] if args else Any
294+ try :
295+ df ._encoder_fn = encoders [df ._inner_type ]
296+ except (TypeError , KeyError ):
297+ df ._encoder_fn = None
298+ # Handle tuple type
299+ elif origin is tuple :
300+ df ._inner_targs = df .args
301+ # For tuple, use first arg if no Ellipsis, otherwise use it for all elements
302+ has_ellipsis = len (args ) == 2 and args [1 ] is Ellipsis
303+ df ._inner_type = args [0 ] if has_ellipsis or args else Any
304+ try :
305+ df ._encoder_fn = encoders [df ._inner_type ]
306+ except (TypeError , KeyError ):
307+ df ._encoder_fn = None
308+ # Handle dict type
309+ elif origin is dict :
310+ df ._inner_targs = df .args
311+ df ._key_type = args [0 ] if len (args ) > 0 else Any
312+ df ._value_type = args [1 ] if len (args ) > 1 else Any
313+ df ._inner_origin = get_origin (df ._value_type )
314+ df ._typing_args = get_args (df ._value_type )
315+ try :
316+ df ._key_encoder_fn = encoders [df ._key_type ]
317+ except (TypeError , KeyError ):
318+ df ._key_encoder_fn = None
319+ try :
320+ df ._value_encoder_fn = encoders [df ._value_type ]
321+ except (TypeError , KeyError ):
322+ df ._value_encoder_fn = None
323+ # Handle Union type
287324 elif origin is Union :
288325 df ._inner_targs = df .args
289- df ._inner_type = args [0 ]
326+ try :
327+ df ._inner_type = args [0 ]
328+ except IndexError as exc :
329+ raise IndexError (
330+ f"Union type { field } must have at least one type."
331+ ) from exc
290332 df ._inner_is_dc = is_dataclass (df ._inner_type )
291333 df ._inner_priv = is_primitive (df ._inner_type )
292334 df ._inner_origin = get_origin (df ._inner_type )
293335 df ._typing_args = get_args (df ._inner_type )
294336 _type_category = 'typing'
295337 elif isclass (_type ):
296338 _type_category = 'class'
297- # elif _is_alias:
298- # _type_category = 'typing'
299339 else :
300340 # TODO: making parser for complex types
301341 _type_category = 'complex'
@@ -360,15 +400,6 @@ def __new__(cls, name, bases, attrs, **kwargs): # noqa
360400 _typing_args .update (new_typing_args )
361401 aliases .update (new_aliases )
362402 primary_keys .extend (nw_primary_keys )
363-
364- # Store computed results in cache
365- # cls._base_class_cache[base_key] = {
366- # 'cols': cols.copy(),
367- # 'types': _types.copy(),
368- # '_typing_args': _typing_args.copy(),
369- # 'aliases': aliases.copy(),
370- # 'primary_keys': primary_keys.copy(),
371- # }
372403 cache_entry = {
373404 'cols' : cols .copy (),
374405 'types' : _types .copy (),
0 commit comments