Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get successful MyPy pass for azure-core #5266

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion 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
10 changes: 5 additions & 5 deletions azure-core/azure/core/paging.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@
except ImportError:
from collections import Iterator

from typing import Dict, Any, List, Callable, Optional, TYPE_CHECKING # pylint: disable=unused-import
from typing import Dict, Any, List, Callable, Optional, TypeVar, TYPE_CHECKING # pylint: disable=unused-import

if TYPE_CHECKING:
from .pipeline import HttpResponse # pylint: disable=unused-import
from msrest.serialization import Deserializer, Model # pylint: disable=unused-import
HTTPResponseType = TypeVar("HTTPResponseType")
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 All @@ -54,15 +54,15 @@ class Paged(AsyncPagedMixin, Iterator):
_attribute_map = {} # type: Dict[str, Dict[str, Any]]

def __init__(self, command, deserializer, **kwargs):
# type: (Callable[[str], HttpResponse], Deserializer, Any) -> None
# type: (Callable[[str], HTTPResponseType], Deserializer, Any) -> None
super(Paged, self).__init__(**kwargs) # type: ignore
# Sets next_link, current_page, and _current_page_iter_index.
self.next_link = ""
self.current_page = [] # type: List[Model]
self._current_page_iter_index = 0
self._deserializer = deserializer
self._get_next = command
self._response = None # type: Optional[HttpResponse]
self._response = None # type: Optional[HTTPResponseType]

def __iter__(self):
"""Return 'self'."""
Expand Down
6 changes: 5 additions & 1 deletion azure-core/azure/core/pipeline/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
# --------------------------------------------------------------------------

import abc
from typing import TYPE_CHECKING, Generic, TypeVar, cast, IO, List, Union, Any, Mapping, Dict, Optional, Tuple, Callable, Iterator # pylint: disable=unused-import

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

try:
ABC = abc.ABC
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
12 changes: 6 additions & 6 deletions azure-core/azure/core/pipeline/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
import logging
import os.path
try:
from urlparse import urlparse
from urlparse import urlparse # type: ignore
except ImportError:
from urllib.parse import urlparse
from urllib.parse import urlparse # type: ignore
import xml.etree.ElementTree as ET
from azure.core.pipeline import AbstractContextManager, PipelineRequest, PipelineResponse, PipelineContext
from azure.core.pipeline.policies import HTTPPolicy, SansIOHTTPPolicy
Expand All @@ -52,7 +52,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 @@ -67,7 +67,7 @@ def send(self, request):
class _TransportRunner(HTTPPolicy):

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

Expand All @@ -87,7 +87,7 @@ class Pipeline(AbstractContextManager, Generic[HTTPRequestType, HTTPResponseType
"""

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

Expand All @@ -112,6 +112,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
15 changes: 8 additions & 7 deletions azure-core/azure/core/pipeline/policies/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,23 @@

from typing import TYPE_CHECKING, Generic, TypeVar, cast, IO, List, Union, Any, Mapping, Dict, Optional, Tuple, Callable, Iterator # pylint: disable=unused-import

from azure.core.pipeline import ABC
from azure.core.pipeline import ABC, PipelineRequest, PipelineResponse

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 @@ -67,20 +67,21 @@ class SansIOHTTPPolicy(Generic[HTTPRequestType, HTTPResponseType]):
sync or async implementation or specific HTTP lib
"""


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

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.
"""
pass

def on_exception(self, request, **kwargs):
# type: (PipelineRequest[HTTPRequestType], Any) -> bool
# type: (PipelineRequest, Any) -> bool
"""Is executed if an exception comes back fron the following
policy.

Expand Down Expand Up @@ -109,7 +110,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, PipelineResponse, Exception, Optional[Dict[str, Any]]) -> None
self.http_request = copy.deepcopy(http_request)
self.http_response = http_response
self.error = error
Expand Down
4 changes: 2 additions & 2 deletions azure-core/azure/core/pipeline/policies/base_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ 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]]]
self.next = None # type: Optional[Any]

@abc.abstractmethod
async def send(self, request: PipelineRequest):
async def send(self, request: PipelineRequest) -> PipelineResponse:
"""Mutate the request.

Context content is dependent on the HttpTransport.
Expand Down
2 changes: 1 addition & 1 deletion azure-core/azure/core/pipeline/policies/credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import requests
from requests.models import CONTENT_CHUNK_SIZE

from urllib3 import Retry # Needs requests 2.16 at least to be safe
from urllib3 import Retry # type: ignore # Needs requests 2.16 at least to be safe

from .base import HTTPPolicy

Expand Down
4 changes: 2 additions & 2 deletions azure-core/azure/core/pipeline/policies/redirect.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@
from typing import TYPE_CHECKING, List, Callable, Iterator, Any, Union, Dict, Optional # pylint: disable=unused-import
import warnings
try:
from urlparse import urlparse
from urlparse import urlparse # type: ignore
except ImportError:
from urllib.parse import urlparse
from urllib.parse import urlparse # type: ignore

from azure.core.exceptions import TooManyRedirectsError

Expand Down
2 changes: 1 addition & 1 deletion azure-core/azure/core/pipeline/policies/redirect_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
from azure.core.exceptions import TooManyRedirectsError


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

async def send(self, request):
Expand Down
2 changes: 1 addition & 1 deletion azure-core/azure/core/pipeline/policies/retry_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@



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


async def _sleep_for_retry(self, response, transport):
Expand Down
Loading