Skip to content
Merged
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
4 changes: 2 additions & 2 deletions sdk/core/azure-core/azure/core/pipeline/transport/aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import aiohttp

from azure.core.configuration import ConnectionConfiguration
from azure.core.exceptions import ServiceRequestError, ServiceResponseError
from azure.core.exceptions import ServiceRequestError, ServiceResponseError, AzureError
from azure.core.pipeline import Pipeline

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

error = None
error = None # type: Optional[AzureError]
response = None
config['ssl'] = self._build_ssl_config(
cert=config.pop('connection_cert', self.connection_config.cert),
Expand Down
46 changes: 28 additions & 18 deletions sdk/core/azure-core/azure/core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,23 @@
"""

from collections import namedtuple
from enum import Enum
import logging
import os
import six
import sys

try:
from typing import TYPE_CHECKING
from typing import Type, Optional, Dict, Callable, cast, TYPE_CHECKING
except ImportError:
TYPE_CHECKING = False

if TYPE_CHECKING:
from typing import Any, Union
try:
from azure.core.tracing.ext.opencensus_span import OpenCensusSpan
except ImportError:
pass


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


class _Unset(object):
pass
# https://www.python.org/dev/peps/pep-0484/#support-for-singleton-types-in-unions
class _Unset(Enum):
token = 0
_unset = _Unset.token


def convert_bool(value):
Expand Down Expand Up @@ -119,7 +126,7 @@ def convert_logging(value):


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


def get_opencensus_span_if_opencensus_is_imported():
# type: () -> Optional[Type[AbstractSpan]]
if "opencensus" not in sys.modules:
return None
return get_opencensus_span()


_tracing_implementation_dict = {"opencensus": get_opencensus_span}
_tracing_implementation_dict = {"opencensus": get_opencensus_span} # type: Dict[str, Callable[[], Optional[Type[AbstractSpan]]]]


def convert_tracing_impl(value):
# type: (Union[str, AbstractSpan]) -> AbstractSpan
# type: (Union[str, Type[AbstractSpan]]) -> Optional[Type[AbstractSpan]]
"""Convert a string to AbstractSpan

If a AbstractSpan is passed in, it is returned as-is. Otherwise the function
Expand All @@ -156,19 +164,21 @@ def convert_tracing_impl(value):
if value is None:
return get_opencensus_span_if_opencensus_is_imported()

wrapper_class = value
if isinstance(value, six.string_types):
value = value.lower()
get_wrapper_class = _tracing_implementation_dict.get(value, lambda: _Unset)
wrapper_class = get_wrapper_class()
if wrapper_class is _Unset:
raise ValueError(
"Cannot convert {} to AbstractSpan, valid values are: {}".format(
value, ", ".join(_tracing_implementation_dict)
)
if not isinstance(value, six.string_types):
return value

value = cast(str, value) # mypy clarity
value = value.lower()
get_wrapper_class = _tracing_implementation_dict.get(value, lambda: _unset)
wrapper_class = get_wrapper_class() # type: Union[None, _Unset, Type[AbstractSpan]]
if wrapper_class is _unset:
raise ValueError(
"Cannot convert {} to AbstractSpan, valid values are: {}".format(
value, ", ".join(_tracing_implementation_dict)
)

return wrapper_class
)
# type ignored until https://github.com/python/mypy/issues/7279
return wrapper_class # type: ignore


class PrioritizedSetting(object):
Expand Down