1- from base64 import b64encode , b64decode
21import binascii
2+ from base64 import b64decode
3+ from base64 import b64encode
34from datetime import datetime
5+ from typing import Any
6+ from typing import Tuple
7+ from typing import Union
48from uuid import UUID
59
610from jsonschema ._format import FormatChecker
913DATETIME_HAS_RFC3339_VALIDATOR = False
1014DATETIME_HAS_STRICT_RFC3339 = False
1115DATETIME_HAS_ISODATE = False
12- DATETIME_RAISES = ()
16+ DATETIME_RAISES : Tuple [ Exception , ...] = ()
1317
1418try :
1519 import isodate
3640 DATETIME_RAISES += (ValueError , TypeError )
3741
3842
39- def is_int32 (instance ) :
43+ def is_int32 (instance : Any ) -> bool :
4044 return isinstance (instance , int )
4145
4246
43- def is_int64 (instance ) :
47+ def is_int64 (instance : Any ) -> bool :
4448 return isinstance (instance , int )
4549
4650
47- def is_float (instance ) :
51+ def is_float (instance : Any ) -> bool :
4852 return isinstance (instance , float )
4953
5054
51- def is_double (instance ) :
55+ def is_double (instance : Any ) -> bool :
5256 # float has double precision in Python
5357 # It's double in CPython and Jython
5458 return isinstance (instance , float )
5559
5660
57- def is_binary (instance ) :
61+ def is_binary (instance : Any ) -> bool :
5862 return isinstance (instance , bytes )
5963
6064
61- def is_byte (instance ) :
65+ def is_byte (instance : Union [ str , bytes ]) -> bool :
6266 if isinstance (instance , str ):
6367 instance = instance .encode ()
6468
6569 try :
66- return b64encode (b64decode (instance )) == instance
70+ encoded = b64encode (b64decode (instance ))
6771 except TypeError :
6872 return False
73+ else :
74+ return encoded == instance
6975
7076
71- def is_datetime (instance ) :
77+ def is_datetime (instance : str ) -> bool :
7278 if not isinstance (instance , (bytes , str )):
7379 return False
7480
7581 if DATETIME_HAS_RFC3339_VALIDATOR :
76- return validate_rfc3339 (instance )
82+ return bool ( validate_rfc3339 (instance ) )
7783
7884 if DATETIME_HAS_STRICT_RFC3339 :
79- return strict_rfc3339 .validate_rfc3339 (instance )
85+ return bool ( strict_rfc3339 .validate_rfc3339 (instance ) )
8086
8187 if DATETIME_HAS_ISODATE :
82- return isodate .parse_datetime (instance )
88+ return bool ( isodate .parse_datetime (instance ) )
8389
8490 return True
8591
8692
87- def is_date (instance ) :
93+ def is_date (instance : Any ) -> bool :
8894 if not isinstance (instance , (bytes , str )):
8995 return False
9096
9197 if isinstance (instance , bytes ):
9298 instance = instance .decode ()
9399
94- return datetime .strptime (instance , "%Y-%m-%d" )
100+ return bool ( datetime .strptime (instance , "%Y-%m-%d" ) )
95101
96102
97- def is_uuid (instance ) :
103+ def is_uuid (instance : Any ) -> bool :
98104 if not isinstance (instance , (bytes , str )):
99105 return False
100106
@@ -104,41 +110,43 @@ def is_uuid(instance):
104110 return str (UUID (instance )).lower () == instance .lower ()
105111
106112
107- def is_password (instance ) :
113+ def is_password (instance : Any ) -> bool :
108114 return True
109115
110116
111- class OASFormatChecker (FormatChecker ):
117+ class OASFormatChecker (FormatChecker ): # type: ignore
112118
113119 checkers = {
114- ' int32' : (is_int32 , ()),
115- ' int64' : (is_int64 , ()),
116- ' float' : (is_float , ()),
117- ' double' : (is_double , ()),
118- ' byte' : (is_byte , (binascii .Error , TypeError )),
119- ' binary' : (is_binary , ()),
120- ' date' : (is_date , (ValueError , )),
121- ' date-time' : (is_datetime , DATETIME_RAISES ),
122- ' password' : (is_password , ()),
120+ " int32" : (is_int32 , ()),
121+ " int64" : (is_int64 , ()),
122+ " float" : (is_float , ()),
123+ " double" : (is_double , ()),
124+ " byte" : (is_byte , (binascii .Error , TypeError )),
125+ " binary" : (is_binary , ()),
126+ " date" : (is_date , (ValueError ,)),
127+ " date-time" : (is_datetime , DATETIME_RAISES ),
128+ " password" : (is_password , ()),
123129 # non standard
124- ' uuid' : (is_uuid , (AttributeError , ValueError )),
130+ " uuid" : (is_uuid , (AttributeError , ValueError )),
125131 }
126132
127- def check (self , instance , format ) :
133+ def check (self , instance : Any , format : str ) -> Any :
128134 if format not in self .checkers :
129135 raise FormatError (
130- "Format checker for %r format not found" % (format , ))
136+ f"Format checker for { format !r} format not found"
137+ )
131138
132139 func , raises = self .checkers [format ]
133140 result , cause = None , None
134141 try :
135142 result = func (instance )
136- except raises as e :
143+ except raises as e : # type: ignore
137144 cause = e
138145
139146 if not result :
140147 raise FormatError (
141- "%r is not a %r" % (instance , format ), cause = cause ,
148+ f"{ instance !r} is not a { format !r} " ,
149+ cause = cause ,
142150 )
143151 return result
144152
0 commit comments