Skip to content

Commit af2800d

Browse files
Add type annotations to quote.py
1 parent fbc1881 commit af2800d

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

adafruit_azureiot/quote.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,22 @@
1111
safe arg.
1212
1313
"""
14+
15+
from __future__ import annotations
16+
17+
try:
18+
from typing import Any, Union
19+
except ImportError:
20+
pass
21+
1422
_ALWAYS_SAFE = frozenset(
1523
b"ABCDEFGHIJKLMNOPQRSTUVWXYZ" b"abcdefghijklmnopqrstuvwxyz" b"0123456789" b"_.-~"
1624
)
1725
_ALWAYS_SAFE_BYTES = bytes(_ALWAYS_SAFE)
18-
SAFE_QUOTERS = {}
26+
SAFE_QUOTERS: dict = {}
1927

2028

21-
def quote(bytes_val: bytes, safe="/"):
29+
def quote(bytes_val: bytes, safe: Union[str, bytes, bytearray] = "/") -> str:
2230
"""The quote function %-escapes all characters that are neither in the
2331
unreserved chars ("always safe") nor the additional chars set via the
2432
safe arg.
@@ -69,34 +77,34 @@ class defaultdict:
6977

7078
@staticmethod
7179
# pylint: disable=W0613
72-
def __new__(cls, default_factory=None, **kwargs):
80+
def __new__(cls, default_factory: Any = None, **kwargs: Any) -> defaultdict:
7381
self = super(defaultdict, cls).__new__(cls)
7482
# pylint: disable=C0103
7583
self.d = {}
7684
return self
7785

78-
def __init__(self, default_factory=None, **kwargs):
86+
def __init__(self, default_factory: Any = None, **kwargs: Any):
7987
self.d = kwargs
8088
self.default_factory = default_factory
8189

82-
def __getitem__(self, key):
90+
def __getitem__(self, key: Any) -> Any:
8391
try:
8492
return self.d[key]
8593
except KeyError:
8694
val = self.__missing__(key)
8795
self.d[key] = val
8896
return val
8997

90-
def __setitem__(self, key, val):
98+
def __setitem__(self, key: Any, val: Any) -> None:
9199
self.d[key] = val
92100

93-
def __delitem__(self, key):
101+
def __delitem__(self, key: Any) -> None:
94102
del self.d[key]
95103

96-
def __contains__(self, key):
104+
def __contains__(self, key: Any) -> bool:
97105
return key in self.d
98106

99-
def __missing__(self, key):
107+
def __missing__(self, key: Any) -> Any:
100108
if self.default_factory is None:
101109
raise KeyError(key)
102110
return self.default_factory()
@@ -111,12 +119,12 @@ class Quoter(defaultdict):
111119

112120
# Keeps a cache internally, using defaultdict, for efficiency (lookups
113121
# of cached keys don't call Python code at all).
114-
def __init__(self, safe):
122+
def __init__(self, safe: Union[bytes, bytearray]):
115123
"""safe: bytes object."""
116124
super().__init__()
117125
self.safe = _ALWAYS_SAFE.union(safe)
118126

119-
def __missing__(self, b):
127+
def __missing__(self, b: int) -> str:
120128
# Handle a cache miss. Store quoted string in cache and return.
121129
res = chr(b) if b in self.safe else "%{:02X}".format(b)
122130
self[b] = res

0 commit comments

Comments
 (0)