11
11
safe arg.
12
12
13
13
"""
14
+
15
+ from __future__ import annotations
16
+
17
+ try :
18
+ from typing import Any , Union
19
+ except ImportError :
20
+ pass
21
+
14
22
_ALWAYS_SAFE = frozenset (
15
23
b"ABCDEFGHIJKLMNOPQRSTUVWXYZ" b"abcdefghijklmnopqrstuvwxyz" b"0123456789" b"_.-~"
16
24
)
17
25
_ALWAYS_SAFE_BYTES = bytes (_ALWAYS_SAFE )
18
- SAFE_QUOTERS = {}
26
+ SAFE_QUOTERS : dict = {}
19
27
20
28
21
- def quote (bytes_val : bytes , safe = "/" ):
29
+ def quote (bytes_val : bytes , safe : Union [ str , bytes , bytearray ] = "/" ) -> str :
22
30
"""The quote function %-escapes all characters that are neither in the
23
31
unreserved chars ("always safe") nor the additional chars set via the
24
32
safe arg.
@@ -69,34 +77,34 @@ class defaultdict:
69
77
70
78
@staticmethod
71
79
# pylint: disable=W0613
72
- def __new__ (cls , default_factory = None , ** kwargs ) :
80
+ def __new__ (cls , default_factory : Any = None , ** kwargs : Any ) -> defaultdict :
73
81
self = super (defaultdict , cls ).__new__ (cls )
74
82
# pylint: disable=C0103
75
83
self .d = {}
76
84
return self
77
85
78
- def __init__ (self , default_factory = None , ** kwargs ):
86
+ def __init__ (self , default_factory : Any = None , ** kwargs : Any ):
79
87
self .d = kwargs
80
88
self .default_factory = default_factory
81
89
82
- def __getitem__ (self , key ) :
90
+ def __getitem__ (self , key : Any ) -> Any :
83
91
try :
84
92
return self .d [key ]
85
93
except KeyError :
86
94
val = self .__missing__ (key )
87
95
self .d [key ] = val
88
96
return val
89
97
90
- def __setitem__ (self , key , val ) :
98
+ def __setitem__ (self , key : Any , val : Any ) -> None :
91
99
self .d [key ] = val
92
100
93
- def __delitem__ (self , key ) :
101
+ def __delitem__ (self , key : Any ) -> None :
94
102
del self .d [key ]
95
103
96
- def __contains__ (self , key ) :
104
+ def __contains__ (self , key : Any ) -> bool :
97
105
return key in self .d
98
106
99
- def __missing__ (self , key ) :
107
+ def __missing__ (self , key : Any ) -> Any :
100
108
if self .default_factory is None :
101
109
raise KeyError (key )
102
110
return self .default_factory ()
@@ -111,12 +119,12 @@ class Quoter(defaultdict):
111
119
112
120
# Keeps a cache internally, using defaultdict, for efficiency (lookups
113
121
# of cached keys don't call Python code at all).
114
- def __init__ (self , safe ):
122
+ def __init__ (self , safe : Union [ bytes , bytearray ] ):
115
123
"""safe: bytes object."""
116
124
super ().__init__ ()
117
125
self .safe = _ALWAYS_SAFE .union (safe )
118
126
119
- def __missing__ (self , b ) :
127
+ def __missing__ (self , b : int ) -> str :
120
128
# Handle a cache miss. Store quoted string in cache and return.
121
129
res = chr (b ) if b in self .safe else "%{:02X}" .format (b )
122
130
self [b ] = res
0 commit comments