Skip to content

Commit 1f93a36

Browse files
c24treyang
authored andcommitted
Type hint edits for #57 (#59)
1 parent 123887e commit 1f93a36

File tree

6 files changed

+44
-33
lines changed

6 files changed

+44
-33
lines changed

mypy-relaxed.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[mypy]
44
disallow_any_unimported = True
55
; disallow_any_expr = True
6-
; disallow_any_decorated = True
6+
disallow_any_decorated = True
77
; disallow_any_explicit = True
88
disallow_any_generics = True
99
disallow_subclassing_any = True

mypy.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[mypy]
22
disallow_any_unimported = True
33
disallow_any_expr = True
4-
; disallow_any_decorated = True
4+
disallow_any_decorated = True
55
; disallow_any_explicit = True
66
disallow_any_generics = True
77
disallow_subclassing_any = True

opentelemetry-api/src/opentelemetry/context/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616

1717
from .base_context import BaseRuntimeContext
1818

19+
1920
__all__ = ['Context']
2021

21-
Context: typing.Union[BaseRuntimeContext, None] = None
22+
23+
Context: typing.Optional[BaseRuntimeContext]
2224

2325
try:
2426
from .async_context import AsyncRuntimeContext

opentelemetry-api/src/opentelemetry/context/async_context.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,31 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from contextvars import ContextVar
1615
import typing
16+
from contextvars import ContextVar
1717

18-
from .base_context import BaseRuntimeContext
18+
from . import base_context
1919

2020

21-
class AsyncRuntimeContext(BaseRuntimeContext):
22-
class Slot(BaseRuntimeContext.Slot):
23-
def __init__(self, name: str, default: typing.Any):
21+
class AsyncRuntimeContext(base_context.BaseRuntimeContext):
22+
class Slot(base_context.BaseRuntimeContext.Slot):
23+
def __init__(self, name: str, default: 'object'):
2424
# pylint: disable=super-init-not-called
2525
self.name = name
26-
self.contextvar: typing.Any = ContextVar(name)
27-
self.default = default if callable(default) else (lambda: default)
26+
self.contextvar: 'ContextVar[object]' = ContextVar(name)
27+
self.default: typing.Callable[..., object]
28+
self.default = base_context.wrap_callable(default)
2829

2930
def clear(self) -> None:
3031
self.contextvar.set(self.default())
3132

32-
def get(self) -> typing.Any:
33+
def get(self) -> 'object':
3334
try:
3435
return self.contextvar.get()
3536
except LookupError:
3637
value = self.default()
3738
self.set(value)
3839
return value
3940

40-
def set(self, value: typing.Any) -> None:
41+
def set(self, value: 'object') -> None:
4142
self.contextvar.set(value)

opentelemetry-api/src/opentelemetry/context/base_context.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,24 @@
1616
import typing
1717

1818

19+
def wrap_callable(target: 'object') -> typing.Callable[[], object]:
20+
if callable(target):
21+
return target
22+
return lambda: target
23+
24+
1925
class BaseRuntimeContext:
2026
class Slot:
21-
def __init__(self, name: str, default: typing.Any):
27+
def __init__(self, name: str, default: 'object'):
2228
raise NotImplementedError
2329

2430
def clear(self) -> None:
2531
raise NotImplementedError
2632

27-
def get(self) -> typing.Any:
33+
def get(self) -> 'object':
2834
raise NotImplementedError
2935

30-
def set(self, value: typing.Any) -> None:
36+
def set(self, value: 'object') -> None:
3137
raise NotImplementedError
3238

3339
_lock = threading.Lock()
@@ -42,7 +48,7 @@ def clear(cls) -> None:
4248
slot.clear()
4349

4450
@classmethod
45-
def register_slot(cls, name: str, default: typing.Any = None) -> 'Slot':
51+
def register_slot(cls, name: str, default: 'object' = None) -> 'Slot':
4652
"""Register a context slot with an optional default value.
4753
4854
:type name: str
@@ -60,13 +66,13 @@ def register_slot(cls, name: str, default: typing.Any = None) -> 'Slot':
6066
cls._slots[name] = slot
6167
return slot
6268

63-
def apply(self, snapshot: typing.Dict[str, typing.Any]) -> None:
69+
def apply(self, snapshot: typing.Dict[str, 'object']) -> None:
6470
"""Set the current context from a given snapshot dictionary"""
6571

6672
for name in snapshot:
6773
setattr(self, name, snapshot[name])
6874

69-
def snapshot(self) -> typing.Dict[str, typing.Any]:
75+
def snapshot(self) -> typing.Dict[str, 'object']:
7076
"""Return a dictionary of current slots by reference."""
7177

7278
keys = self._slots.keys()
@@ -75,31 +81,31 @@ def snapshot(self) -> typing.Dict[str, typing.Any]:
7581
def __repr__(self) -> str:
7682
return '{}({})'.format(type(self).__name__, self.snapshot())
7783

78-
def __getattr__(self, name: str) -> typing.Any:
84+
def __getattr__(self, name: str) -> 'object':
7985
if name not in self._slots:
8086
self.register_slot(name, None)
8187
slot = self._slots[name]
8288
return slot.get()
8389

84-
def __setattr__(self, name: str, value: typing.Any) -> None:
90+
def __setattr__(self, name: str, value: 'object') -> None:
8591
if name not in self._slots:
8692
self.register_slot(name, None)
8793
slot = self._slots[name]
8894
slot.set(value)
8995

9096
def with_current_context(
9197
self,
92-
func: typing.Callable[..., typing.Any],
93-
) -> typing.Callable[..., typing.Any]:
98+
func: typing.Callable[..., 'object'],
99+
) -> typing.Callable[..., 'object']:
94100
"""Capture the current context and apply it to the provided func.
95101
"""
96102

97103
caller_context = self.snapshot()
98104

99105
def call_with_current_context(
100-
*args: typing.Any,
101-
**kwargs: typing.Any,
102-
) -> typing.Any:
106+
*args: 'object',
107+
**kwargs: 'object',
108+
) -> 'object':
103109
try:
104110
backup_context = self.snapshot()
105111
self.apply(caller_context)

opentelemetry-api/src/opentelemetry/context/thread_local_context.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,30 @@
1515
import threading
1616
import typing
1717

18-
from .base_context import BaseRuntimeContext
18+
from . import base_context
1919

2020

21-
class ThreadLocalRuntimeContext(BaseRuntimeContext):
22-
class Slot(BaseRuntimeContext.Slot):
21+
class ThreadLocalRuntimeContext(base_context.BaseRuntimeContext):
22+
class Slot(base_context.BaseRuntimeContext.Slot):
2323
_thread_local = threading.local()
2424

25-
def __init__(self, name: str, default: typing.Any):
25+
def __init__(self, name: str, default: 'object'):
2626
# pylint: disable=super-init-not-called
2727
self.name = name
28-
self.default = default if callable(default) else (lambda: default)
28+
self.default: typing.Callable[..., object]
29+
self.default = base_context.wrap_callable(default)
2930

3031
def clear(self) -> None:
3132
setattr(self._thread_local, self.name, self.default())
3233

33-
def get(self) -> typing.Any:
34+
def get(self) -> 'object':
3435
try:
35-
return getattr(self._thread_local, self.name)
36+
got: object = getattr(self._thread_local, self.name)
37+
return got
3638
except AttributeError:
3739
value = self.default()
3840
self.set(value)
3941
return value
4042

41-
def set(self, value: typing.Any) -> None:
43+
def set(self, value: 'object') -> None:
4244
setattr(self._thread_local, self.name, value)

0 commit comments

Comments
 (0)