@@ -211,7 +211,7 @@ def canonicalish(schema: JSONType) -> Dict[str, Any]:
211211 This is obviously incomplete, but improves best-effort recognition of
212212 equivalent schemas and makes conversion logic simpler.
213213 """
214- if schema is True :
214+ if schema is True : # noqa: SIM114
215215 return {}
216216 elif schema is False :
217217 return {"not" : {}}
@@ -245,13 +245,16 @@ def canonicalish(schema: JSONType) -> Dict[str, Any]:
245245 if_ = schema .pop ("if" , None )
246246 then = schema .pop ("then" , schema )
247247 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 })
255258 assert isinstance (schema , dict )
256259 # Recurse into the value of each keyword with a schema (or list of them) as a value
257260 for key in SCHEMA_KEYS :
@@ -395,7 +398,7 @@ def canonicalish(schema: JSONType) -> Dict[str, Any]:
395398 type_ .remove ("object" )
396399 # Discard dependencies values that don't restrict anything
397400 for k , v in schema .get ("dependencies" , {}).copy ().items ():
398- if v == [] or v == TRUTHY :
401+ if v in ([], TRUTHY ) :
399402 schema ["dependencies" ].pop (k )
400403 # Remove no-op keywords
401404 for kw , identity in {
@@ -438,15 +441,13 @@ def canonicalish(schema: JSONType) -> Dict[str, Any]:
438441 max_ = schema .get ("maxProperties" , float ("inf" ))
439442 assert isinstance (max_ , (int , float ))
440443 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+ ):
444450 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" )
450451
451452 for t , kw in TYPE_SPECIFIC_KEYS :
452453 numeric = {"number" , "integer" }
@@ -719,7 +720,7 @@ def merged(schemas: List[Any]) -> Optional[Schema]:
719720 if "contains" in out and "contains" in s and out ["contains" ] != s ["contains" ]:
720721 # If one `contains` schema is a subset of the other, we can discard it.
721722 m = merged ([out ["contains" ], s ["contains" ]])
722- if m == out ["contains" ] or m == s ["contains" ]:
723+ if m in ( out ["contains" ], s ["contains" ]) :
723724 out ["contains" ] = m
724725 s .pop ("contains" )
725726 if "not" in out and "not" in s and out ["not" ] != s ["not" ]:
0 commit comments