2929)
3030
3131
32- def _validate_hostname (
33- host ,
34- ):
32+ def _validate_hostname (host ):
3533 if not host :
3634 return False
37-
3835 if len (host ) > 253 :
3936 return False
4037
@@ -57,11 +54,7 @@ def _validate_hostname(
5754 return not all_digits
5855
5956
60- def validate_host_and_port (
61- string : str ,
62- * ,
63- port_required : bool ,
64- ) -> bool :
57+ def validate_host_and_port (string : str , * , port_required : bool ) -> bool :
6558 if not string :
6659 return False
6760
@@ -70,51 +63,28 @@ def validate_host_and_port(
7063 end = string .find ("]" )
7164 after_end = end + 1
7265 if after_end == len (string ): # no port
73- return not port_required and validate_ip (
74- string [1 :end ],
75- 6 ,
76- )
66+ return not port_required and validate_ip (string [1 :end ], 6 )
7767 if after_end == split_idx : # port
7868 return validate_ip (string [1 :end ]) and validate_port (string [split_idx + 1 :])
7969 return False # malformed
8070
8171 if split_idx == - 1 :
82- return not port_required and (
83- _validate_hostname (string )
84- or validate_ip (
85- string ,
86- 4 ,
87- )
88- )
72+ return not port_required and (_validate_hostname (string ) or validate_ip (string , 4 ))
8973
9074 host = string [:split_idx ]
9175 port = string [split_idx + 1 :]
92- return (
93- _validate_hostname (host )
94- or validate_ip (
95- host ,
96- 4 ,
97- )
98- ) and validate_port (port )
99-
100-
101- def validate_port (
102- val : str ,
103- ) -> bool :
76+ return (_validate_hostname (host ) or validate_ip (host , 4 )) and validate_port (port )
77+
78+
79+ def validate_port (val : str ) -> bool :
10480 try :
10581 port = int (val )
10682 return port <= 65535
10783 except ValueError :
10884 return False
10985
11086
111- def validate_ip (
112- val : typing .Union [
113- str ,
114- bytes ,
115- ],
116- version : typing .Optional [int ] = None ,
117- ) -> bool :
87+ def validate_ip (val : typing .Union [str , bytes ], version : typing .Optional [int ] = None ) -> bool :
11888 try :
11989 if version is None :
12090 ip_address (val )
@@ -130,114 +100,42 @@ def validate_ip(
130100 return False
131101
132102
133- def is_ip (
134- val : celtypes .Value ,
135- version : typing .Optional [celtypes .Value ] = None ,
136- ) -> celpy .Result :
137- if not isinstance (
138- val ,
139- (
140- celtypes .BytesType ,
141- celtypes .StringType ,
142- ),
143- ):
103+ def is_ip (val : celtypes .Value , version : typing .Optional [celtypes .Value ] = None ) -> celpy .Result :
104+ if not isinstance (val , (celtypes .BytesType , celtypes .StringType )):
144105 msg = "invalid argument, expected string or bytes"
145106 raise celpy .CELEvalError (msg )
146- if (
147- not isinstance (
148- version ,
149- celtypes .IntType ,
150- )
151- and version is not None
152- ):
107+ if not isinstance (version , celtypes .IntType ) and version is not None :
153108 msg = "invalid argument, expected int"
154109 raise celpy .CELEvalError (msg )
155- return celtypes .BoolType (
156- validate_ip (
157- val ,
158- version ,
159- )
160- )
161-
162-
163- def is_ip_prefix (
164- val : celtypes .Value ,
165- * args ,
166- ) -> celpy .Result :
167- if not isinstance (
168- val ,
169- (
170- celtypes .BytesType ,
171- celtypes .StringType ,
172- ),
173- ):
110+ return celtypes .BoolType (validate_ip (val , version ))
111+
112+
113+ def is_ip_prefix (val : celtypes .Value , * args ) -> celpy .Result :
114+ if not isinstance (val , (celtypes .BytesType , celtypes .StringType )):
174115 msg = "invalid argument, expected string or bytes"
175116 raise celpy .CELEvalError (msg )
176117 version = None
177118 strict = celtypes .BoolType (False )
178- if len (args ) == 1 and isinstance (
179- args [0 ],
180- celtypes .BoolType ,
181- ):
119+ if len (args ) == 1 and isinstance (args [0 ], celtypes .BoolType ):
182120 strict = args [0 ]
183- elif len (args ) == 1 and isinstance (
184- args [0 ],
185- celtypes .IntType ,
186- ):
121+ elif len (args ) == 1 and isinstance (args [0 ], celtypes .IntType ):
187122 version = args [0 ]
188- elif len (args ) == 1 and (
189- not isinstance (
190- args [0 ],
191- celtypes .BoolType ,
192- )
193- or not isinstance (
194- args [0 ],
195- celtypes .IntType ,
196- )
197- ):
123+ elif len (args ) == 1 and (not isinstance (args [0 ], celtypes .BoolType ) or not isinstance (args [0 ], celtypes .IntType )):
198124 msg = "invalid argument, expected bool or int"
199125 raise celpy .CELEvalError (msg )
200- elif (
201- len (args ) == 2
202- and isinstance (
203- args [0 ],
204- celtypes .IntType ,
205- )
206- and isinstance (
207- args [1 ],
208- celtypes .BoolType ,
209- )
210- ):
126+ elif len (args ) == 2 and isinstance (args [0 ], celtypes .IntType ) and isinstance (args [1 ], celtypes .BoolType ):
211127 version = args [0 ]
212128 strict = args [1 ]
213- elif len (args ) == 2 and (
214- not isinstance (
215- args [0 ],
216- celtypes .IntType ,
217- )
218- or not isinstance (
219- args [1 ],
220- celtypes .BoolType ,
221- )
222- ):
129+ elif len (args ) == 2 and (not isinstance (args [0 ], celtypes .IntType ) or not isinstance (args [1 ], celtypes .BoolType )):
223130 msg = "invalid argument, expected int and bool"
224131 raise celpy .CELEvalError (msg )
225132 try :
226133 if version is None :
227- ip_network (
228- val ,
229- strict = bool (strict ),
230- )
134+ ip_network (val , strict = bool (strict ))
231135 elif version == 4 :
232- IPv4Network (
233- val ,
234- strict = bool (strict ),
235- )
136+ IPv4Network (val , strict = bool (strict ))
236137 elif version == 6 :
237- IPv6Network (
238- val ,
239- strict = bool (strict ),
240- )
138+ IPv6Network (val , strict = bool (strict ))
241139 else :
242140 msg = "invalid argument, expected 4 or 6"
243141 raise celpy .CELEvalError (msg )
@@ -246,35 +144,22 @@ def is_ip_prefix(
246144 return celtypes .BoolType (False )
247145
248146
249- def is_email (
250- string : celtypes .Value ,
251- ) -> celpy .Result :
252- if not isinstance (
253- string ,
254- celtypes .StringType ,
255- ):
147+ def is_email (string : celtypes .Value ) -> celpy .Result :
148+ if not isinstance (string , celtypes .StringType ):
256149 msg = "invalid argument, expected string"
257150 raise celpy .CELEvalError (msg )
258151 m = _email_regex .match (string ) is not None
259152 return celtypes .BoolType (m )
260153
261154
262- def is_uri (
263- string : celtypes .Value ,
264- ) -> celpy .Result :
155+ def is_uri (string : celtypes .Value ) -> celpy .Result :
265156 url = urlparse .urlparse (str (string ))
266157 # urlparse correctly reads the scheme from URNs but parses everything
267158 # after (except the query string) as the path.
268159 if url .scheme == "urn" :
269160 if not (url .path ):
270161 return celtypes .BoolType (False )
271- elif not all (
272- [
273- url .scheme ,
274- url .netloc ,
275- url .path ,
276- ]
277- ):
162+ elif not all ([url .scheme , url .netloc , url .path ]):
278163 return celtypes .BoolType (False )
279164
280165 # If the query string contains percent-encoding, then try to decode it.
@@ -285,88 +170,45 @@ def is_uri(
285170 return celtypes .BoolType (True )
286171
287172
288- def is_uri_ref (
289- string : celtypes .Value ,
290- ) -> celpy .Result :
173+ def is_uri_ref (string : celtypes .Value ) -> celpy .Result :
291174 url = urlparse .urlparse (str (string ))
292- if (
293- not all (
294- [
295- url .scheme ,
296- url .path ,
297- ]
298- )
299- and url .fragment
300- ):
175+ if not all ([url .scheme , url .path ]) and url .fragment :
301176 return celtypes .BoolType (False )
302177 return celtypes .BoolType (True )
303178
304179
305- def is_hostname (
306- string : celtypes .Value ,
307- ) -> celpy .Result :
308- if not isinstance (
309- string ,
310- celtypes .StringType ,
311- ):
180+ def is_hostname (string : celtypes .Value ) -> celpy .Result :
181+ if not isinstance (string , celtypes .StringType ):
312182 msg = "invalid argument, expected string"
313183 raise celpy .CELEvalError (msg )
314184 return celtypes .BoolType (_validate_hostname (string ))
315185
316186
317- def is_host_and_port (
318- string : celtypes .Value ,
319- port_required : celtypes .Value ,
320- ) -> celpy .Result :
321- if not isinstance (
322- string ,
323- celtypes .StringType ,
324- ):
187+ def is_host_and_port (string : celtypes .Value , port_required : celtypes .Value ) -> celpy .Result :
188+ if not isinstance (string , celtypes .StringType ):
325189 msg = "invalid argument, expected string"
326190 raise celpy .CELEvalError (msg )
327- if not isinstance (
328- port_required ,
329- celtypes .BoolType ,
330- ):
191+ if not isinstance (port_required , celtypes .BoolType ):
331192 msg = "invalid argument, expected bool"
332193 raise celpy .CELEvalError (msg )
333- return celtypes .BoolType (
334- validate_host_and_port (
335- string ,
336- port_required = bool (port_required ),
337- )
338- )
339-
340-
341- def is_nan (
342- val : celtypes .Value ,
343- ) -> celpy .Result :
344- if not isinstance (
345- val ,
346- celtypes .DoubleType ,
347- ):
194+ return celtypes .BoolType (validate_host_and_port (string , port_required = bool (port_required )))
195+
196+
197+ def is_nan (val : celtypes .Value ) -> celpy .Result :
198+ if not isinstance (val , celtypes .DoubleType ):
348199 msg = "invalid argument, expected double"
349200 raise celpy .CELEvalError (msg )
350201 return celtypes .BoolType (math .isnan (val ))
351202
352203
353- def is_inf (
354- val : celtypes .Value ,
355- sign : typing .Optional [celtypes .Value ] = None ,
356- ) -> celpy .Result :
357- if not isinstance (
358- val ,
359- celtypes .DoubleType ,
360- ):
204+ def is_inf (val : celtypes .Value , sign : typing .Optional [celtypes .Value ] = None ) -> celpy .Result :
205+ if not isinstance (val , celtypes .DoubleType ):
361206 msg = "invalid argument, expected double"
362207 raise celpy .CELEvalError (msg )
363208 if sign is None :
364209 return celtypes .BoolType (math .isinf (val ))
365210
366- if not isinstance (
367- sign ,
368- celtypes .IntType ,
369- ):
211+ if not isinstance (sign , celtypes .IntType ):
370212 msg = "invalid argument, expected int"
371213 raise celpy .CELEvalError (msg )
372214 if sign > 0 :
@@ -377,24 +219,14 @@ def is_inf(
377219 return celtypes .BoolType (math .isinf (val ))
378220
379221
380- def unique (
381- val : celtypes .Value ,
382- ) -> celpy .Result :
383- if not isinstance (
384- val ,
385- celtypes .ListType ,
386- ):
222+ def unique (val : celtypes .Value ) -> celpy .Result :
223+ if not isinstance (val , celtypes .ListType ):
387224 msg = "invalid argument, expected list"
388225 raise celpy .CELEvalError (msg )
389226 return celtypes .BoolType (len (val ) == len (set (val )))
390227
391228
392- def make_extra_funcs (
393- locale : str ,
394- ) -> dict [
395- str ,
396- celpy .CELFunction ,
397- ]:
229+ def make_extra_funcs (locale : str ) -> dict [str , celpy .CELFunction ]:
398230 # TODO(#257): Fix types and add tests for StringFormat.
399231 # For now, ignoring the type.
400232 string_fmt = string_format .StringFormat (locale ) # type: ignore
0 commit comments