Skip to content

Commit 8685103

Browse files
lmazuelkashifkhan
andauthored
Azure-Core: Fix all typings that are not pipeline related (#31494)
* Fix all typing that are not pipeline related * Don't need those quotes * Update sdk/core/azure-core/azure/core/tracing/_abstract_span.py Co-authored-by: Kashif Khan <[email protected]> * Quotes again * Clean span file a bit * Simplify AsyncCredentials --------- Co-authored-by: Kashif Khan <[email protected]>
1 parent aa50310 commit 8685103

File tree

8 files changed

+56
-69
lines changed

8 files changed

+56
-69
lines changed

sdk/core/azure-core/azure/core/credentials_async.py

+3-8
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
# Copyright (c) Microsoft Corporation.
33
# Licensed under the MIT License.
44
# ------------------------------------
5-
from typing import Any, Optional
5+
from __future__ import annotations
6+
from typing import Any, Optional, AsyncContextManager
67
from typing_extensions import Protocol, runtime_checkable
78
from .credentials import AccessToken as _AccessToken
89

910

1011
@runtime_checkable
11-
class AsyncTokenCredential(Protocol):
12+
class AsyncTokenCredential(Protocol, AsyncContextManager["AsyncTokenCredential"]):
1213
"""Protocol for classes able to provide OAuth tokens."""
1314

1415
async def get_token(
@@ -30,9 +31,3 @@ async def get_token(
3031

3132
async def close(self) -> None:
3233
pass
33-
34-
async def __aenter__(self):
35-
pass
36-
37-
async def __aexit__(self, exc_type, exc_value, traceback) -> None:
38-
pass

sdk/core/azure-core/azure/core/messaging.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# Licensed under the MIT License. See License.txt in the project root for
55
# license information.
66
# --------------------------------------------------------------------------
7+
from __future__ import annotations
78
import uuid
89
from base64 import b64decode
910
from datetime import datetime
@@ -139,7 +140,7 @@ def __repr__(self) -> str:
139140
)[:1024]
140141

141142
@classmethod
142-
def from_dict(cls, event: Dict[str, Any]) -> "CloudEvent":
143+
def from_dict(cls, event: Dict[str, Any]) -> CloudEvent[DataType]:
143144
"""Returns the deserialized CloudEvent object when a dict is provided.
144145
145146
:param event: The dict representation of the event which needs to be deserialized.
@@ -214,7 +215,7 @@ def from_dict(cls, event: Dict[str, Any]) -> "CloudEvent":
214215
return event_obj
215216

216217
@classmethod
217-
def from_json(cls, event: Any) -> "CloudEvent":
218+
def from_json(cls, event: Any) -> CloudEvent[DataType]:
218219
"""
219220
Returns the deserialized CloudEvent object when a json payload is provided.
220221
:param event: The json string that should be converted into a CloudEvent. This can also be

sdk/core/azure-core/azure/core/serialization.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# --------------------------------------------------------------------------
77
import base64
88
from json import JSONEncoder
9-
from typing import Union, cast
9+
from typing import Union, cast, Any
1010
from datetime import datetime, date, time, timedelta
1111
from datetime import timezone
1212

@@ -18,7 +18,7 @@
1818
class _Null:
1919
"""To create a Falsy object"""
2020

21-
def __bool__(self):
21+
def __bool__(self) -> bool:
2222
return False
2323

2424

@@ -115,7 +115,7 @@ def _datetime_as_isostr(dt: Union[datetime, date, time, timedelta]) -> str:
115115
class AzureJSONEncoder(JSONEncoder):
116116
"""A JSON encoder that's capable of serializing datetime objects and bytes."""
117117

118-
def default(self, o): # pylint: disable=too-many-return-statements
118+
def default(self, o: Any) -> Any: # pylint: disable=too-many-return-statements
119119
if isinstance(o, (bytes, bytearray)):
120120
return base64.b64encode(o).decode()
121121
try:

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

+41-52
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,13 @@
33
# Licensed under the MIT License.
44
# ------------------------------------
55
"""Protocol that defines what functions wrappers of tracing libraries should implement."""
6+
from __future__ import annotations
67
from enum import Enum
78
from urllib.parse import urlparse
89

9-
from typing import (
10-
TYPE_CHECKING,
11-
Any,
12-
Sequence,
13-
Optional,
14-
Union,
15-
Callable,
16-
ContextManager,
17-
Dict,
18-
)
10+
from typing import Any, Sequence, Optional, Union, Callable, Dict, Type
11+
from types import TracebackType
12+
from typing_extensions import Protocol, ContextManager
1913
from azure.core.pipeline.transport import HttpRequest, HttpResponse, AsyncHttpResponse
2014
from azure.core.rest import (
2115
HttpResponse as RestHttpResponse,
@@ -26,23 +20,17 @@
2620
HttpResponseType = Union[HttpResponse, AsyncHttpResponse, RestHttpResponse, AsyncRestHttpResponse]
2721
HttpRequestType = Union[HttpRequest, RestHttpRequest]
2822

29-
if TYPE_CHECKING:
30-
AttributeValue = Union[
31-
str,
32-
bool,
33-
int,
34-
float,
35-
Sequence[str],
36-
Sequence[bool],
37-
Sequence[int],
38-
Sequence[float],
39-
]
40-
Attributes = Optional[Dict[str, AttributeValue]]
41-
42-
try:
43-
from typing_extensions import Protocol
44-
except ImportError:
45-
Protocol = object # type: ignore
23+
AttributeValue = Union[
24+
str,
25+
bool,
26+
int,
27+
float,
28+
Sequence[str],
29+
Sequence[bool],
30+
Sequence[int],
31+
Sequence[float],
32+
]
33+
Attributes = Dict[str, AttributeValue]
4634

4735

4836
class SpanKind(Enum):
@@ -67,11 +55,11 @@ class AbstractSpan(Protocol):
6755
"""
6856

6957
def __init__( # pylint: disable=super-init-not-called
70-
self, span: Optional[Any] = None, name: Optional[str] = None, **kwargs
58+
self, span: Optional[Any] = None, name: Optional[str] = None, **kwargs: Any
7159
) -> None:
7260
pass
7361

74-
def span(self, name: str = "child_span", **kwargs) -> "AbstractSpan":
62+
def span(self, name: str = "child_span", **kwargs: Any) -> AbstractSpan:
7563
"""
7664
Create a child span for the current span and append it to the child spans list.
7765
The child span must be wrapped by an implementation of AbstractSpan
@@ -98,10 +86,15 @@ def kind(self, value: SpanKind) -> None:
9886
:type value: SpanKind
9987
"""
10088

101-
def __enter__(self):
89+
def __enter__(self) -> AbstractSpan:
10290
"""Start a span."""
10391

104-
def __exit__(self, exception_type, exception_value, traceback):
92+
def __exit__(
93+
self,
94+
exception_type: Optional[Type[BaseException]],
95+
exception_value: Optional[BaseException],
96+
traceback: TracebackType,
97+
) -> None:
10598
"""Finish a span.
10699
107100
:param exception_type: The type of the exception
@@ -159,7 +152,7 @@ def span_instance(self) -> Any:
159152
"""
160153

161154
@classmethod
162-
def link(cls, traceparent: str, attributes: Optional["Attributes"] = None) -> None:
155+
def link(cls, traceparent: str, attributes: Optional[Attributes] = None) -> None:
163156
"""
164157
Given a traceparent, extracts the context and links the context to the current tracer.
165158
@@ -170,7 +163,7 @@ def link(cls, traceparent: str, attributes: Optional["Attributes"] = None) -> No
170163
"""
171164

172165
@classmethod
173-
def link_from_headers(cls, headers: Dict[str, str], attributes: Optional["Attributes"] = None) -> None:
166+
def link_from_headers(cls, headers: Dict[str, str], attributes: Optional[Attributes] = None) -> None:
174167
"""
175168
Given a dictionary, extracts the context and links the context to the current tracer.
176169
@@ -215,7 +208,7 @@ def set_current_tracer(cls, tracer: Any) -> None:
215208
"""
216209

217210
@classmethod
218-
def change_context(cls, span: "AbstractSpan") -> ContextManager:
211+
def change_context(cls, span: AbstractSpan) -> ContextManager[AbstractSpan]:
219212
"""Change the context for the life of this context manager.
220213
221214
:param span: The span to run in the new context
@@ -235,14 +228,7 @@ def with_current_context(cls, func: Callable) -> Callable:
235228
"""
236229

237230

238-
# https://github.com/python/mypy/issues/5837
239-
if TYPE_CHECKING:
240-
_MIXIN_BASE = AbstractSpan
241-
else:
242-
_MIXIN_BASE = object
243-
244-
245-
class HttpSpanMixin(_MIXIN_BASE):
231+
class HttpSpanMixin:
246232
"""Can be used to get HTTP span attributes settings for free."""
247233

248234
_SPAN_COMPONENT = "component"
@@ -253,7 +239,9 @@ class HttpSpanMixin(_MIXIN_BASE):
253239
_NET_PEER_NAME = "net.peer.name"
254240
_NET_PEER_PORT = "net.peer.port"
255241

256-
def set_http_attributes(self, request: HttpRequestType, response: Optional[HttpResponseType] = None) -> None:
242+
def set_http_attributes(
243+
self: AbstractSpan, request: HttpRequestType, response: Optional[HttpResponseType] = None
244+
) -> None:
257245
"""
258246
Add correct attributes for a http client span.
259247
@@ -262,24 +250,25 @@ def set_http_attributes(self, request: HttpRequestType, response: Optional[HttpR
262250
:param response: The response received from the server. Is None if no response received.
263251
:type response: ~azure.core.pipeline.transport.HttpResponse or ~azure.core.pipeline.transport.AsyncHttpResponse
264252
"""
253+
# Also see https://github.com/python/mypy/issues/5837
265254
self.kind = SpanKind.CLIENT
266-
self.add_attribute(self._SPAN_COMPONENT, "http")
267-
self.add_attribute(self._HTTP_METHOD, request.method)
268-
self.add_attribute(self._HTTP_URL, request.url)
255+
self.add_attribute(HttpSpanMixin._SPAN_COMPONENT, "http")
256+
self.add_attribute(HttpSpanMixin._HTTP_METHOD, request.method)
257+
self.add_attribute(HttpSpanMixin._HTTP_URL, request.url)
269258

270259
parsed_url = urlparse(request.url)
271260
if parsed_url.hostname:
272-
self.add_attribute(self._NET_PEER_NAME, parsed_url.hostname)
261+
self.add_attribute(HttpSpanMixin._NET_PEER_NAME, parsed_url.hostname)
273262
if parsed_url.port and parsed_url.port not in [80, 443]:
274-
self.add_attribute(self._NET_PEER_PORT, parsed_url.port)
263+
self.add_attribute(HttpSpanMixin._NET_PEER_PORT, parsed_url.port)
275264

276265
user_agent = request.headers.get("User-Agent")
277266
if user_agent:
278-
self.add_attribute(self._HTTP_USER_AGENT, user_agent)
267+
self.add_attribute(HttpSpanMixin._HTTP_USER_AGENT, user_agent)
279268
if response and response.status_code:
280-
self.add_attribute(self._HTTP_STATUS_CODE, response.status_code)
269+
self.add_attribute(HttpSpanMixin._HTTP_STATUS_CODE, response.status_code)
281270
else:
282-
self.add_attribute(self._HTTP_STATUS_CODE, 504)
271+
self.add_attribute(HttpSpanMixin._HTTP_STATUS_CODE, 504)
283272

284273

285274
class Link:
@@ -291,6 +280,6 @@ class Link:
291280
:type attributes: dict
292281
"""
293282

294-
def __init__(self, headers: Dict[str, str], attributes: Optional["Attributes"] = None) -> None:
283+
def __init__(self, headers: Dict[str, str], attributes: Optional[Attributes] = None) -> None:
295284
self.headers = headers
296285
self.attributes = attributes

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
]
3939

4040

41-
def get_function_and_class_name(func: Callable, *args) -> str:
41+
def get_function_and_class_name(func: Callable, *args: object) -> str:
4242
"""
4343
Given a function and its unamed arguments, returns class_name.function_name. It assumes the first argument
4444
is `self`. If there are no arguments then it only returns the function name.

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ def distributed_trace( # pylint:disable=function-redefined
5050
pass
5151

5252

53-
def distributed_trace(__func: Optional[Callable[P, T]] = None, **kwargs: Any): # pylint:disable=function-redefined
53+
def distributed_trace(
54+
__func: Optional[Callable[P, T]] = None, **kwargs: Any
55+
) -> Any: # pylint:disable=function-redefined
5456
"""Decorator to apply to function to get traced automatically.
5557
5658
Span will use the func name or "name_of_span".

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def distributed_trace_async( # pylint:disable=function-redefined
5151

5252
def distributed_trace_async( # pylint:disable=function-redefined
5353
__func: Optional[Callable[P, Awaitable[T]]] = None, **kwargs: Any
54-
):
54+
) -> Any:
5555
"""Decorator to apply to function to get traced automatically.
5656
5757
Span will use the func name or "name_of_span".

sdk/core/azure-core/azure/core/utils/_utils.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ def __iter__(self) -> Iterator[str]:
148148
def __len__(self) -> int:
149149
return len(self._store)
150150

151-
def lowerkey_items(self):
151+
def lowerkey_items(self) -> Iterator[Tuple[str, Any]]:
152152
return ((lower_case_key, pair[1]) for lower_case_key, pair in self._store.items())
153153

154154
def __eq__(self, other: Any) -> bool:

0 commit comments

Comments
 (0)