@@ -394,7 +394,10 @@ def _internal_class_schema(
394
394
clazz_frame : Optional [types .FrameType ] = None ,
395
395
generic_params_to_args : Optional [Tuple [Tuple [type , type ], ...]] = None ,
396
396
) -> Type [marshmallow .Schema ]:
397
- _RECURSION_GUARD .seen_classes [clazz ] = clazz .__name__
397
+ # generic aliases do not have a __name__ prior python 3.10
398
+ _name = getattr (clazz , "__name__" , repr (clazz ))
399
+
400
+ _RECURSION_GUARD .seen_classes [clazz ] = _name
398
401
try :
399
402
fields = _dataclass_fields (clazz )
400
403
except TypeError : # Not a dataclass
@@ -450,7 +453,7 @@ def _internal_class_schema(
450
453
if field .init or include_non_init
451
454
)
452
455
453
- schema_class = type (clazz . __name__ , (_base_schema (clazz , base_schema ),), attributes )
456
+ schema_class = type (_name , (_base_schema (clazz , base_schema ),), attributes )
454
457
return cast (Type [marshmallow .Schema ], schema_class )
455
458
456
459
@@ -469,6 +472,7 @@ def _field_by_supertype(
469
472
metadata : dict ,
470
473
base_schema : Optional [Type [marshmallow .Schema ]],
471
474
typ_frame : Optional [types .FrameType ],
475
+ generic_params_to_args : Optional [Tuple [Tuple [type , type ], ...]] = None ,
472
476
) -> marshmallow .fields .Field :
473
477
"""
474
478
Return a new field for fields based on a super field. (Usually spawned from NewType)
@@ -500,6 +504,7 @@ def _field_by_supertype(
500
504
default = default ,
501
505
base_schema = base_schema ,
502
506
typ_frame = typ_frame ,
507
+ generic_params_to_args = generic_params_to_args ,
503
508
)
504
509
505
510
@@ -524,6 +529,7 @@ def _field_for_generic_type(
524
529
typ : type ,
525
530
base_schema : Optional [Type [marshmallow .Schema ]],
526
531
typ_frame : Optional [types .FrameType ],
532
+ generic_params_to_args : Optional [Tuple [Tuple [type , type ], ...]] = None ,
527
533
** metadata : Any ,
528
534
) -> Optional [marshmallow .fields .Field ]:
529
535
"""
@@ -537,7 +543,10 @@ def _field_for_generic_type(
537
543
538
544
if origin in (list , List ):
539
545
child_type = field_for_schema (
540
- arguments [0 ], base_schema = base_schema , typ_frame = typ_frame
546
+ arguments [0 ],
547
+ base_schema = base_schema ,
548
+ typ_frame = typ_frame ,
549
+ generic_params_to_args = generic_params_to_args ,
541
550
)
542
551
list_type = cast (
543
552
Type [marshmallow .fields .List ],
@@ -552,14 +561,20 @@ def _field_for_generic_type(
552
561
from . import collection_field
553
562
554
563
child_type = field_for_schema (
555
- arguments [0 ], base_schema = base_schema , typ_frame = typ_frame
564
+ arguments [0 ],
565
+ base_schema = base_schema ,
566
+ typ_frame = typ_frame ,
567
+ generic_params_to_args = generic_params_to_args ,
556
568
)
557
569
return collection_field .Sequence (cls_or_instance = child_type , ** metadata )
558
570
if origin in (set , Set ):
559
571
from . import collection_field
560
572
561
573
child_type = field_for_schema (
562
- arguments [0 ], base_schema = base_schema , typ_frame = typ_frame
574
+ arguments [0 ],
575
+ base_schema = base_schema ,
576
+ typ_frame = typ_frame ,
577
+ generic_params_to_args = generic_params_to_args ,
563
578
)
564
579
return collection_field .Set (
565
580
cls_or_instance = child_type , frozen = False , ** metadata
@@ -568,14 +583,22 @@ def _field_for_generic_type(
568
583
from . import collection_field
569
584
570
585
child_type = field_for_schema (
571
- arguments [0 ], base_schema = base_schema , typ_frame = typ_frame
586
+ arguments [0 ],
587
+ base_schema = base_schema ,
588
+ typ_frame = typ_frame ,
589
+ generic_params_to_args = generic_params_to_args ,
572
590
)
573
591
return collection_field .Set (
574
592
cls_or_instance = child_type , frozen = True , ** metadata
575
593
)
576
594
if origin in (tuple , Tuple ):
577
595
children = tuple (
578
- field_for_schema (arg , base_schema = base_schema , typ_frame = typ_frame )
596
+ field_for_schema (
597
+ arg ,
598
+ base_schema = base_schema ,
599
+ typ_frame = typ_frame ,
600
+ generic_params_to_args = generic_params_to_args ,
601
+ )
579
602
for arg in arguments
580
603
)
581
604
tuple_type = cast (
@@ -589,10 +612,16 @@ def _field_for_generic_type(
589
612
dict_type = type_mapping .get (Dict , marshmallow .fields .Dict )
590
613
return dict_type (
591
614
keys = field_for_schema (
592
- arguments [0 ], base_schema = base_schema , typ_frame = typ_frame
615
+ arguments [0 ],
616
+ base_schema = base_schema ,
617
+ typ_frame = typ_frame ,
618
+ generic_params_to_args = generic_params_to_args ,
593
619
),
594
620
values = field_for_schema (
595
- arguments [1 ], base_schema = base_schema , typ_frame = typ_frame
621
+ arguments [1 ],
622
+ base_schema = base_schema ,
623
+ typ_frame = typ_frame ,
624
+ generic_params_to_args = generic_params_to_args ,
596
625
),
597
626
** metadata ,
598
627
)
@@ -610,6 +639,7 @@ def _field_for_generic_type(
610
639
metadata = metadata ,
611
640
base_schema = base_schema ,
612
641
typ_frame = typ_frame ,
642
+ generic_params_to_args = generic_params_to_args ,
613
643
)
614
644
from . import union_field
615
645
@@ -622,6 +652,7 @@ def _field_for_generic_type(
622
652
metadata = {"required" : True },
623
653
base_schema = base_schema ,
624
654
typ_frame = typ_frame ,
655
+ generic_params_to_args = generic_params_to_args ,
625
656
),
626
657
)
627
658
for subtyp in subtypes
@@ -730,7 +761,9 @@ def field_for_schema(
730
761
)
731
762
else :
732
763
subtyp = Any
733
- return field_for_schema (subtyp , default , metadata , base_schema , typ_frame )
764
+ return field_for_schema (
765
+ subtyp , default , metadata , base_schema , typ_frame , generic_params_to_args
766
+ )
734
767
735
768
# Generic types
736
769
generic_field = _field_for_generic_type (typ , base_schema , typ_frame , ** metadata )
@@ -748,6 +781,7 @@ def field_for_schema(
748
781
metadata = metadata ,
749
782
base_schema = base_schema ,
750
783
typ_frame = typ_frame ,
784
+ generic_params_to_args = generic_params_to_args ,
751
785
)
752
786
753
787
# enumerations
0 commit comments