Skip to content

Commit abc3f20

Browse files
authored
MyPy azure-core (Azure#6619)
* MyPy azure-core settings * MyPy azure-core aiohttp * Redesign sentinel
1 parent e12b658 commit abc3f20

File tree

2 files changed

+30
-20
lines changed

2 files changed

+30
-20
lines changed

sdk/core/azure-core/azure/core/pipeline/transport/aiohttp.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import aiohttp
3232

3333
from azure.core.configuration import ConnectionConfiguration
34-
from azure.core.exceptions import ServiceRequestError, ServiceResponseError
34+
from azure.core.exceptions import ServiceRequestError, ServiceResponseError, AzureError
3535
from azure.core.pipeline import Pipeline
3636

3737
from requests.exceptions import (
@@ -159,7 +159,7 @@ async def send(self, request: HttpRequest, **config: Any) -> Optional[AsyncHttpR
159159
config['proxy'] = proxies[protocol]
160160
break
161161

162-
error = None
162+
error = None # type: Optional[AzureError]
163163
response = None
164164
config['ssl'] = self._build_ssl_config(
165165
cert=config.pop('connection_cert', self.connection_config.cert),

sdk/core/azure-core/azure/core/settings.py

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,23 @@
2929
"""
3030

3131
from collections import namedtuple
32+
from enum import Enum
3233
import logging
3334
import os
3435
import six
3536
import sys
3637

3738
try:
38-
from typing import TYPE_CHECKING
39+
from typing import Type, Optional, Dict, Callable, cast, TYPE_CHECKING
3940
except ImportError:
4041
TYPE_CHECKING = False
4142

4243
if TYPE_CHECKING:
4344
from typing import Any, Union
45+
try:
46+
from azure.core.tracing.ext.opencensus_span import OpenCensusSpan
47+
except ImportError:
48+
pass
4449

4550

4651
from azure.core.tracing import AbstractSpan
@@ -49,8 +54,10 @@
4954
__all__ = ("settings",)
5055

5156

52-
class _Unset(object):
53-
pass
57+
# https://www.python.org/dev/peps/pep-0484/#support-for-singleton-types-in-unions
58+
class _Unset(Enum):
59+
token = 0
60+
_unset = _Unset.token
5461

5562

5663
def convert_bool(value):
@@ -119,7 +126,7 @@ def convert_logging(value):
119126

120127

121128
def get_opencensus_span():
122-
# type: () -> OpenCensusSpan
129+
# type: () -> Optional[Type[AbstractSpan]]
123130
"""Returns the OpenCensusSpan if opencensus is installed else returns None"""
124131
try:
125132
from azure.core.tracing.ext.opencensus_span import OpenCensusSpan
@@ -130,16 +137,17 @@ def get_opencensus_span():
130137

131138

132139
def get_opencensus_span_if_opencensus_is_imported():
140+
# type: () -> Optional[Type[AbstractSpan]]
133141
if "opencensus" not in sys.modules:
134142
return None
135143
return get_opencensus_span()
136144

137145

138-
_tracing_implementation_dict = {"opencensus": get_opencensus_span}
146+
_tracing_implementation_dict = {"opencensus": get_opencensus_span} # type: Dict[str, Callable[[], Optional[Type[AbstractSpan]]]]
139147

140148

141149
def convert_tracing_impl(value):
142-
# type: (Union[str, AbstractSpan]) -> AbstractSpan
150+
# type: (Union[str, Type[AbstractSpan]]) -> Optional[Type[AbstractSpan]]
143151
"""Convert a string to AbstractSpan
144152
145153
If a AbstractSpan is passed in, it is returned as-is. Otherwise the function
@@ -156,19 +164,21 @@ def convert_tracing_impl(value):
156164
if value is None:
157165
return get_opencensus_span_if_opencensus_is_imported()
158166

159-
wrapper_class = value
160-
if isinstance(value, six.string_types):
161-
value = value.lower()
162-
get_wrapper_class = _tracing_implementation_dict.get(value, lambda: _Unset)
163-
wrapper_class = get_wrapper_class()
164-
if wrapper_class is _Unset:
165-
raise ValueError(
166-
"Cannot convert {} to AbstractSpan, valid values are: {}".format(
167-
value, ", ".join(_tracing_implementation_dict)
168-
)
167+
if not isinstance(value, six.string_types):
168+
return value
169+
170+
value = cast(str, value) # mypy clarity
171+
value = value.lower()
172+
get_wrapper_class = _tracing_implementation_dict.get(value, lambda: _unset)
173+
wrapper_class = get_wrapper_class() # type: Union[None, _Unset, Type[AbstractSpan]]
174+
if wrapper_class is _unset:
175+
raise ValueError(
176+
"Cannot convert {} to AbstractSpan, valid values are: {}".format(
177+
value, ", ".join(_tracing_implementation_dict)
169178
)
170-
171-
return wrapper_class
179+
)
180+
# type ignored until https://github.com/python/mypy/issues/7279
181+
return wrapper_class # type: ignore
172182

173183

174184
class PrioritizedSetting(object):

0 commit comments

Comments
 (0)