Skip to content
2 changes: 1 addition & 1 deletion sdk/core/azure-core/azure/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__path__ = __import__('pkgutil').extend_path(__path__, __name__)
__path__ = __import__('pkgutil').extend_path(__path__, __name__) # type: ignore
32 changes: 32 additions & 0 deletions sdk/core/azure-core/azure/core/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# --------------------------------------------------------------------------
#
# Copyright (c) Microsoft Corporation. All rights reserved.
#
# The MIT License (MIT)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the ""Software""), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
#
# --------------------------------------------------------------------------
from typing import Union, Optional
from azure.core.exceptions import (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need "try" for AsyncHTTPPolicy?

ServiceRequestError,
ServiceResponseError
)

ErrorType = Optional[Union[ServiceRequestError, ServiceResponseError]]
2 changes: 1 addition & 1 deletion sdk/core/azure-core/azure/core/paging.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

if TYPE_CHECKING:
from .pipeline.transport.base import HttpResponse
from msrest.serialization import Deserializer, Model # pylint: disable=unused-import
from msrest.serialization import Deserializer, Model # type: ignore # pylint: disable=unused-import

if sys.version_info >= (3, 5, 2):
# Not executed on old Python, no syntax error
Expand Down
6 changes: 5 additions & 1 deletion sdk/core/azure-core/azure/core/pipeline/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,16 @@
# --------------------------------------------------------------------------

import abc
from typing import (TypeVar, Any, Dict, Optional)

try:
ABC = abc.ABC
except AttributeError: # Python 2.7, abc exists, but not ABC
ABC = abc.ABCMeta('ABC', (object,), {'__slots__': ()}) # type: ignore

HTTPResponseType = TypeVar("HTTPResponseType")
HTTPRequestType = TypeVar("HTTPRequestType")

try:
from contextlib import AbstractContextManager # type: ignore #pylint: disable=unused-import
except ImportError: # Python <= 3.5
Expand Down Expand Up @@ -107,7 +111,7 @@ class PipelineResponse(object):
However, nothing prevents a policy to actually sub-class this class a return it instead of the initial instance.
"""
def __init__(self, http_request, http_response, context):
# type: (HttpRequest[HTTPRequestType], HTTPResponseType, Optional[Dict[str, Any]]) -> None
# type: (HTTPRequestType, HTTPResponseType, Optional[Dict[str, Any]]) -> None
self.http_request = http_request
self.http_response = http_response
self.context = context
Expand Down
14 changes: 8 additions & 6 deletions sdk/core/azure-core/azure/core/pipeline/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@
from azure.core.pipeline.policies import HTTPPolicy, SansIOHTTPPolicy
HTTPResponseType = TypeVar("HTTPResponseType")
HTTPRequestType = TypeVar("HTTPRequestType")
HttpTransportType = TypeVar("HttpTransportType")

_LOGGER = logging.getLogger(__name__)
PoliciesType = List[Union[HTTPPolicy, SansIOHTTPPolicy]]


class _SansIOHTTPPolicyRunner(HTTPPolicy, Generic[HTTPRequestType, HTTPResponseType]):
Expand All @@ -45,7 +47,7 @@ def __init__(self, policy):
self._policy = policy

def send(self, request):
# type: (PipelineRequest[HTTPRequestType], Any) -> PipelineResponse[HTTPRequestType, HTTPResponseType]
# type: (PipelineRequest) -> PipelineResponse
self._policy.on_request(request)
try:
response = self.next.send(request)
Expand All @@ -60,7 +62,7 @@ def send(self, request):
class _TransportRunner(HTTPPolicy):

def __init__(self, sender):
# type: (HttpTransport) -> None
# type: (HttpTransportType) -> None
super(_TransportRunner, self).__init__()
self._sender = sender

Expand All @@ -80,9 +82,9 @@ class Pipeline(AbstractContextManager, Generic[HTTPRequestType, HTTPResponseType
"""

def __init__(self, transport, policies=None):
# type: (HttpTransport, List[Union[HTTPPolicy, SansIOHTTPPolicy]]) -> None
# type: (HttpTransportType, PoliciesType) -> None
self._impl_policies = [] # type: List[HTTPPolicy]
self._transport = transport # type: HTTPPolicy
self._transport = transport # type: ignore

for policy in (policies or []):
if isinstance(policy, SansIOHTTPPolicy):
Expand All @@ -96,7 +98,7 @@ def __init__(self, transport, policies=None):

def __enter__(self):
# type: () -> Pipeline
self._transport.__enter__()
self._transport.__enter__() # type: ignore
return self

def __exit__(self, *exc_details): # pylint: disable=arguments-differ
Expand All @@ -105,6 +107,6 @@ def __exit__(self, *exc_details): # pylint: disable=arguments-differ
def run(self, request, **kwargs):
# type: (HTTPRequestType, Any) -> PipelineResponse
context = PipelineContext(self._transport, **kwargs)
pipeline_request = PipelineRequest(request, context) # type: PipelineRequest[HTTPRequestType]
pipeline_request = PipelineRequest(request, context) # type: PipelineRequest
first_node = self._impl_policies[0] if self._impl_policies else _TransportRunner(self._transport)
return first_node.send(pipeline_request) # type: ignore
9 changes: 5 additions & 4 deletions sdk/core/azure-core/azure/core/pipeline/base_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@
# --------------------------------------------------------------------------
import abc

from typing import Any, List, Union, Generic, TypeVar
from typing import Any, Union, List, Generic, TypeVar

from azure.core.pipeline import PipelineRequest, PipelineResponse, PipelineContext
from azure.core.pipeline.policies import AsyncHTTPPolicy, SansIOHTTPPolicy

AsyncHTTPResponseType = TypeVar("AsyncHTTPResponseType")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AsyncHTTPResponseType is already defined in azure.core.common

HTTPRequestType = TypeVar("HTTPRequestType")

ImplPoliciesType = List[AsyncHTTPPolicy[HTTPRequestType, AsyncHTTPResponseType]] #pylint: disable=unsubscriptable-object
AsyncPoliciesType = List[Union[AsyncHTTPPolicy, SansIOHTTPPolicy]]

try:
from contextlib import AbstractAsyncContextManager # type: ignore
Expand Down Expand Up @@ -88,8 +89,8 @@ class AsyncPipeline(AbstractAsyncContextManager, Generic[HTTPRequestType, AsyncH
of the HTTP sender.
"""

def __init__(self, transport, policies: List[Union[AsyncHTTPPolicy, SansIOHTTPPolicy]] = None) -> None:
self._impl_policies = [] # type: List[AsyncHTTPPolicy[HTTPRequestType, AsyncHTTPResponseType]]
def __init__(self, transport, policies: AsyncPoliciesType = None) -> None:
self._impl_policies = [] # type: ImplPoliciesType
self._transport = transport

for policy in (policies or []):
Expand Down
14 changes: 7 additions & 7 deletions sdk/core/azure-core/azure/core/pipeline/policies/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,23 @@
from typing import (TYPE_CHECKING, Generic, TypeVar, cast, IO, List, Union, Any, Mapping, Dict, Optional, # pylint: disable=unused-import
Tuple, Callable, Iterator)

from azure.core.pipeline import ABC
from azure.core.pipeline import ABC, PipelineRequest, PipelineResponse
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should use PipelineRequestType


HTTPResponseType = TypeVar("HTTPResponseType")
HTTPRequestType = TypeVar("HTTPRequestType")

_LOGGER = logging.getLogger(__name__)


class HTTPPolicy(ABC, Generic[HTTPRequestType, HTTPResponseType]):
class HTTPPolicy(ABC, Generic[HTTPRequestType, HTTPResponseType]): # type: ignore
"""An HTTP policy ABC.
"""
def __init__(self):
self.next = None

@abc.abstractmethod
def send(self, request):
# type: (PipelineRequest[HTTPRequestType]) -> PipelineResponse[HTTPRequestType, HTTPResponseType]
# type: (PipelineRequest) -> PipelineResponse
"""Mutate the request.
Context content is dependent on the HttpTransport.
"""
Expand All @@ -64,18 +64,18 @@ class SansIOHTTPPolicy(Generic[HTTPRequestType, HTTPResponseType]):
"""

def on_request(self, request, **kwargs):
# type: (PipelineRequest[HTTPRequestType], Any) -> None
# type: (PipelineRequest, Any) -> None
"""Is executed before sending the request to next policy.
"""

def on_response(self, request, response, **kwargs):
# type: (PipelineRequest[HTTPRequestType], PipelineResponse[HTTPRequestType, HTTPResponseType], Any) -> None
# type: (PipelineRequest, PipelineResponse, Any) -> None
"""Is executed after the request comes back from the policy.
"""

#pylint: disable=no-self-use
def on_exception(self, _request, **kwargs): #pylint: disable=unused-argument
# type: (PipelineRequest[HTTPRequestType], Any) -> bool
# type: (PipelineRequest, Any) -> bool
"""Is executed if an exception comes back from the following
policy.
Return True if the exception has been handled and should not
Expand All @@ -98,7 +98,7 @@ class RequestHistory(object):
"""

def __init__(self, http_request, http_response=None, error=None, context=None):
# type: (PipelineRequest[HTTPRequestType], Exception, Optional[Dict[str, Any]]) -> None
# type: (PipelineRequest, Optional[PipelineResponse], Exception, Optional[Dict[str, Any]]) -> None
self.http_request = copy.deepcopy(http_request)
self.http_response = http_response
self.error = error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
# --------------------------------------------------------------------------
import abc

from typing import Generic, TypeVar
from typing import Generic, TypeVar, Optional, Any

from azure.core.pipeline import PipelineRequest

Expand Down Expand Up @@ -54,7 +54,7 @@ class AsyncHTTPPolicy(abc.ABC, Generic[HTTPRequestType, AsyncHTTPResponseType]):
"""
def __init__(self) -> None:
# next will be set once in the pipeline
self.next = None # type: Optional[Union[AsyncHTTPPolicy[HTTPRequestType, AsyncHTTPResponseType], AsyncHttpTransport[HTTPRequestType, AsyncHTTPResponseType]]] #pylint: disable=line-too-long
self.next = None # type: Optional[Any]

@abc.abstractmethod
async def send(self, request: PipelineRequest):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@ class BearerTokenCredentialPolicy(_BearerTokenCredentialPolicyBase, HTTPPolicy):
def send(self, request):
# type: (PipelineRequest) -> PipelineResponse
token = self._credential.get_token(self._scopes)
self._update_headers(request.http_request.headers, token)
self._update_headers(request.http_request.headers, token) # type: ignore
return self.next.send(request)
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ class AsyncBearerTokenCredentialPolicy(_BearerTokenCredentialPolicyBase, AsyncHT
"""Adds a bearer token Authorization header to requests."""

async def send(self, request: PipelineRequest) -> PipelineResponse:
token = await self._credential.get_token(self._scopes)
self._update_headers(request.http_request.headers, token)
return await self.next.send(request)
token = await self._credential.get_token(self._scopes) # type: ignore
self._update_headers(request.http_request.headers, token) # type: ignore
return await self.next.send(request) # type: ignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"""
This module is the requests implementation of Pipeline ABC
"""
from azure.core.pipeline import PipelineRequest, PipelineResponse
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PipelineRequestType?

from .base import SansIOHTTPPolicy

class CustomHookPolicy(SansIOHTTPPolicy):
Expand All @@ -35,12 +36,12 @@ class CustomHookPolicy(SansIOHTTPPolicy):
def __init__(self, **kwargs): # pylint: disable=unused-argument
self._callback = None

def on_request(self, request): # pylint: disable=arguments-differ
def on_request(self, request): # type: ignore # pylint: disable=arguments-differ
# type: (PipelineRequest) -> None
self._callback = request.context.options.pop('raw_response_hook', None)
self._callback = request.context.options.pop('raw_response_hook', None) # type: ignore

def on_response(self, request, response): # pylint: disable=arguments-differ
def on_response(self, request, response): # type: ignore # pylint: disable=arguments-differ
# type: (PipelineRequest, PipelineResponse) -> None
if self._callback:
self._callback(response)
request.context.options.update({'raw_response_hook': self._callback})
request.context.options.update({'raw_response_hook': self._callback}) # type: ignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import logging
from typing import TYPE_CHECKING, List, Callable, Iterator, Any, Union, Dict, Optional # pylint: disable=unused-import
try:
from urlparse import urlparse
from urlparse import urlparse # type: ignore
except ImportError:
from urllib.parse import urlparse

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from .redirect import RedirectPolicy


class AsyncRedirectPolicy(RedirectPolicy, AsyncHTTPPolicy):
class AsyncRedirectPolicy(RedirectPolicy, AsyncHTTPPolicy): # type: ignore
"""An async redirect policy."""

async def send(self, request):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@



class AsyncRetryPolicy(RetryPolicy, AsyncHTTPPolicy):
class AsyncRetryPolicy(RetryPolicy, AsyncHTTPPolicy): # type: ignore


async def _sleep_for_retry(self, response, transport):
Expand Down Expand Up @@ -92,4 +92,3 @@ async def send(self, request):

self.update_context(response.context, retry_settings)
return response

Loading