29
29
"""
30
30
31
31
from collections import namedtuple
32
+ from enum import Enum
32
33
import logging
33
34
import os
34
35
import six
35
36
import sys
36
37
37
38
try :
38
- from typing import TYPE_CHECKING
39
+ from typing import Type , Optional , Dict , Callable , cast , TYPE_CHECKING
39
40
except ImportError :
40
41
TYPE_CHECKING = False
41
42
42
43
if TYPE_CHECKING :
43
44
from typing import Any , Union
45
+ try :
46
+ from azure .core .tracing .ext .opencensus_span import OpenCensusSpan
47
+ except ImportError :
48
+ pass
44
49
45
50
46
51
from azure .core .tracing import AbstractSpan
49
54
__all__ = ("settings" ,)
50
55
51
56
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
54
61
55
62
56
63
def convert_bool (value ):
@@ -119,7 +126,7 @@ def convert_logging(value):
119
126
120
127
121
128
def get_opencensus_span ():
122
- # type: () -> OpenCensusSpan
129
+ # type: () -> Optional[Type[AbstractSpan]]
123
130
"""Returns the OpenCensusSpan if opencensus is installed else returns None"""
124
131
try :
125
132
from azure .core .tracing .ext .opencensus_span import OpenCensusSpan
@@ -130,16 +137,17 @@ def get_opencensus_span():
130
137
131
138
132
139
def get_opencensus_span_if_opencensus_is_imported ():
140
+ # type: () -> Optional[Type[AbstractSpan]]
133
141
if "opencensus" not in sys .modules :
134
142
return None
135
143
return get_opencensus_span ()
136
144
137
145
138
- _tracing_implementation_dict = {"opencensus" : get_opencensus_span }
146
+ _tracing_implementation_dict = {"opencensus" : get_opencensus_span } # type: Dict[str, Callable[[], Optional[Type[AbstractSpan]]]]
139
147
140
148
141
149
def convert_tracing_impl (value ):
142
- # type: (Union[str, AbstractSpan]) -> AbstractSpan
150
+ # type: (Union[str, Type[ AbstractSpan]] ) -> Optional[Type[ AbstractSpan]]
143
151
"""Convert a string to AbstractSpan
144
152
145
153
If a AbstractSpan is passed in, it is returned as-is. Otherwise the function
@@ -156,19 +164,21 @@ def convert_tracing_impl(value):
156
164
if value is None :
157
165
return get_opencensus_span_if_opencensus_is_imported ()
158
166
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 )
169
178
)
170
-
171
- return wrapper_class
179
+ )
180
+ # type ignored until https://github.com/python/mypy/issues/7279
181
+ return wrapper_class # type: ignore
172
182
173
183
174
184
class PrioritizedSetting (object ):
0 commit comments