22
33import warnings
44from collections .abc import Callable
5- from typing import TYPE_CHECKING , Any , ClassVar
5+ from typing import TYPE_CHECKING , Any , ClassVar , Generic , TypeVar
6+
7+ from .domain import BaseDomain
68
79if TYPE_CHECKING :
810 from .._client import Client , ClientBase
9- from .domain import BaseDomain
11+ from .domain import Meta
12+
13+
14+ T = TypeVar ("T" )
1015
1116
1217class ResourceClientBase :
@@ -23,10 +28,10 @@ def __init__(self, client: Client):
2328
2429 def _iter_pages ( # type: ignore[no-untyped-def]
2530 self ,
26- list_function : Callable ,
31+ list_function : Callable [..., tuple [ list [ T ], Meta ]] ,
2732 * args ,
2833 ** kwargs ,
29- ) -> list :
34+ ) -> list [ T ] :
3035 results = []
3136
3237 page = 1
@@ -46,7 +51,12 @@ def _iter_pages( # type: ignore[no-untyped-def]
4651
4752 return results
4853
49- def _get_first_by (self , list_function : Callable , * args , ** kwargs ): # type: ignore[no-untyped-def]
54+ def _get_first_by ( # type: ignore[no-untyped-def]
55+ self ,
56+ list_function : Callable [..., tuple [list [T ], Meta ]],
57+ * args ,
58+ ** kwargs ,
59+ ) -> T | None :
5060 entities , _ = list_function (* args , ** kwargs )
5161 return entities [0 ] if entities else None
5262
@@ -69,15 +79,18 @@ def __init__(self, client: Client):
6979 super ().__init__ (client )
7080
7181
72- class BoundModelBase :
82+ Domain = TypeVar ("Domain" , bound = BaseDomain )
83+
84+
85+ class BoundModelBase (Generic [Domain ]):
7386 """Bound Model Base"""
7487
75- model : type [BaseDomain ]
88+ model : type [Domain ]
7689
7790 def __init__ (
7891 self ,
7992 client : ResourceClientBase ,
80- data : dict ,
93+ data : dict [ str , Any ] ,
8194 complete : bool = True ,
8295 ):
8396 """
@@ -90,7 +103,7 @@ def __init__(
90103 """
91104 self ._client = client
92105 self .complete = complete
93- self .data_model = self .model .from_dict (data )
106+ self .data_model : Domain = self .model .from_dict (data )
94107
95108 def __getattr__ (self , name : str ): # type: ignore[no-untyped-def]
96109 """Allow magical access to the properties of the model
@@ -103,9 +116,10 @@ def __getattr__(self, name: str): # type: ignore[no-untyped-def]
103116 value = getattr (self .data_model , name )
104117 return value
105118
106- def _get_self (self ) -> BoundModelBase :
119+ def _get_self (self ) -> BoundModelBase [ Domain ] :
107120 assert hasattr (self ._client , "get_by_id" )
108- return self ._client .get_by_id (self .data_model .id )
121+ assert hasattr (self .data_model , "id" )
122+ return self ._client .get_by_id (self .data_model .id ) # type: ignore
109123
110124 def reload (self ) -> None :
111125 """Reloads the model and tries to get all data from the API"""
0 commit comments