Skip to content

Commit d39ba6f

Browse files
committed
🐛 Use mypy suggested method for array.array genericness
Signed-off-by: Paillat-dev <paillat@pycord.dev>
1 parent 5391fa0 commit d39ba6f

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

discord/utils/private.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,20 @@ async def sane_wait_for(futures: Iterable[Awaitable[T]], *, timeout: float) -> s
410410
return done
411411

412412

413-
class SnowflakeList(array.array):
413+
# array.array is generic only since Python 3.12
414+
# ref: https://docs.python.org/3/whatsnew/3.12.html#array
415+
# We use the method suggested by mypy
416+
# ref: https://mypy.readthedocs.io/en/stable/runtime_troubles.html#using-classes-that-are-generic-in-stubs-but-not-at-runtime
417+
418+
if TYPE_CHECKING:
419+
SnowflakeListBase = array.array[int]
420+
else:
421+
if sys.version_info >= (3, 12):
422+
SnowflakeListBase = array.array[int]
423+
else:
424+
SnowflakeListBase = array.array
425+
426+
class SnowflakeList(SnowflakeListBase):
414427
"""Internal data storage class to efficiently store a list of snowflakes.
415428
416429
This should have the following characteristics:
@@ -424,12 +437,6 @@ class SnowflakeList(array.array):
424437

425438
__slots__ = ()
426439

427-
if TYPE_CHECKING:
428-
429-
def __init__(self, data: Iterable[int], *, is_sorted: bool = False): ...
430-
def __iter__(self) -> Iterator[int]: ...
431-
def __getitem__(self, i: int) -> int: ...
432-
433440
def __new__(cls, data: Iterable[int], *, is_sorted: bool = False):
434441
return super().__new__(cls, "Q", data if is_sorted else sorted(data))
435442

@@ -445,6 +452,7 @@ def has(self, element: int) -> bool:
445452
i = bisect_left(self, element)
446453
return i != len(self) and self[i] == element
447454

455+
ee = SnowflakeList()
448456

449457
def copy_doc(original: Callable[..., object]) -> Callable[[T], T]:
450458
def decorator(overridden: T) -> T:

0 commit comments

Comments
 (0)