Skip to content

Commit e74fe67

Browse files
authored
fix: properly typehint Storage.get/pop (#910)
* fix: properly typehint Storage.get * fix: fix Storage.pop's typehints
1 parent 668b7c0 commit e74fe67

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

interactions/api/cache.py

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
from collections import defaultdict
2-
from typing import TYPE_CHECKING, Dict, Generic, List, Optional, Tuple, Type, TypeVar, Union
2+
from typing import (
3+
TYPE_CHECKING,
4+
Dict,
5+
Generic,
6+
List,
7+
Optional,
8+
Tuple,
9+
Type,
10+
TypeVar,
11+
Union,
12+
overload,
13+
)
314

415
if TYPE_CHECKING:
516
from .models import Snowflake
@@ -41,7 +52,15 @@ def add(self, item: _T, id: Optional["Key"] = None) -> None:
4152
"""
4253
self.values[id or item.id] = item
4354

44-
def get(self, id: "Key", default: Optional[_P] = None) -> Union[_T, _P]:
55+
@overload
56+
def get(self, id: "Key") -> Optional[_T]:
57+
...
58+
59+
@overload
60+
def get(self, id: "Key", default: _P) -> Union[_T, _P]:
61+
...
62+
63+
def get(self, id: "Key", default: Optional[_P] = None) -> Union[_T, _P, None]:
4564
"""
4665
Gets an item from the storage.
4766
@@ -63,7 +82,15 @@ def update(self, data: Dict["Key", _T]):
6382
"""
6483
self.values.update(data)
6584

66-
def pop(self, key: "Key", default: Optional[_P] = None) -> Union[_T, _P]:
85+
@overload
86+
def pop(self, key: "Key") -> Optional[_T]:
87+
...
88+
89+
@overload
90+
def pop(self, key: "Key", default: _P) -> Union[_T, _P]:
91+
...
92+
93+
def pop(self, key: "Key", default: Optional[_P] = None) -> Union[_T, _P, None]:
6794
try:
6895
return self.values.pop(key)
6996
except KeyError:

0 commit comments

Comments
 (0)