11import os
22import unittest
3- from typing import Any , Union , Type , Callable , cast , Tuple , List
3+ from typing import Any , Optional , Union , Type , Callable , cast , Tuple , List
44from urllib .parse import urlparse
55
66import pytest
2929 url_query_cleaner ,
3030)
3131
32-
33- UNSET = object ()
34-
3532# Test cases for URL-to-safe-URL conversions with a URL and an encoding as
3633# input parameters.
3734#
3835# (encoding, input URL, output URL or exception)
39- SAFE_URL_ENCODING_CASES : List [Tuple [Any , StrOrBytes , Union [str , Type [Exception ]]]] = [
40- (UNSET , "" , ValueError ),
41- (UNSET , "https://example.com" , "https://example.com" ),
42- (UNSET , "https://example.com/©" , "https://example.com/%C2%A9" ),
36+ SAFE_URL_ENCODING_CASES : List [
37+ Tuple [Optional [str ], StrOrBytes , Union [str , Type [Exception ]]]
38+ ] = [
39+ (None , "" , ValueError ),
40+ (None , "https://example.com" , "https://example.com" ),
41+ (None , "https://example.com/©" , "https://example.com/%C2%A9" ),
4342 # Paths are always UTF-8-encoded.
4443 ("iso-8859-1" , "https://example.com/©" , "https://example.com/%C2%A9" ),
4544 # Queries are UTF-8-encoded if the scheme is not special, ws or wss.
317316)
318317
319318
320- # encoding is actually either str or literally UNSET, but typing.Literal
321- # doesn't support literals of arbitrary types
322319def _test_safe_url_func (
323320 url : StrOrBytes ,
324321 * ,
325- encoding : Any = UNSET ,
322+ encoding : Optional [ str ] = None ,
326323 output : Union [str , Type [Exception ]],
327324 func : Callable [..., str ],
328325) -> None :
329326 kwargs = {}
330- if encoding is not UNSET :
327+ if encoding is not None :
331328 kwargs ["encoding" ] = encoding
332329 try :
333330 is_exception = issubclass (cast (Type [Exception ], output ), Exception )
@@ -343,7 +340,10 @@ def _test_safe_url_func(
343340
344341
345342def _test_safe_url_string (
346- url : StrOrBytes , * , encoding : Any = UNSET , output : Union [str , Type [Exception ]]
343+ url : StrOrBytes ,
344+ * ,
345+ encoding : Optional [str ] = None ,
346+ output : Union [str , Type [Exception ]],
347347) -> None :
348348 return _test_safe_url_func (
349349 url ,
@@ -354,7 +354,7 @@ def _test_safe_url_string(
354354
355355
356356KNOWN_SAFE_URL_STRING_ENCODING_ISSUES = {
357- (UNSET , "" ), # Invalid URL
357+ (None , "" ), # Invalid URL
358358 # UTF-8 encoding is not enforced in non-special URLs, or in URLs with the
359359 # ws or wss schemas.
360360 ("iso-8859-1" , "a://example.com?\xa9 " ),
0 commit comments