Skip to content

Commit f9ab732

Browse files
authored
mypy fixes (Azure#6641)
* mypy fixes * use six
1 parent b647582 commit f9ab732

File tree

2 files changed

+20
-19
lines changed

2 files changed

+20
-19
lines changed

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

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,47 +25,45 @@
2525
# --------------------------------------------------------------------------
2626
"""Traces network calls using the implementation library from the settings."""
2727

28-
from azure.core.pipeline import PipelineRequest, PipelineResponse
2928
from azure.core.tracing.context import tracing_context
30-
from azure.core.tracing.abstract_span import AbstractSpan
3129
from azure.core.tracing.common import set_span_contexts
3230
from azure.core.pipeline.policies import SansIOHTTPPolicy
3331
from azure.core.settings import settings
3432

35-
try:
36-
from urllib.parse import urlparse
37-
except ImportError:
38-
from urlparse import urlparse
33+
from six.moves import urllib
3934

4035
try:
4136
from typing import TYPE_CHECKING
4237
except ImportError:
4338
TYPE_CHECKING = False
4439

4540
if TYPE_CHECKING:
46-
from typing import Any, Optional
47-
from azure.core.pipeline.transport import HttpRequest, HttpResponse
41+
# the HttpRequest and HttpResponse related type ignores stem from this issue: #5796
42+
from azure.core.pipeline.transport import HttpRequest, HttpResponse # pylint: disable=ungrouped-imports
43+
from azure.core.tracing.abstract_span import AbstractSpan # pylint: disable=ungrouped-imports
44+
from azure.core.pipeline import PipelineRequest, PipelineResponse # pylint: disable=ungrouped-imports
45+
from typing import Any, Optional, Dict, List, Union
4846

4947

5048
class DistributedTracingPolicy(SansIOHTTPPolicy):
5149
"""The policy to create spans for Azure Calls"""
5250

5351
def __init__(self):
5452
# type: () -> None
55-
self.parent_span_dict = {}
53+
self.parent_span_dict = {} # type: Dict[AbstractSpan, List[Union[AbstractSpan, Any]]]
5654
self._request_id = "x-ms-client-request-id"
5755
self._response_id = "x-ms-request-id"
5856

5957
def set_header(self, request, span):
60-
# type: (PipelineRequest[HttpRequest], Any) -> None
58+
# type: (PipelineRequest, Any) -> None
6159
"""
6260
Sets the header information on the span.
6361
"""
6462
headers = span.to_header()
65-
request.http_request.headers.update(headers)
63+
request.http_request.headers.update(headers) # type: ignore
6664

6765
def on_request(self, request):
68-
# type: (PipelineRequest[HttpRequest], Any) -> None
66+
# type: (PipelineRequest) -> None
6967
parent_span = tracing_context.current_span.get()
7068
wrapper_class = settings.tracing_implementation()
7169
original_context = [parent_span, None]
@@ -77,7 +75,7 @@ def on_request(self, request):
7775
if parent_span is None:
7876
return
7977

80-
path = urlparse(request.http_request.url).path
78+
path = urllib.parse.urlparse(request.http_request.url).path # type: ignore
8179
if not path:
8280
path = "/"
8381
child = parent_span.span(name=path)
@@ -99,12 +97,15 @@ def end_span(self, request, response=None):
9997
if response and self._response_id in response.headers:
10098
span.add_attribute(self._response_id, response.headers[self._response_id])
10199
span.finish()
102-
set_span_contexts(*self.parent_span_dict.pop(span, None))
100+
original_context = self.parent_span_dict.pop(span, None)
101+
if original_context:
102+
set_span_contexts(original_context[0], original_context[1])
103103

104104
def on_response(self, request, response):
105-
# type: (PipelineRequest[HttpRequest], PipelineResponse[HttpRequest, HttpResponse], Any) -> None
106-
self.end_span(request.http_request, response=response.http_response)
105+
# type: (PipelineRequest, PipelineResponse) -> None
106+
self.end_span(request.http_request, response=response.http_response) # type: ignore
107107

108108
def on_exception(self, _request): # pylint: disable=unused-argument
109-
# type: (PipelineRequest[HttpRequest], Any) -> bool
110-
self.end_span(_request.http_request)
109+
# type: (PipelineRequest) -> bool
110+
self.end_span(_request.http_request) # type: ignore
111+
return False

sdk/core/azure-core/azure/core/tracing/context.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def __init__(self):
123123
self.current_span = _ThreadLocalContext("current_span", None)
124124

125125
def with_current_context(self, func):
126-
# type: (Callable[[Any], Any]) -> Any
126+
# type: (Callable) -> Any
127127
"""
128128
Passes the current spans to the new context the function will be run in.
129129
:param func: The function that will be run in the new context

0 commit comments

Comments
 (0)