@@ -211,7 +211,7 @@ def canonicalish(schema: JSONType) -> Dict[str, Any]:
211
211
This is obviously incomplete, but improves best-effort recognition of
212
212
equivalent schemas and makes conversion logic simpler.
213
213
"""
214
- if schema is True :
214
+ if schema is True : # noqa: SIM114
215
215
return {}
216
216
elif schema is False :
217
217
return {"not" : {}}
@@ -245,13 +245,16 @@ def canonicalish(schema: JSONType) -> Dict[str, Any]:
245
245
if_ = schema .pop ("if" , None )
246
246
then = schema .pop ("then" , schema )
247
247
else_ = schema .pop ("else" , schema )
248
- if if_ is not None and (then is not schema or else_ is not schema ):
249
- if then not in (if_ , TRUTHY ) or else_ != TRUTHY :
250
- alternatives = [
251
- {"allOf" : [if_ , then , schema ]},
252
- {"allOf" : [{"not" : if_ }, else_ , schema ]},
253
- ]
254
- schema = canonicalish ({"anyOf" : alternatives })
248
+ if (
249
+ if_ is not None
250
+ and (then is not schema or else_ is not schema )
251
+ and (then not in (if_ , TRUTHY ) or else_ != TRUTHY )
252
+ ):
253
+ alternatives = [
254
+ {"allOf" : [if_ , then , schema ]},
255
+ {"allOf" : [{"not" : if_ }, else_ , schema ]},
256
+ ]
257
+ schema = canonicalish ({"anyOf" : alternatives })
255
258
assert isinstance (schema , dict )
256
259
# Recurse into the value of each keyword with a schema (or list of them) as a value
257
260
for key in SCHEMA_KEYS :
@@ -395,7 +398,7 @@ def canonicalish(schema: JSONType) -> Dict[str, Any]:
395
398
type_ .remove ("object" )
396
399
# Discard dependencies values that don't restrict anything
397
400
for k , v in schema .get ("dependencies" , {}).copy ().items ():
398
- if v == [] or v == TRUTHY :
401
+ if v in ([], TRUTHY ) :
399
402
schema ["dependencies" ].pop (k )
400
403
# Remove no-op keywords
401
404
for kw , identity in {
@@ -438,15 +441,13 @@ def canonicalish(schema: JSONType) -> Dict[str, Any]:
438
441
max_ = schema .get ("maxProperties" , float ("inf" ))
439
442
assert isinstance (max_ , (int , float ))
440
443
properties = schema .get ("properties" , {})
441
- if len (schema ["required" ]) > max_ :
442
- type_ .remove ("object" )
443
- elif any (properties .get (name , {}) == FALSEY for name in schema ["required" ]):
444
+ propnames_validator = make_validator (schema .get ("propertyNames" , {})).is_valid
445
+ if (
446
+ len (schema ["required" ]) > max_
447
+ or any (properties .get (name , {}) == FALSEY for name in schema ["required" ])
448
+ or not all (propnames_validator (name ) for name in schema ["required" ])
449
+ ):
444
450
type_ .remove ("object" )
445
- else :
446
- propnames = schema .get ("propertyNames" , {})
447
- validator = make_validator (propnames )
448
- if not all (validator .is_valid (name ) for name in schema ["required" ]):
449
- type_ .remove ("object" )
450
451
451
452
for t , kw in TYPE_SPECIFIC_KEYS :
452
453
numeric = {"number" , "integer" }
@@ -719,7 +720,7 @@ def merged(schemas: List[Any]) -> Optional[Schema]:
719
720
if "contains" in out and "contains" in s and out ["contains" ] != s ["contains" ]:
720
721
# If one `contains` schema is a subset of the other, we can discard it.
721
722
m = merged ([out ["contains" ], s ["contains" ]])
722
- if m == out ["contains" ] or m == s ["contains" ]:
723
+ if m in ( out ["contains" ], s ["contains" ]) :
723
724
out ["contains" ] = m
724
725
s .pop ("contains" )
725
726
if "not" in out and "not" in s and out ["not" ] != s ["not" ]:
0 commit comments