@@ -790,6 +790,11 @@ def __init__(self, type_: Literal['cat']) -> None:
790
790
self .type_ = 'cat'
791
791
792
792
793
+ class ModelAlien :
794
+ def __init__ (self , type_ : Literal ['alien' ]) -> None :
795
+ self .type_ = 'alien'
796
+
797
+
793
798
@pytest .fixture
794
799
def model_a_b_union_schema () -> core_schema .UnionSchema :
795
800
return core_schema .union_schema (
@@ -850,6 +855,20 @@ def test_union_of_unions_of_models(model_a_b_union_schema: core_schema.UnionSche
850
855
assert s .to_python (ModelCat (type_ = 'cat' ), warnings = 'error' ) == {'type_' : 'cat' }
851
856
assert s .to_python (ModelDog (type_ = 'dog' ), warnings = 'error' ) == {'type_' : 'dog' }
852
857
858
+ # All warnings should be available
859
+ messages = [
860
+ 'Expected `ModelA` but got `ModelAlien`' ,
861
+ 'Expected `ModelB` but got `ModelAlien`' ,
862
+ 'Expected `ModelCat` but got `ModelAlien`' ,
863
+ 'Expected `ModelDog` but got `ModelAlien`' ,
864
+ ]
865
+
866
+ with warnings .catch_warnings (record = True ) as w :
867
+ warnings .simplefilter ('always' )
868
+ s .to_python (ModelAlien (type_ = 'alien' ))
869
+ for m in messages :
870
+ assert m in str (w [0 ].message )
871
+
853
872
854
873
def test_union_of_unions_of_models_with_tagged_union (model_a_b_union_schema : core_schema .UnionSchema ) -> None :
855
874
s = SchemaSerializer (
@@ -885,3 +904,17 @@ def test_union_of_unions_of_models_with_tagged_union(model_a_b_union_schema: cor
885
904
assert s .to_python (ModelB (c = 'c' , d = 'd' ), warnings = 'error' ) == {'c' : 'c' , 'd' : 'd' }
886
905
assert s .to_python (ModelCat (type_ = 'cat' ), warnings = 'error' ) == {'type_' : 'cat' }
887
906
assert s .to_python (ModelDog (type_ = 'dog' ), warnings = 'error' ) == {'type_' : 'dog' }
907
+
908
+ # All warnings should be available
909
+ messages = [
910
+ 'Expected `ModelA` but got `ModelAlien`' ,
911
+ 'Expected `ModelB` but got `ModelAlien`' ,
912
+ 'Expected `ModelCat` but got `ModelAlien`' ,
913
+ 'Expected `ModelDog` but got `ModelAlien`' ,
914
+ ]
915
+
916
+ with warnings .catch_warnings (record = True ) as w :
917
+ warnings .simplefilter ('always' )
918
+ s .to_python (ModelAlien (type_ = 'alien' ))
919
+ for m in messages :
920
+ assert m in str (w [0 ].message )
0 commit comments