1+ from collections .abc import Iterator
2+ from collections .abc import Mapping
13from typing import Any
2- from typing import Iterator
3- from typing import Mapping
44from typing import cast
55
66from jsonschema ._keywords import allOf as _allOf
1818from openapi_schema_validator ._regex import search as regex_search
1919
2020
21- def handle_discriminator (
22- validator : Any , _ : Any , instance : Any , schema : Mapping [str , Any ]
23- ) -> Iterator [ValidationError ]:
21+ def handle_discriminator (validator : Any , _ : Any , instance : Any , schema : Mapping [str , Any ]) -> Iterator [ValidationError ]:
2422 """
2523 Handle presence of discriminator in anyOf, oneOf and allOf.
2624 The behaviour is the same in all 3 cases because at most 1 schema will match.
@@ -29,9 +27,7 @@ def handle_discriminator(
2927 prop_name = discriminator ["propertyName" ]
3028
3129 if not validator .is_type (instance , "object" ):
32- yield ValidationError (
33- f"{ instance !r} is not of type 'object'" , context = []
34- )
30+ yield ValidationError (f"{ instance !r} is not of type 'object'" , context = [])
3531 return
3632
3733 prop_value = instance .get (prop_name )
@@ -44,10 +40,7 @@ def handle_discriminator(
4440 return
4541
4642 # Use explicit mapping if available, otherwise try implicit value
47- ref = (
48- discriminator .get ("mapping" , {}).get (prop_value )
49- or f"#/components/schemas/{ prop_value } "
50- )
43+ ref = discriminator .get ("mapping" , {}).get (prop_value ) or f"#/components/schemas/{ prop_value } "
5144
5245 if not isinstance (ref , str ):
5346 # this is a schema error
@@ -132,11 +125,7 @@ def type(
132125 yield ValidationError ("None for not nullable" )
133126
134127 # Pragmatic: allow bytes for binary format (common in Python use cases)
135- if (
136- data_type == "string"
137- and schema .get ("format" ) == "binary"
138- and isinstance (instance , bytes )
139- ):
128+ if data_type == "string" and schema .get ("format" ) == "binary" and isinstance (instance , bytes ):
140129 return
141130
142131 if not validator .is_type (instance , data_type ):
@@ -183,9 +172,7 @@ def pattern(
183172 try :
184173 matches = regex_search (patrn , instance )
185174 except ECMARegexSyntaxError as exc :
186- yield ValidationError (
187- f"{ patrn !r} is not a valid regular expression ({ exc } )"
188- )
175+ yield ValidationError (f"{ patrn !r} is not a valid regular expression ({ exc } )" )
189176 return
190177
191178 if not matches :
@@ -235,11 +222,8 @@ def required(
235222 if prop_schema :
236223 read_only = prop_schema .get ("readOnly" , False )
237224 write_only = prop_schema .get ("writeOnly" , False )
238- if (
239- getattr (validator , "write" , True )
240- and read_only
241- or getattr (validator , "read" , True )
242- and write_only
225+ if (getattr (validator , "write" , True ) and read_only ) or (
226+ getattr (validator , "read" , True ) and write_only
243227 ):
244228 continue
245229 yield ValidationError (f"{ property !r} is a required property" )
@@ -299,10 +283,9 @@ def additionalProperties(
299283 for extra in extras :
300284 for error in validator .descend (instance [extra ], aP , path = extra ):
301285 yield error
302- elif validator .is_type (aP , "boolean" ):
303- if not aP :
304- error = "Additional properties are not allowed (%s %s unexpected)"
305- yield ValidationError (error % extras_msg (extras ))
286+ elif validator .is_type (aP , "boolean" ) and not aP :
287+ error = "Additional properties are not allowed (%s %s unexpected)"
288+ yield ValidationError (error % extras_msg (extras ))
306289
307290
308291def write_readOnly (
0 commit comments