Skip to content

Commit 467f3d9

Browse files
committed
refactor(query_list): use typing namespace as t
1 parent 3d70a2c commit 467f3d9

File tree

1 file changed

+53
-59
lines changed

1 file changed

+53
-59
lines changed

src/libtmux/_internal/query_list.py

Lines changed: 53 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,24 @@
66
"""
77
import re
88
import traceback
9+
import typing as t
910
from collections.abc import Mapping, Sequence
10-
from re import Pattern
11-
from typing import (
12-
TYPE_CHECKING,
13-
Any,
14-
Callable,
15-
List,
16-
Optional,
17-
Protocol,
18-
TypeVar,
19-
Union,
20-
)
21-
22-
if TYPE_CHECKING:
23-
24-
class LookupProtocol(Protocol):
11+
12+
if t.TYPE_CHECKING:
13+
14+
class LookupProtocol(t.Protocol):
2515
"""Protocol for :class:`QueryList` filtering operators."""
2616

2717
def __call__(
2818
self,
29-
data: Union[str, List[str], "Mapping[str, str]"],
30-
rhs: Union[str, List[str], "Mapping[str, str]", "Pattern[str]"],
19+
data: t.Union[str, t.List[str], "Mapping[str, str]"],
20+
rhs: t.Union[str, t.List[str], "Mapping[str, str]", "re.Pattern[str]"],
3121
) -> bool:
3222
"""Callback for :class:`QueryList` filtering operators."""
3323
...
3424

3525

36-
T = TypeVar("T", Any, Any)
26+
T = t.TypeVar("T", t.Any, t.Any)
3727

3828
no_arg = object()
3929

@@ -47,9 +37,9 @@ class ObjectDoesNotExist(Exception):
4737

4838

4939
def keygetter(
50-
obj: "Mapping[str, Any]",
40+
obj: "Mapping[str, t.Any]",
5141
path: str,
52-
) -> Union[None, Any, str, List[str], "Mapping[str, str]"]:
42+
) -> t.Union[None, t.Any, str, t.List[str], "Mapping[str, str]"]:
5343
"""obj, "foods__breakfast", obj['foods']['breakfast']
5444
5545
>>> keygetter({ "foods": { "breakfast": "cereal" } }, "foods__breakfast")
@@ -75,7 +65,9 @@ def keygetter(
7565
return dct
7666

7767

78-
def parse_lookup(obj: "Mapping[str, Any]", path: str, lookup: str) -> Optional[Any]:
68+
def parse_lookup(
69+
obj: "Mapping[str, t.Any]", path: str, lookup: str
70+
) -> t.Optional[t.Any]:
7971
"""Check if field lookup key, e.g. "my__path__contains" has comparator, return val.
8072
8173
If comparator not used or value not found, return None.
@@ -96,15 +88,15 @@ def parse_lookup(obj: "Mapping[str, Any]", path: str, lookup: str) -> Optional[A
9688

9789

9890
def lookup_exact(
99-
data: Union[str, List[str], "Mapping[str, str]"],
100-
rhs: Union[str, List[str], "Mapping[str, str]", "Pattern[str]"],
91+
data: t.Union[str, t.List[str], "Mapping[str, str]"],
92+
rhs: t.Union[str, t.List[str], "Mapping[str, str]", "re.Pattern[str]"],
10193
) -> bool:
10294
return rhs == data
10395

10496

10597
def lookup_iexact(
106-
data: Union[str, List[str], "Mapping[str, str]"],
107-
rhs: Union[str, List[str], "Mapping[str, str]", "Pattern[str]"],
98+
data: t.Union[str, t.List[str], "Mapping[str, str]"],
99+
rhs: t.Union[str, t.List[str], "Mapping[str, str]", "re.Pattern[str]"],
108100
) -> bool:
109101
if not isinstance(rhs, str) or not isinstance(data, str):
110102
return False
@@ -113,8 +105,8 @@ def lookup_iexact(
113105

114106

115107
def lookup_contains(
116-
data: Union[str, List[str], "Mapping[str, str]"],
117-
rhs: Union[str, List[str], "Mapping[str, str]", "Pattern[str]"],
108+
data: t.Union[str, t.List[str], "Mapping[str, str]"],
109+
rhs: t.Union[str, t.List[str], "Mapping[str, str]", "re.Pattern[str]"],
118110
) -> bool:
119111
if not isinstance(rhs, str) or not isinstance(data, (str, Mapping, list)):
120112
return False
@@ -123,8 +115,8 @@ def lookup_contains(
123115

124116

125117
def lookup_icontains(
126-
data: Union[str, List[str], "Mapping[str, str]"],
127-
rhs: Union[str, List[str], "Mapping[str, str]", "Pattern[str]"],
118+
data: t.Union[str, t.List[str], "Mapping[str, str]"],
119+
rhs: t.Union[str, t.List[str], "Mapping[str, str]", "re.Pattern[str]"],
128120
) -> bool:
129121
if not isinstance(rhs, str) or not isinstance(data, (str, Mapping, list)):
130122
return False
@@ -138,8 +130,8 @@ def lookup_icontains(
138130

139131

140132
def lookup_startswith(
141-
data: Union[str, List[str], "Mapping[str, str]"],
142-
rhs: Union[str, List[str], "Mapping[str, str]", "Pattern[str]"],
133+
data: t.Union[str, t.List[str], "Mapping[str, str]"],
134+
rhs: t.Union[str, t.List[str], "Mapping[str, str]", "re.Pattern[str]"],
143135
) -> bool:
144136
if not isinstance(rhs, str) or not isinstance(data, str):
145137
return False
@@ -148,8 +140,8 @@ def lookup_startswith(
148140

149141

150142
def lookup_istartswith(
151-
data: Union[str, List[str], "Mapping[str, str]"],
152-
rhs: Union[str, List[str], "Mapping[str, str]", "Pattern[str]"],
143+
data: t.Union[str, t.List[str], "Mapping[str, str]"],
144+
rhs: t.Union[str, t.List[str], "Mapping[str, str]", "re.Pattern[str]"],
153145
) -> bool:
154146
if not isinstance(rhs, str) or not isinstance(data, str):
155147
return False
@@ -158,8 +150,8 @@ def lookup_istartswith(
158150

159151

160152
def lookup_endswith(
161-
data: Union[str, List[str], "Mapping[str, str]"],
162-
rhs: Union[str, List[str], "Mapping[str, str]", "Pattern[str]"],
153+
data: t.Union[str, t.List[str], "Mapping[str, str]"],
154+
rhs: t.Union[str, t.List[str], "Mapping[str, str]", "re.Pattern[str]"],
163155
) -> bool:
164156
if not isinstance(rhs, str) or not isinstance(data, str):
165157
return False
@@ -168,17 +160,17 @@ def lookup_endswith(
168160

169161

170162
def lookup_iendswith(
171-
data: Union[str, List[str], "Mapping[str, str]"],
172-
rhs: Union[str, List[str], "Mapping[str, str]", "Pattern[str]"],
163+
data: t.Union[str, t.List[str], "Mapping[str, str]"],
164+
rhs: t.Union[str, t.List[str], "Mapping[str, str]", "re.Pattern[str]"],
173165
) -> bool:
174166
if not isinstance(rhs, str) or not isinstance(data, str):
175167
return False
176168
return data.lower().endswith(rhs.lower())
177169

178170

179171
def lookup_in(
180-
data: Union[str, List[str], "Mapping[str, str]"],
181-
rhs: Union[str, List[str], "Mapping[str, str]", "Pattern[str]"],
172+
data: t.Union[str, t.List[str], "Mapping[str, str]"],
173+
rhs: t.Union[str, t.List[str], "Mapping[str, str]", "re.Pattern[str]"],
182174
) -> bool:
183175
if isinstance(rhs, list):
184176
return data in rhs
@@ -199,8 +191,8 @@ def lookup_in(
199191

200192

201193
def lookup_nin(
202-
data: Union[str, List[str], "Mapping[str, str]"],
203-
rhs: Union[str, List[str], "Mapping[str, str]", "Pattern[str]"],
194+
data: t.Union[str, t.List[str], "Mapping[str, str]"],
195+
rhs: t.Union[str, t.List[str], "Mapping[str, str]", "re.Pattern[str]"],
204196
) -> bool:
205197
if isinstance(rhs, list):
206198
return data not in rhs
@@ -221,17 +213,17 @@ def lookup_nin(
221213

222214

223215
def lookup_regex(
224-
data: Union[str, List[str], "Mapping[str, str]"],
225-
rhs: Union[str, List[str], "Mapping[str, str]", "Pattern[str]"],
216+
data: t.Union[str, t.List[str], "Mapping[str, str]"],
217+
rhs: t.Union[str, t.List[str], "Mapping[str, str]", "re.Pattern[str]"],
226218
) -> bool:
227219
if isinstance(data, (str, bytes, re.Pattern)) and isinstance(rhs, (str, bytes)):
228220
return bool(re.search(rhs, data))
229221
return False
230222

231223

232224
def lookup_iregex(
233-
data: Union[str, List[str], "Mapping[str, str]"],
234-
rhs: Union[str, List[str], "Mapping[str, str]", "Pattern[str]"],
225+
data: t.Union[str, t.List[str], "Mapping[str, str]"],
226+
rhs: t.Union[str, t.List[str], "Mapping[str, str]", "re.Pattern[str]"],
235227
) -> bool:
236228
if isinstance(data, (str, bytes, re.Pattern)) and isinstance(rhs, (str, bytes)):
237229
return bool(re.search(rhs, data, re.IGNORECASE))
@@ -265,7 +257,7 @@ def __init__(self, op: str, *args: object):
265257
return super().__init__(f"{op} not in LOOKUP_NAME_MAP")
266258

267259

268-
class QueryList(List[T]):
260+
class QueryList(t.List[T]):
269261
"""Filter list of object/dictionaries. For small, local datasets.
270262
271263
*Experimental, unstable*.
@@ -301,21 +293,21 @@ class QueryList(List[T]):
301293
"""
302294

303295
data: "Sequence[T]"
304-
pk_key: Optional[str]
296+
pk_key: t.Optional[str]
305297

306-
def items(self) -> List[T]:
298+
def items(self) -> t.List[T]:
307299
if self.pk_key is None:
308300
raise PKRequiredException()
309301
return [(getattr(item, self.pk_key), item) for item in self]
310302

311303
def __eq__(
312304
self,
313305
other: object,
314-
# other: Union[
306+
# other: t.Union[
315307
# "QueryList[T]",
316-
# List[Mapping[str, str]],
317-
# List[Mapping[str, int]],
318-
# List[Mapping[str, Union[str, Mapping[str, Union[List[str], str]]]]],
308+
# t.List[Mapping[str, str]],
309+
# t.List[Mapping[str, int]],
310+
# t.List[Mapping[str, t.Union[str, Mapping[str, t.Union[List[str], str]]]]],
319311
# ],
320312
) -> bool:
321313
data = other
@@ -339,11 +331,13 @@ def __eq__(
339331
return False
340332

341333
def filter(
342-
self, matcher: Optional[Union[Callable[[T], bool], T]] = None, **kwargs: Any
334+
self,
335+
matcher: t.Optional[t.Union[t.Callable[[T], bool], T]] = None,
336+
**kwargs: t.Any,
343337
) -> "QueryList[T]":
344338
"""Filter list of objects."""
345339

346-
def filter_lookup(obj: Any) -> bool:
340+
def filter_lookup(obj: t.Any) -> bool:
347341
for path, v in kwargs.items():
348342
try:
349343
lhs, op = path.rsplit("__", 1)
@@ -367,7 +361,7 @@ def filter_lookup(obj: Any) -> bool:
367361
_filter = matcher
368362
elif matcher is not None:
369363

370-
def val_match(obj: Union[str, List[Any]]) -> bool:
364+
def val_match(obj: t.Union[str, t.List[t.Any]]) -> bool:
371365
if isinstance(matcher, list):
372366
return obj in matcher
373367
else:
@@ -381,10 +375,10 @@ def val_match(obj: Union[str, List[Any]]) -> bool:
381375

382376
def get(
383377
self,
384-
matcher: Optional[Union[Callable[[T], bool], T]] = None,
385-
default: Optional[Any] = no_arg,
386-
**kwargs: Any,
387-
) -> Optional[T]:
378+
matcher: t.Optional[t.Union[t.Callable[[T], bool], T]] = None,
379+
default: t.Optional[t.Any] = no_arg,
380+
**kwargs: t.Any,
381+
) -> t.Optional[T]:
388382
"""Retrieve one object.
389383
390384
Raises :exc:`MultipleObjectsReturned` if multiple objects found.

0 commit comments

Comments
 (0)