1- from copy import deepcopy
21from typing import Any
3- from typing import Dict
4- from typing import Hashable
5- from typing import ItemsView
62from typing import Iterator
7- from typing import List
83from typing import Mapping
9- from typing import Union
4+ from typing import cast
105
116from jsonschema ._keywords import allOf as _allOf
127from jsonschema ._keywords import anyOf as _anyOf
1510from jsonschema ._utils import find_additional_properties
1611from jsonschema .exceptions import FormatError
1712from jsonschema .exceptions import ValidationError
18- from jsonschema .protocols import Validator
1913
2014
2115def handle_discriminator (
22- validator : Validator , _ : Any , instance : Any , schema : Mapping [Hashable , Any ]
16+ validator : Any , _ : Any , instance : Any , schema : Mapping [str , Any ]
2317) -> Iterator [ValidationError ]:
2418 """
2519 Handle presence of discriminator in anyOf, oneOf and allOf.
@@ -45,16 +39,14 @@ def handle_discriminator(
4539 if not isinstance (ref , str ):
4640 # this is a schema error
4741 yield ValidationError (
48- "{!r} mapped value for {!r} should be a string, was {!r}" .format (
49- instance , prop_value , ref
50- ),
42+ f"{ instance !r} mapped value for { prop_value !r} should be a string, was { ref !r} " ,
5143 context = [],
5244 )
5345 return
5446
5547 try :
5648 validator ._validate_reference (ref = ref , instance = instance )
57- except :
49+ except Exception :
5850 yield ValidationError (
5951 f"{ instance !r} reference { ref !r} could not be resolved" ,
6052 context = [],
@@ -65,54 +57,63 @@ def handle_discriminator(
6557
6658
6759def anyOf (
68- validator : Validator ,
69- anyOf : List [Mapping [Hashable , Any ]],
60+ validator : Any ,
61+ anyOf : list [Mapping [str , Any ]],
7062 instance : Any ,
71- schema : Mapping [Hashable , Any ],
63+ schema : Mapping [str , Any ],
7264) -> Iterator [ValidationError ]:
7365 if "discriminator" not in schema :
74- yield from _anyOf (validator , anyOf , instance , schema )
66+ yield from cast (
67+ Iterator [ValidationError ],
68+ _anyOf (validator , anyOf , instance , schema ),
69+ )
7570 else :
7671 yield from handle_discriminator (validator , anyOf , instance , schema )
7772
7873
7974def oneOf (
80- validator : Validator ,
81- oneOf : List [Mapping [Hashable , Any ]],
75+ validator : Any ,
76+ oneOf : list [Mapping [str , Any ]],
8277 instance : Any ,
83- schema : Mapping [Hashable , Any ],
78+ schema : Mapping [str , Any ],
8479) -> Iterator [ValidationError ]:
8580 if "discriminator" not in schema :
86- yield from _oneOf (validator , oneOf , instance , schema )
81+ yield from cast (
82+ Iterator [ValidationError ],
83+ _oneOf (validator , oneOf , instance , schema ),
84+ )
8785 else :
8886 yield from handle_discriminator (validator , oneOf , instance , schema )
8987
9088
9189def allOf (
92- validator : Validator ,
93- allOf : List [Mapping [Hashable , Any ]],
90+ validator : Any ,
91+ allOf : list [Mapping [str , Any ]],
9492 instance : Any ,
95- schema : Mapping [Hashable , Any ],
93+ schema : Mapping [str , Any ],
9694) -> Iterator [ValidationError ]:
9795 if "discriminator" not in schema :
98- yield from _allOf (validator , allOf , instance , schema )
96+ yield from cast (
97+ Iterator [ValidationError ],
98+ _allOf (validator , allOf , instance , schema ),
99+ )
99100 else :
100101 yield from handle_discriminator (validator , allOf , instance , schema )
101102
102103
103104def type (
104- validator : Validator ,
105+ validator : Any ,
105106 data_type : str ,
106107 instance : Any ,
107- schema : Mapping [Hashable , Any ],
108+ schema : Mapping [str , Any ],
108109) -> Iterator [ValidationError ]:
109110 if instance is None :
110111 # nullable implementation based on OAS 3.0.3
111112 # * nullable is only meaningful if its value is true
112113 # * nullable: true is only meaningful in combination with a type
113114 # assertion specified in the same Schema Object.
114115 # * nullable: true operates within a single Schema Object
115- if "nullable" in schema and schema [ "nullable" ] == True :
116+ if schema . get ( "nullable" ) is True :
116117 return
117118 yield ValidationError ("None for not nullable" )
118119
@@ -122,10 +123,10 @@ def type(
122123
123124
124125def format (
125- validator : Validator ,
126+ validator : Any ,
126127 format : str ,
127128 instance : Any ,
128- schema : Mapping [Hashable , Any ],
129+ schema : Mapping [str , Any ],
129130) -> Iterator [ValidationError ]:
130131 if instance is None :
131132 return
@@ -138,10 +139,10 @@ def format(
138139
139140
140141def items (
141- validator : Validator ,
142- items : Mapping [Hashable , Any ],
142+ validator : Any ,
143+ items : Mapping [str , Any ],
143144 instance : Any ,
144- schema : Mapping [Hashable , Any ],
145+ schema : Mapping [str , Any ],
145146) -> Iterator [ValidationError ]:
146147 if not validator .is_type (instance , "array" ):
147148 return
@@ -151,10 +152,10 @@ def items(
151152
152153
153154def required (
154- validator : Validator ,
155- required : List [str ],
155+ validator : Any ,
156+ required : list [str ],
156157 instance : Any ,
157- schema : Mapping [Hashable , Any ],
158+ schema : Mapping [str , Any ],
158159) -> Iterator [ValidationError ]:
159160 if not validator .is_type (instance , "object" ):
160161 return
@@ -175,10 +176,10 @@ def required(
175176
176177
177178def read_required (
178- validator : Validator ,
179- required : List [str ],
179+ validator : Any ,
180+ required : list [str ],
180181 instance : Any ,
181- schema : Mapping [Hashable , Any ],
182+ schema : Mapping [str , Any ],
182183) -> Iterator [ValidationError ]:
183184 if not validator .is_type (instance , "object" ):
184185 return
@@ -193,10 +194,10 @@ def read_required(
193194
194195
195196def write_required (
196- validator : Validator ,
197- required : List [str ],
197+ validator : Any ,
198+ required : list [str ],
198199 instance : Any ,
199- schema : Mapping [Hashable , Any ],
200+ schema : Mapping [str , Any ],
200201) -> Iterator [ValidationError ]:
201202 if not validator .is_type (instance , "object" ):
202203 return
@@ -211,10 +212,10 @@ def write_required(
211212
212213
213214def additionalProperties (
214- validator : Validator ,
215- aP : Union [ Mapping [ Hashable , Any ], bool ] ,
215+ validator : Any ,
216+ aP : Any ,
216217 instance : Any ,
217- schema : Mapping [Hashable , Any ],
218+ schema : Mapping [str , Any ],
218219) -> Iterator [ValidationError ]:
219220 if not validator .is_type (instance , "object" ):
220221 return
@@ -235,28 +236,27 @@ def additionalProperties(
235236
236237
237238def write_readOnly (
238- validator : Validator ,
239+ validator : Any ,
239240 ro : bool ,
240241 instance : Any ,
241- schema : Mapping [Hashable , Any ],
242+ schema : Mapping [str , Any ],
242243) -> Iterator [ValidationError ]:
243244 yield ValidationError (f"Tried to write read-only property with { instance } " )
244245
245246
246247def read_writeOnly (
247- validator : Validator ,
248+ validator : Any ,
248249 wo : bool ,
249250 instance : Any ,
250- schema : Mapping [Hashable , Any ],
251+ schema : Mapping [str , Any ],
251252) -> Iterator [ValidationError ]:
252253 yield ValidationError (f"Tried to read write-only property with { instance } " )
253254
254255
255256def not_implemented (
256- validator : Validator ,
257+ validator : Any ,
257258 value : Any ,
258259 instance : Any ,
259- schema : Mapping [Hashable , Any ],
260+ schema : Mapping [str , Any ],
260261) -> Iterator [ValidationError ]:
261- return
262- yield
262+ yield from ()
0 commit comments