1
- from base64 import b64encode , b64decode
2
1
import binascii
2
+ from base64 import b64decode
3
+ from base64 import b64encode
3
4
from datetime import datetime
5
+ from typing import Any
6
+ from typing import Tuple
7
+ from typing import Union
4
8
from uuid import UUID
5
9
6
10
from jsonschema ._format import FormatChecker
9
13
DATETIME_HAS_RFC3339_VALIDATOR = False
10
14
DATETIME_HAS_STRICT_RFC3339 = False
11
15
DATETIME_HAS_ISODATE = False
12
- DATETIME_RAISES = ()
16
+ DATETIME_RAISES : Tuple [ Exception , ...] = ()
13
17
14
18
try :
15
19
import isodate
36
40
DATETIME_RAISES += (ValueError , TypeError )
37
41
38
42
39
- def is_int32 (instance ) :
43
+ def is_int32 (instance : Any ) -> bool :
40
44
return isinstance (instance , int )
41
45
42
46
43
- def is_int64 (instance ) :
47
+ def is_int64 (instance : Any ) -> bool :
44
48
return isinstance (instance , int )
45
49
46
50
47
- def is_float (instance ) :
51
+ def is_float (instance : Any ) -> bool :
48
52
return isinstance (instance , float )
49
53
50
54
51
- def is_double (instance ) :
55
+ def is_double (instance : Any ) -> bool :
52
56
# float has double precision in Python
53
57
# It's double in CPython and Jython
54
58
return isinstance (instance , float )
55
59
56
60
57
- def is_binary (instance ) :
61
+ def is_binary (instance : Any ) -> bool :
58
62
return isinstance (instance , bytes )
59
63
60
64
61
- def is_byte (instance ) :
65
+ def is_byte (instance : Union [ str , bytes ]) -> bool :
62
66
if isinstance (instance , str ):
63
67
instance = instance .encode ()
64
68
65
69
try :
66
- return b64encode (b64decode (instance )) == instance
70
+ encoded = b64encode (b64decode (instance ))
67
71
except TypeError :
68
72
return False
73
+ else :
74
+ return encoded == instance
69
75
70
76
71
- def is_datetime (instance ) :
77
+ def is_datetime (instance : str ) -> bool :
72
78
if not isinstance (instance , (bytes , str )):
73
79
return False
74
80
75
81
if DATETIME_HAS_RFC3339_VALIDATOR :
76
- return validate_rfc3339 (instance )
82
+ return bool ( validate_rfc3339 (instance ) )
77
83
78
84
if DATETIME_HAS_STRICT_RFC3339 :
79
- return strict_rfc3339 .validate_rfc3339 (instance )
85
+ return bool ( strict_rfc3339 .validate_rfc3339 (instance ) )
80
86
81
87
if DATETIME_HAS_ISODATE :
82
- return isodate .parse_datetime (instance )
88
+ return bool ( isodate .parse_datetime (instance ) )
83
89
84
90
return True
85
91
86
92
87
- def is_date (instance ) :
93
+ def is_date (instance : Any ) -> bool :
88
94
if not isinstance (instance , (bytes , str )):
89
95
return False
90
96
91
97
if isinstance (instance , bytes ):
92
98
instance = instance .decode ()
93
99
94
- return datetime .strptime (instance , "%Y-%m-%d" )
100
+ return bool ( datetime .strptime (instance , "%Y-%m-%d" ) )
95
101
96
102
97
- def is_uuid (instance ) :
103
+ def is_uuid (instance : Any ) -> bool :
98
104
if not isinstance (instance , (bytes , str )):
99
105
return False
100
106
@@ -104,41 +110,43 @@ def is_uuid(instance):
104
110
return str (UUID (instance )).lower () == instance .lower ()
105
111
106
112
107
- def is_password (instance ) :
113
+ def is_password (instance : Any ) -> bool :
108
114
return True
109
115
110
116
111
- class OASFormatChecker (FormatChecker ):
117
+ class OASFormatChecker (FormatChecker ): # type: ignore
112
118
113
119
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 , ()),
123
129
# non standard
124
- ' uuid' : (is_uuid , (AttributeError , ValueError )),
130
+ " uuid" : (is_uuid , (AttributeError , ValueError )),
125
131
}
126
132
127
- def check (self , instance , format ) :
133
+ def check (self , instance : Any , format : str ) -> Any :
128
134
if format not in self .checkers :
129
135
raise FormatError (
130
- "Format checker for %r format not found" % (format , ))
136
+ f"Format checker for { format !r} format not found"
137
+ )
131
138
132
139
func , raises = self .checkers [format ]
133
140
result , cause = None , None
134
141
try :
135
142
result = func (instance )
136
- except raises as e :
143
+ except raises as e : # type: ignore
137
144
cause = e
138
145
139
146
if not result :
140
147
raise FormatError (
141
- "%r is not a %r" % (instance , format ), cause = cause ,
148
+ f"{ instance !r} is not a { format !r} " ,
149
+ cause = cause ,
142
150
)
143
151
return result
144
152
0 commit comments