28
28
Sequence ,
29
29
TypeVar ,
30
30
Union ,
31
+ get_args ,
31
32
overload ,
32
33
)
33
34
@@ -69,9 +70,6 @@ def resolve_invite(invite: Invite | str) -> str:
69
70
return invite
70
71
71
72
72
- __all__ = ("resolve_invite" ,)
73
-
74
-
75
73
def get_as_snowflake (data : Any , key : str ) -> int | None :
76
74
try :
77
75
value = data [key ]
@@ -111,7 +109,7 @@ def parse_ratelimit_header(request: Any, *, use_clock: bool = False) -> float:
111
109
return (reset - now ).total_seconds ()
112
110
113
111
114
- def string_width (string : str , * , _IS_ASCII = _IS_ASCII ) -> int :
112
+ def string_width (string : str , * , _IS_ASCII : re . Pattern [ str ] = _IS_ASCII ) -> int :
115
113
"""Returns string's width."""
116
114
match = _IS_ASCII .match (string )
117
115
if match :
@@ -138,7 +136,7 @@ def resolve_template(code: Template | str) -> str:
138
136
:class:`str`
139
137
The template code.
140
138
"""
141
- from ..template import Template # noqa: PLC0415 # circular import
139
+ from ..template import Template # noqa: PLC0415
142
140
143
141
if isinstance (code , Template ):
144
142
return code .code
@@ -167,10 +165,6 @@ def parse_time(timestamp: None) -> None: ...
167
165
def parse_time (timestamp : str ) -> datetime .datetime : ...
168
166
169
167
170
- @overload
171
- def parse_time (timestamp : str | None ) -> datetime .datetime | None : ...
172
-
173
-
174
168
def parse_time (timestamp : str | None ) -> datetime .datetime | None :
175
169
"""A helper function to convert an ISO 8601 timestamp to a datetime object.
176
170
@@ -218,7 +212,6 @@ def warn_deprecated(
218
212
stacklevel: :class:`int`
219
213
The stacklevel kwarg passed to :func:`warnings.warn`. Defaults to 3.
220
214
"""
221
- warnings .simplefilter ("always" , DeprecationWarning ) # turn off filter
222
215
message = f"{ name } is deprecated"
223
216
if since :
224
217
message += f" since version { since } "
@@ -231,7 +224,6 @@ def warn_deprecated(
231
224
message += f" See { reference } for more information."
232
225
233
226
warnings .warn (message , stacklevel = stacklevel , category = DeprecationWarning )
234
- warnings .simplefilter ("default" , DeprecationWarning ) # reset filter
235
227
236
228
237
229
def deprecated (
@@ -242,7 +234,7 @@ def deprecated(
242
234
stacklevel : int = 3 ,
243
235
* ,
244
236
use_qualname : bool = True ,
245
- ) -> Callable [[Callable [[ P ] , T ]], Callable [[ P ] , T ]]:
237
+ ) -> Callable [[Callable [P , T ]], Callable [P , T ]]:
246
238
"""A decorator implementation of :func:`warn_deprecated`. This will automatically call :func:`warn_deprecated` when
247
239
the decorated function is called.
248
240
@@ -267,7 +259,7 @@ def deprecated(
267
259
will display as ``login``. Defaults to ``True``.
268
260
"""
269
261
270
- def actual_decorator (func : Callable [[ P ] , T ]) -> Callable [[ P ] , T ]:
262
+ def actual_decorator (func : Callable [P , T ]) -> Callable [P , T ]:
271
263
@functools .wraps (func )
272
264
def decorated (* args : P .args , ** kwargs : P .kwargs ) -> T :
273
265
warn_deprecated (
@@ -289,11 +281,11 @@ def decorated(*args: P.args, **kwargs: P.kwargs) -> T:
289
281
290
282
291
283
def flatten_literal_params (parameters : Iterable [Any ]) -> tuple [Any , ...]:
292
- params = []
284
+ params : list [ Any ] = []
293
285
literal_cls = type (Literal [0 ])
294
286
for p in parameters :
295
287
if isinstance (p , literal_cls ):
296
- params .extend (p . __args__ )
288
+ params .extend (get_args ( p ) )
297
289
else :
298
290
params .append (p )
299
291
return tuple (params )
@@ -311,7 +303,7 @@ def evaluate_annotation(
311
303
cache : dict [str , Any ],
312
304
* ,
313
305
implicit_str : bool = True ,
314
- ):
306
+ ) -> Any :
315
307
if isinstance (tp , ForwardRef ):
316
308
tp = tp .__forward_arg__
317
309
# ForwardRefs always evaluate their internals
@@ -329,8 +321,8 @@ def evaluate_annotation(
329
321
is_literal = False
330
322
args = tp .__args__
331
323
if not hasattr (tp , "__origin__" ):
332
- if PY_310 and tp .__class__ is types .UnionType : # type: ignore
333
- converted = Union [args ] # type: ignore # noqa: UP007
324
+ if PY_310 and tp .__class__ is types .UnionType :
325
+ converted = Union [args ] # noqa: UP007
334
326
return evaluate_annotation (converted , globals , locals , cache )
335
327
336
328
return tp
@@ -381,7 +373,7 @@ def resolve_annotation(
381
373
return evaluate_annotation (annotation , globalns , locals , cache )
382
374
383
375
384
- def delay_task (delay : float , func : Coroutine ):
376
+ def delay_task (delay : float , func : Coroutine [ Any , Any , Any ] ):
385
377
async def inner_call ():
386
378
await asyncio .sleep (delay )
387
379
try :
@@ -408,7 +400,7 @@ async def maybe_awaitable(f: Callable[P, T | Awaitable[T]], *args: P.args, **kwa
408
400
return value
409
401
410
402
411
- async def sane_wait_for (futures : Iterable [Awaitable [T ]], * , timeout : float ) -> set [asyncio .Future [T ]]:
403
+ async def sane_wait_for (futures : Iterable [Awaitable [T ]], * , timeout : float ) -> set [asyncio .Task [T ]]:
412
404
ensured = [asyncio .ensure_future (fut ) for fut in futures ]
413
405
done , pending = await asyncio .wait (ensured , timeout = timeout , return_when = asyncio .ALL_COMPLETED )
414
406
@@ -418,7 +410,7 @@ async def sane_wait_for(futures: Iterable[Awaitable[T]], *, timeout: float) -> s
418
410
return done
419
411
420
412
421
- class SnowflakeList (array .array ):
413
+ class SnowflakeList (array .array [ int ] ):
422
414
"""Internal data storage class to efficiently store a list of snowflakes.
423
415
424
416
This should have the following characteristics:
@@ -437,7 +429,7 @@ class SnowflakeList(array.array):
437
429
def __init__ (self , data : Iterable [int ], * , is_sorted : bool = False ): ...
438
430
439
431
def __new__ (cls , data : Iterable [int ], * , is_sorted : bool = False ):
440
- return array . array . __new__ (cls , "Q" , data if is_sorted else sorted (data )) # type: ignore
432
+ return super (). __new__ (cls , "Q" , data if is_sorted else sorted (data ))
441
433
442
434
def add (self , element : int ) -> None :
443
435
i = bisect_left (self , element )
@@ -452,22 +444,27 @@ def has(self, element: int) -> bool:
452
444
return i != len (self ) and self [i ] == element
453
445
454
446
455
- def copy_doc (original : Callable ) -> Callable [[T ], T ]:
447
+ def copy_doc (original : Callable [..., object ] ) -> Callable [[T ], T ]:
456
448
def decorator (overridden : T ) -> T :
457
449
overridden .__doc__ = original .__doc__
458
- overridden .__signature__ = signature (original ) # type: ignore
450
+ overridden .__signature__ = signature (original ) # type: ignore[reportAttributeAccessIssue]
459
451
return overridden
460
452
461
453
return decorator
462
454
463
455
464
- class SequenceProxy (collections .abc .Sequence , Generic [T_co ]):
456
+ class SequenceProxy (collections .abc .Sequence [ T_co ] , Generic [T_co ]):
465
457
"""Read-only proxy of a Sequence."""
466
458
467
459
def __init__ (self , proxied : Sequence [T_co ]):
468
460
self .__proxied = proxied
469
461
470
- def __getitem__ (self , idx : int ) -> T_co :
462
+ @overload
463
+ def __getitem__ (self , idx : int ) -> T_co : ...
464
+ @overload
465
+ def __getitem__ (self , idx : slice ) -> Sequence [T_co ]: ...
466
+
467
+ def __getitem__ (self , idx : int | slice ) -> T_co | Sequence [T_co ]:
471
468
return self .__proxied [idx ]
472
469
473
470
def __len__ (self ) -> int :
@@ -482,7 +479,7 @@ def __iter__(self) -> Iterator[T_co]:
482
479
def __reversed__ (self ) -> Iterator [T_co ]:
483
480
return reversed (self .__proxied )
484
481
485
- def index (self , value : Any , * args , ** kwargs ) -> int :
482
+ def index (self , value : Any , * args : Any , ** kwargs : Any ) -> int :
486
483
return self .__proxied .index (value , * args , ** kwargs )
487
484
488
485
def count (self , value : Any ) -> int :
@@ -515,10 +512,10 @@ def __get__(self, instance: T | None, owner: type[T]) -> Any:
515
512
516
513
def get_slots (cls : type [Any ]) -> Iterator [str ]:
517
514
for mro in reversed (cls .__mro__ ):
518
- try :
519
- yield from mro .__slots__
520
- except AttributeError :
515
+ slots = getattr (mro , "__slots__" , None )
516
+ if slots is None :
521
517
continue
518
+ yield from slots
522
519
523
520
524
521
def cached_slot_property (
@@ -533,15 +530,15 @@ def decorator(func: Callable[[T], T_co]) -> CachedSlotProperty[T, T_co]:
533
530
try :
534
531
import msgspec
535
532
536
- def to_json (obj : Any ) -> str : # type: ignore
533
+ def to_json (obj : Any ) -> str : # type: ignore[reportUnusedFunction]
537
534
return msgspec .json .encode (obj ).decode ("utf-8" )
538
535
539
- from_json = msgspec .json .decode # type: ignore
536
+ from_json = msgspec .json .decode
540
537
541
538
except ModuleNotFoundError :
542
539
import json
543
540
544
- def to_json (obj : Any ) -> str :
541
+ def to_json (obj : Any ) -> str : # type: ignore[reportUnusedFunction]
545
542
return json .dumps (obj , separators = ("," , ":" ), ensure_ascii = True )
546
543
547
544
from_json = json .loads
0 commit comments