@@ -4175,47 +4175,47 @@ class StructWithLiterals(csp.Struct):
41754175
41764176 def test_pipe_operator_types (self ):
41774177 """Test using the pipe operator for union types in Python 3.10+"""
4178- if sys . version_info >= ( 3 , 10 ): # Only run on Python 3.10+
4179- # Define a class using various pipe operator combinations
4180- class PipeTypesConfig (csp .Struct ):
4181- # Basic primitive types with pipe
4182- id_field : str | int
4183- # Pipe with None (similar to Optional)
4184- description : str | None = None
4185- # Multiple types with pipe
4186- value : str | int | float | bool
4187- # Container with pipe
4188- tags : List [str ] | Dict [str , str ] | None = None
4189- # Pipe with literal for comparison
4190- status : Literal ["active" , "inactive" ] | None = "active"
4191-
4192- # Test all valid types
4193- valid_cases = [
4194- {"id_field" : "string_id" , "value" : "string_value" },
4195- {"id_field" : 42 , "value" : 123 },
4196- {"id_field" : "mixed" , "value" : 3.14 },
4197- {"id_field" : 999 , "value" : True },
4198- {"id_field" : "with_desc" , "value" : 1 , "description" : "Description" },
4199- {"id_field" : "with_dict" , "value" : 1 , "tags" : None },
4200- ]
4201-
4202- for case in valid_cases :
4203- result = PipeTypesConfig .from_dict (case )
4204- # use the other route to get back the result
4205- result_to_dict_loop = TypeAdapter (PipeTypesConfig ).validate_python (result .to_dict ())
4206- self .assertEqual (result , result_to_dict_loop )
4207-
4208- # Test invalid values
4209- invalid_cases = [
4210- {"id_field" : 3.14 , "value" : 1 }, # Float for id_field
4211- {"id_field" : None , "value" : 1 }, # None for required id_field
4212- {"id_field" : "test" , "value" : {}}, # Dict for value
4213- {"id_field" : "test" , "value" : None }, # None for required value
4214- {"id_field" : "test" , "value" : 1 , "status" : "unknown" }, # Invalid literal
4215- ]
4216- for case in invalid_cases :
4217- with self .assertRaises (ValidationError ):
4218- TypeAdapter (PipeTypesConfig ).validate_python (case )
4178+
4179+ # Define a class using various pipe operator combinations
4180+ class PipeTypesConfig (csp .Struct ):
4181+ # Basic primitive types with pipe
4182+ id_field : str | int
4183+ # Pipe with None (similar to Optional)
4184+ description : str | None = None
4185+ # Multiple types with pipe
4186+ value : str | int | float | bool
4187+ # Container with pipe
4188+ tags : List [str ] | Dict [str , str ] | None = None
4189+ # Pipe with literal for comparison
4190+ status : Literal ["active" , "inactive" ] | None = "active"
4191+
4192+ # Test all valid types
4193+ valid_cases = [
4194+ {"id_field" : "string_id" , "value" : "string_value" },
4195+ {"id_field" : 42 , "value" : 123 },
4196+ {"id_field" : "mixed" , "value" : 3.14 },
4197+ {"id_field" : 999 , "value" : True },
4198+ {"id_field" : "with_desc" , "value" : 1 , "description" : "Description" },
4199+ {"id_field" : "with_dict" , "value" : 1 , "tags" : None },
4200+ ]
4201+
4202+ for case in valid_cases :
4203+ result = PipeTypesConfig .from_dict (case )
4204+ # use the other route to get back the result
4205+ result_to_dict_loop = TypeAdapter (PipeTypesConfig ).validate_python (result .to_dict ())
4206+ self .assertEqual (result , result_to_dict_loop )
4207+
4208+ # Test invalid values
4209+ invalid_cases = [
4210+ {"id_field" : 3.14 , "value" : 1 }, # Float for id_field
4211+ {"id_field" : None , "value" : 1 }, # None for required id_field
4212+ {"id_field" : "test" , "value" : {}}, # Dict for value
4213+ {"id_field" : "test" , "value" : None }, # None for required value
4214+ {"id_field" : "test" , "value" : 1 , "status" : "unknown" }, # Invalid literal
4215+ ]
4216+ for case in invalid_cases :
4217+ with self .assertRaises (ValidationError ):
4218+ TypeAdapter (PipeTypesConfig ).validate_python (case )
42194219
42204220 def test__metadata_info (self ):
42214221 class MyStruct (DerivedMixed ):
0 commit comments