11import json .decoder
22from contextlib import contextmanager
3- from typing import Optional , Type
3+ from typing import Generator , Optional , Type , TypeVar
44
55from requests import Response
66
7+ import hostingde
78from hostingde .exceptions import ApiException , ClientException
89from hostingde .model import Model
910from hostingde .model .filter import FilterElement
1011from hostingde .model .sort import SortConfiguration
1112from hostingde .session import HostingDeSession
1213
14+ T = TypeVar ('T' , bound = 'Model' )
15+
1316
1417class HostingDeCore :
1518 def __init__ (self , parent : 'HostingDeCore' = None ):
@@ -31,7 +34,7 @@ def _post(self, *args, **kwargs):
3134 """
3235 return self .session .post (* args , ** kwargs )
3336
34- def _request (self , url , model : Optional [Model ] = None , ** kwargs ) :
37+ def _request (self , url : str , model : Optional [Model ] = None , ** kwargs : dict ) -> Response :
3538 """
3639 Execute a new request, given an URL and a model. To generate a URL, you can use the _build_url() utility
3740 method.
@@ -56,17 +59,17 @@ def _request(self, url, model: Optional[Model] = None, **kwargs):
5659
5760 return response
5861
59- def _instance (self , instance_type : Type [Model ], data : dict ):
62+ def _instance (self , instance_type : Type [T ], data : dict ) -> T :
6063 """
6164 Reconstructs a model from the passed in result.
6265
6366 :param instance_type: The instance type to be reconstructed.
6467 :param data: The data used to reconstruct the model
6568 :return: The parsed model
6669 """
67- return instance_type .from_json (data )
70+ return instance_type .from_json (data , self ) # type: ignore
6871
69- def _bool (self , response : Response ):
72+ def _bool (self , response : Response ) -> bool :
7073 """
7174 Converts a response to a boolean. True, if the request succeeded, otherwise False. Note that a status of
7275 'pending' also returns False. You may want to use the _async method to wait for the job to complete.
@@ -89,11 +92,11 @@ def _build_uri(self, service: str, method: str) -> str:
8992 def _iter (
9093 self ,
9194 url : str ,
92- instance_class : Type [Model ],
95+ instance_class : Type [T ],
9396 filter : Optional [FilterElement ] = None ,
9497 limit : Optional [int ] = None ,
9598 sort : Optional [SortConfiguration ] = None ,
96- ):
99+ ) -> 'hostingde.HostingDePaginator[T]' :
97100 """
98101 Use the generic filtering and sorting API to paginate over results.
99102
@@ -104,9 +107,8 @@ def _iter(
104107 :param sort: The sorting of the resulting list
105108 :return: The iterator for the resultset
106109 """
107- from hostingde .paginator import HostingDePaginator
108110
109- return HostingDePaginator (self , instance_class , url , filter = filter , limit = limit , sort = sort )
111+ return hostingde . HostingDePaginator (self , instance_class , url , filter = filter , limit = limit , sort = sort )
110112
111113 def login (self , url : str , token : str ) -> None :
112114 """
@@ -118,7 +120,7 @@ def login(self, url: str, token: str) -> None:
118120 self .session .base_uri = url
119121 self .session .token_auth (token )
120122
121- def set_account_context (self , account_id : str ) :
123+ def set_account_context (self , account_id : Optional [ str ]) -> None :
122124 """
123125 Sets the account context for this client.
124126
@@ -128,15 +130,15 @@ def set_account_context(self, account_id: str):
128130 self .session .set_account_context (account_id )
129131
130132 @contextmanager
131- def switch_account_context (self , account_id : str ):
133+ def switch_account_context (self , account_id : str ) -> Generator [ None , None , None ] :
132134 """
133135 Temporarily switch the account context for this client. After the context guard closes, the context is reset to
134136 the account that was used prior to the guard.
135137
136138 :param account_id: The account id to switch to
137139 :return:
138140 """
139- old_id : str = self .session .get_account_context ()
141+ old_id : Optional [ str ] = self .session .get_account_context ()
140142
141143 self .session .set_account_context (account_id )
142144
@@ -145,7 +147,7 @@ def switch_account_context(self, account_id: str):
145147 self .session .set_account_context (old_id )
146148
147149 @staticmethod
148- def new_session ():
150+ def new_session () -> HostingDeSession :
149151 """
150152 Generates a new, unauthenticated session
151153
0 commit comments