Skip to content

Commit 63476fb

Browse files
Azure-Core type complete (Azure#31502)
* Pipeline complete * Missing type * Mypy fixes part1 * Mypy fixes part 2 * mypy fixes part 3 * mypy fixes part 4 * mypy fixes part 5 * mypy fixes part 6 * Pyright fixes * CI fixes * pyright fixes again * mypy clean, no warnings * pylint * next-mypy * Update sdk/core/azure-core/azure/core/pipeline/policies/_redirect.py Co-authored-by: Krista Pratico <[email protected]> * Update sdk/core/azure-core/azure/core/pipeline/policies/_retry.py Co-authored-by: Krista Pratico <[email protected]> * Move pylint ignore * Correct aexit * Black * Feedback from Anna * Kashif's feedback * CI feedback --------- Co-authored-by: Krista Pratico <[email protected]>
1 parent 02c46b5 commit 63476fb

31 files changed

+455
-213
lines changed

sdk/core/azure-core/azure/core/_pipeline_client.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,10 @@
2323
# IN THE SOFTWARE.
2424
#
2525
# --------------------------------------------------------------------------
26-
26+
from __future__ import annotations
2727
import logging
2828
from collections.abc import Iterable
29-
from typing import (
30-
TypeVar,
31-
Generic,
32-
Optional,
33-
)
29+
from typing import TypeVar, Generic, Optional, Any
3430
from .configuration import Configuration
3531
from .pipeline import Pipeline
3632
from .pipeline.transport._base import PipelineClientBase
@@ -82,34 +78,34 @@ def __init__(
8278
base_url: str,
8379
*,
8480
pipeline: Optional[Pipeline[HTTPRequestType, HTTPResponseType]] = None,
85-
config: Optional[Configuration] = None,
86-
**kwargs
81+
config: Optional[Configuration[HTTPRequestType, HTTPResponseType]] = None,
82+
**kwargs: Any,
8783
):
8884
super(PipelineClient, self).__init__(base_url)
89-
self._config: Configuration = config or Configuration(**kwargs)
85+
self._config: Configuration[HTTPRequestType, HTTPResponseType] = config or Configuration(**kwargs)
9086
self._base_url = base_url
9187

9288
self._pipeline = pipeline or self._build_pipeline(self._config, **kwargs)
9389

94-
def __enter__(self):
90+
def __enter__(self) -> PipelineClient[HTTPRequestType, HTTPResponseType]:
9591
self._pipeline.__enter__()
9692
return self
9793

98-
def __exit__(self, *exc_details):
94+
def __exit__(self, *exc_details: Any) -> None:
9995
self._pipeline.__exit__(*exc_details)
10096

101-
def close(self):
97+
def close(self) -> None:
10298
self.__exit__()
10399

104100
def _build_pipeline(
105101
self,
106-
config: Configuration,
102+
config: Configuration[HTTPRequestType, HTTPResponseType],
107103
*,
108104
transport: Optional[HttpTransport[HTTPRequestType, HTTPResponseType]] = None,
109105
policies=None,
110106
per_call_policies=None,
111107
per_retry_policies=None,
112-
**kwargs
108+
**kwargs,
113109
) -> Pipeline[HTTPRequestType, HTTPResponseType]:
114110
per_call_policies = per_call_policies or []
115111
per_retry_policies = per_retry_policies or []
@@ -182,7 +178,7 @@ def _build_pipeline(
182178

183179
return Pipeline(transport, policies)
184180

185-
def send_request(self, request: HTTPRequestType, **kwargs) -> HTTPResponseType:
181+
def send_request(self, request: HTTPRequestType, **kwargs: Any) -> HTTPResponseType:
186182
"""Method that runs the network request through the client's chained policies.
187183
188184
>>> from azure.core.rest import HttpRequest

sdk/core/azure-core/azure/core/_pipeline_client_async.py

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
# IN THE SOFTWARE.
2424
#
2525
# --------------------------------------------------------------------------
26-
26+
from __future__ import annotations
2727
import logging
2828
import collections.abc
2929
from typing import (
@@ -34,8 +34,10 @@
3434
Generator,
3535
Generic,
3636
Optional,
37+
Type,
3738
cast,
3839
)
40+
from types import TracebackType
3941
from .configuration import Configuration
4042
from .pipeline import AsyncPipeline
4143
from .pipeline.transport._base import PipelineClientBase
@@ -108,8 +110,13 @@ async def __aenter__(self) -> AsyncHTTPResponseType:
108110
self._response = await self
109111
return self._response
110112

111-
async def __aexit__(self, *args) -> None:
112-
await self._response.__aexit__(*args)
113+
async def __aexit__(
114+
self,
115+
exc_type: Optional[Type[BaseException]] = None,
116+
exc_value: Optional[BaseException] = None,
117+
traceback: Optional[TracebackType] = None,
118+
) -> None:
119+
await self._response.__aexit__(exc_type, exc_value, traceback)
113120

114121

115122
class AsyncPipelineClient(
@@ -150,26 +157,37 @@ def __init__(
150157
base_url: str,
151158
*,
152159
pipeline: Optional[AsyncPipeline[HTTPRequestType, AsyncHTTPResponseType]] = None,
153-
config: Optional[Configuration] = None,
154-
**kwargs
160+
config: Optional[Configuration[HTTPRequestType, AsyncHTTPResponseType]] = None,
161+
**kwargs: Any,
155162
):
156163
super(AsyncPipelineClient, self).__init__(base_url)
157-
self._config: Configuration = config or Configuration(**kwargs)
164+
self._config: Configuration[HTTPRequestType, AsyncHTTPResponseType] = config or Configuration(**kwargs)
158165
self._base_url = base_url
159166
self._pipeline = pipeline or self._build_pipeline(self._config, **kwargs)
160167

161-
async def __aenter__(self):
168+
async def __aenter__(self) -> AsyncPipelineClient[HTTPRequestType, AsyncHTTPResponseType]:
162169
await self._pipeline.__aenter__()
163170
return self
164171

165-
async def __aexit__(self, *args):
166-
await self.close()
172+
async def __aexit__(
173+
self,
174+
exc_type: Optional[Type[BaseException]] = None,
175+
exc_value: Optional[BaseException] = None,
176+
traceback: Optional[TracebackType] = None,
177+
) -> None:
178+
await self._pipeline.__aexit__(exc_type, exc_value, traceback)
167179

168-
async def close(self):
169-
await self._pipeline.__aexit__()
180+
async def close(self) -> None:
181+
await self.__aexit__()
170182

171183
def _build_pipeline(
172-
self, config: Configuration, *, policies=None, per_call_policies=None, per_retry_policies=None, **kwargs
184+
self,
185+
config: Configuration[HTTPRequestType, AsyncHTTPResponseType],
186+
*,
187+
policies=None,
188+
per_call_policies=None,
189+
per_retry_policies=None,
190+
**kwargs,
173191
) -> AsyncPipeline[HTTPRequestType, AsyncHTTPResponseType]:
174192
transport = kwargs.get("transport")
175193
per_call_policies = per_call_policies or []

sdk/core/azure-core/azure/core/credentials.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ def get_token(
3939
:rtype: AccessToken
4040
:return: An AccessToken instance containing the token string and its expiration time in Unix time.
4141
"""
42+
...
4243

4344

4445
AzureNamedKey = namedtuple("AzureNamedKey", ["name", "key"])

sdk/core/azure-core/azure/core/pipeline/__init__.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,16 @@
2424
#
2525
# --------------------------------------------------------------------------
2626

27-
from typing import TypeVar, Generic, Dict, Any, Tuple, List, Optional, overload
27+
from typing import TypeVar, Generic, Dict, Any, Tuple, List, Optional, overload, TYPE_CHECKING, Union
2828

2929
HTTPResponseType = TypeVar("HTTPResponseType", covariant=True) # pylint: disable=typevar-name-incorrect-variance
3030
HTTPRequestType = TypeVar("HTTPRequestType", covariant=True) # pylint: disable=typevar-name-incorrect-variance
3131

32+
if TYPE_CHECKING:
33+
from .transport import HttpTransport, AsyncHttpTransport
34+
35+
TransportType = Union[HttpTransport[Any, Any], AsyncHttpTransport[Any, Any]]
36+
3237

3338
class PipelineContext(Dict[str, Any]):
3439
"""A context object carried by the pipeline request and response containers.
@@ -45,8 +50,10 @@ class PipelineContext(Dict[str, Any]):
4550

4651
_PICKLE_CONTEXT = {"deserialized_data"}
4752

48-
def __init__(self, transport: Any, **kwargs: Any) -> None: # pylint: disable=super-init-not-called
49-
self.transport: Optional[Any] = transport
53+
def __init__(
54+
self, transport: Optional["TransportType"], **kwargs: Any
55+
) -> None: # pylint: disable=super-init-not-called
56+
self.transport: Optional["TransportType"] = transport
5057
self.options = kwargs
5158
self._protected = ["transport", "options"]
5259

sdk/core/azure-core/azure/core/pipeline/_base.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@
2323
# IN THE SOFTWARE.
2424
#
2525
# --------------------------------------------------------------------------
26-
26+
from __future__ import annotations
2727
import logging
28-
from typing import Generic, TypeVar, Union, Any, List, Dict, Optional, Iterable
29-
from contextlib import AbstractContextManager
28+
from typing import Generic, TypeVar, Union, Any, List, Dict, Optional, Iterable, ContextManager
3029
from azure.core.pipeline import (
3130
PipelineRequest,
3231
PipelineResponse,
@@ -39,9 +38,7 @@
3938
HTTPResponseType = TypeVar("HTTPResponseType")
4039
HTTPRequestType = TypeVar("HTTPRequestType")
4140

42-
4341
_LOGGER = logging.getLogger(__name__)
44-
PoliciesType = Iterable[Union[HTTPPolicy, SansIOHTTPPolicy]]
4542

4643

4744
def cleanup_kwargs_for_transport(kwargs: Dict[str, str]) -> None:
@@ -124,7 +121,7 @@ def send(self, request: PipelineRequest[HTTPRequestType]) -> PipelineResponse[HT
124121
)
125122

126123

127-
class Pipeline(AbstractContextManager, Generic[HTTPRequestType, HTTPResponseType]):
124+
class Pipeline(ContextManager["Pipeline"], Generic[HTTPRequestType, HTTPResponseType]):
128125
"""A pipeline implementation.
129126
130127
This is implemented as a context manager, that will activate the context
@@ -147,7 +144,13 @@ class Pipeline(AbstractContextManager, Generic[HTTPRequestType, HTTPResponseType
147144
def __init__(
148145
self,
149146
transport: HttpTransport[HTTPRequestType, HTTPResponseType],
150-
policies: Optional[PoliciesType] = None,
147+
policies: Optional[
148+
Iterable[
149+
Union[
150+
HTTPPolicy[HTTPRequestType, HTTPResponseType], SansIOHTTPPolicy[HTTPRequestType, HTTPResponseType]
151+
]
152+
]
153+
] = None,
151154
) -> None:
152155
self._impl_policies: List[HTTPPolicy[HTTPRequestType, HTTPResponseType]] = []
153156
self._transport = transport
@@ -162,11 +165,11 @@ def __init__(
162165
if self._impl_policies:
163166
self._impl_policies[-1].next = _TransportRunner(self._transport)
164167

165-
def __enter__(self) -> "Pipeline":
168+
def __enter__(self) -> Pipeline[HTTPRequestType, HTTPResponseType]:
166169
self._transport.__enter__()
167170
return self
168171

169-
def __exit__(self, *exc_details): # pylint: disable=arguments-differ
172+
def __exit__(self, *exc_details: Any) -> None: # pylint: disable=arguments-differ
170173
self._transport.__exit__(*exc_details)
171174

172175
@staticmethod

sdk/core/azure-core/azure/core/pipeline/_base_async.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323
# IN THE SOFTWARE.
2424
#
2525
# --------------------------------------------------------------------------
26-
from typing import Any, Union, Generic, TypeVar, List, Dict, Optional, Iterable
27-
from contextlib import AbstractAsyncContextManager
26+
from __future__ import annotations
27+
from types import TracebackType
28+
from typing import Any, Union, Generic, TypeVar, List, Dict, Optional, Iterable, Type
29+
from typing_extensions import AsyncContextManager
2830

2931
from azure.core.pipeline import PipelineRequest, PipelineResponse, PipelineContext
3032
from azure.core.pipeline.policies import AsyncHTTPPolicy, SansIOHTTPPolicy
@@ -34,7 +36,6 @@
3436

3537
AsyncHTTPResponseType = TypeVar("AsyncHTTPResponseType")
3638
HTTPRequestType = TypeVar("HTTPRequestType")
37-
AsyncPoliciesType = Iterable[Union[AsyncHTTPPolicy, SansIOHTTPPolicy]]
3839

3940

4041
class _SansIOAsyncHTTPPolicyRunner(
@@ -107,7 +108,7 @@ async def send(
107108
)
108109

109110

110-
class AsyncPipeline(AbstractAsyncContextManager, Generic[HTTPRequestType, AsyncHTTPResponseType]):
111+
class AsyncPipeline(AsyncContextManager["AsyncPipeline"], Generic[HTTPRequestType, AsyncHTTPResponseType]):
111112
"""Async pipeline implementation.
112113
113114
This is implemented as a context manager, that will activate the context
@@ -130,7 +131,14 @@ class AsyncPipeline(AbstractAsyncContextManager, Generic[HTTPRequestType, AsyncH
130131
def __init__(
131132
self,
132133
transport: AsyncHttpTransport[HTTPRequestType, AsyncHTTPResponseType],
133-
policies: Optional[AsyncPoliciesType] = None,
134+
policies: Optional[
135+
Iterable[
136+
Union[
137+
AsyncHTTPPolicy[HTTPRequestType, AsyncHTTPResponseType],
138+
SansIOHTTPPolicy[HTTPRequestType, AsyncHTTPResponseType],
139+
]
140+
]
141+
] = None,
134142
) -> None:
135143
self._impl_policies: List[AsyncHTTPPolicy[HTTPRequestType, AsyncHTTPResponseType]] = []
136144
self._transport = transport
@@ -145,12 +153,17 @@ def __init__(
145153
if self._impl_policies:
146154
self._impl_policies[-1].next = _AsyncTransportRunner(self._transport)
147155

148-
async def __aenter__(self) -> "AsyncPipeline":
156+
async def __aenter__(self) -> AsyncPipeline[HTTPRequestType, AsyncHTTPResponseType]:
149157
await self._transport.__aenter__()
150158
return self
151159

152-
async def __aexit__(self, *exc_details): # pylint: disable=arguments-differ
153-
await self._transport.__aexit__(*exc_details)
160+
async def __aexit__(
161+
self,
162+
exc_type: Optional[Type[BaseException]] = None,
163+
exc_value: Optional[BaseException] = None,
164+
traceback: Optional[TracebackType] = None,
165+
) -> None:
166+
await self._transport.__aexit__(exc_type, exc_value, traceback)
154167

155168
async def _prepare_multipart_mixed_request(self, request: HTTPRequestType) -> None:
156169
"""Will execute the multipart policies.

sdk/core/azure-core/azure/core/pipeline/policies/_authentication.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def on_request(self, request: PipelineRequest[HTTPRequestType]) -> None:
9999
self._token = self._credential.get_token(*self._scopes)
100100
self._update_headers(request.http_request.headers, self._token.token)
101101

102-
def authorize_request(self, request: PipelineRequest[HTTPRequestType], *scopes: str, **kwargs) -> None:
102+
def authorize_request(self, request: PipelineRequest[HTTPRequestType], *scopes: str, **kwargs: Any) -> None:
103103
"""Acquire a token from the credential and authorize the request with it.
104104
105105
Keyword arguments are passed to the credential's get_token method. The token will be cached and used to
@@ -195,13 +195,13 @@ class AzureKeyCredentialPolicy(SansIOHTTPPolicy[HTTPRequestType, HTTPResponseTyp
195195
:raises: ValueError or TypeError
196196
"""
197197

198-
def __init__(
198+
def __init__( # pylint: disable=unused-argument
199199
self,
200200
credential: "AzureKeyCredential",
201201
name: str,
202202
*,
203203
prefix: Optional[str] = None,
204-
**kwargs, # pylint: disable=unused-argument
204+
**kwargs: Any,
205205
) -> None:
206206
super().__init__()
207207
if not hasattr(credential, "key"):
@@ -226,7 +226,7 @@ class AzureSasCredentialPolicy(SansIOHTTPPolicy[HTTPRequestType, HTTPResponseTyp
226226
:raises: ValueError or TypeError
227227
"""
228228

229-
def __init__(self, credential: "AzureSasCredential", **kwargs) -> None: # pylint: disable=unused-argument
229+
def __init__(self, credential: "AzureSasCredential", **kwargs: Any) -> None: # pylint: disable=unused-argument
230230
super(AzureSasCredentialPolicy, self).__init__()
231231
if not credential:
232232
raise ValueError("credential can not be None")

sdk/core/azure-core/azure/core/pipeline/policies/_custom_hook.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
# IN THE SOFTWARE.
2424
#
2525
# --------------------------------------------------------------------------
26-
from typing import TypeVar
26+
from typing import TypeVar, Any
2727
from azure.core.pipeline import PipelineRequest, PipelineResponse
2828
from azure.core.pipeline.transport import HttpResponse as LegacyHttpResponse, HttpRequest as LegacyHttpRequest
2929
from azure.core.rest import HttpResponse, HttpRequest
@@ -41,7 +41,7 @@ class CustomHookPolicy(SansIOHTTPPolicy[HTTPRequestType, HTTPResponseType]):
4141
:keyword callback raw_response_hook: Callback function. Will be invoked on response.
4242
"""
4343

44-
def __init__(self, **kwargs): # pylint: disable=unused-argument,super-init-not-called
44+
def __init__(self, **kwargs: Any): # pylint: disable=unused-argument,super-init-not-called
4545
self._request_callback = kwargs.get("raw_request_hook")
4646
self._response_callback = kwargs.get("raw_response_hook")
4747

0 commit comments

Comments
 (0)