Skip to content

Commit 767dd16

Browse files
committed
type hint
1 parent 572f1b0 commit 767dd16

File tree

4 files changed

+28
-17
lines changed

4 files changed

+28
-17
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import typing
16+
1517
from .base_context import BaseRuntimeContext
1618

1719
__all__ = ['Context']
1820

19-
Context: BaseRuntimeContext = None
21+
Context: typing.Union[BaseRuntimeContext, None] = None
2022

2123
try:
2224
from .async_context import AsyncRuntimeContext

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

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

15-
import contextvars
15+
from contextvars import ContextVar
16+
import typing
1617

1718
from .base_context import BaseRuntimeContext
1819

1920

2021
class AsyncRuntimeContext(BaseRuntimeContext):
2122
class Slot(BaseRuntimeContext.Slot):
22-
def __init__(self, name, default):
23+
def __init__(self, name: str, default: typing.Any):
2324
# pylint: disable=super-init-not-called
2425
self.name = name
25-
self.contextvar = contextvars.ContextVar(name)
26+
self.contextvar: typing.Any = ContextVar(name)
2627
self.default = default if callable(default) else (lambda: default)
2728

28-
def clear(self):
29+
def clear(self) -> None:
2930
self.contextvar.set(self.default())
3031

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

39-
def set(self, value):
40+
def set(self, value: typing.Any) -> None:
4041
self.contextvar.set(value)

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def register_slot(cls, name: str, default: typing.Any = None) -> 'Slot':
6060
cls._slots[name] = slot
6161
return slot
6262

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

6666
for name in snapshot:
@@ -75,24 +75,31 @@ def snapshot(self) -> typing.Dict[str, typing.Any]:
7575
def __repr__(self) -> str:
7676
return '{}({})'.format(type(self).__name__, self.snapshot())
7777

78-
def __getattr__(self, name) -> typing.Any:
78+
def __getattr__(self, name: str) -> typing.Any:
7979
if name not in self._slots:
8080
self.register_slot(name, None)
8181
slot = self._slots[name]
8282
return slot.get()
8383

84-
def __setattr__(self, name, value) -> None:
84+
def __setattr__(self, name: str, value: typing.Any) -> None:
8585
if name not in self._slots:
8686
self.register_slot(name, None)
8787
slot = self._slots[name]
8888
slot.set(value)
8989

90-
def with_current_context(self, func: typing.Callable) -> typing.Callable:
91-
"""Capture the current context and apply it to the provided func"""
90+
def with_current_context(
91+
self,
92+
func: typing.Callable[..., typing.Any],
93+
) -> typing.Callable[..., typing.Any]:
94+
"""Capture the current context and apply it to the provided func.
95+
"""
9296

9397
caller_context = self.snapshot()
9498

95-
def call_with_current_context(*args, **kwargs) -> typing.Any:
99+
def call_with_current_context(
100+
*args: typing.Any,
101+
**kwargs: typing.Any,
102+
) -> typing.Any:
96103
try:
97104
backup_context = self.snapshot()
98105
self.apply(caller_context)

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
import threading
16+
import typing
1617

1718
from .base_context import BaseRuntimeContext
1819

@@ -21,21 +22,21 @@ class ThreadLocalRuntimeContext(BaseRuntimeContext):
2122
class Slot(BaseRuntimeContext.Slot):
2223
_thread_local = threading.local()
2324

24-
def __init__(self, name, default):
25+
def __init__(self, name: str, default: typing.Any):
2526
# pylint: disable=super-init-not-called
2627
self.name = name
2728
self.default = default if callable(default) else (lambda: default)
2829

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

32-
def get(self):
33+
def get(self) -> typing.Any:
3334
try:
3435
return getattr(self._thread_local, self.name)
3536
except AttributeError:
3637
value = self.default()
3738
self.set(value)
3839
return value
3940

40-
def set(self, value):
41+
def set(self, value: typing.Any) -> None:
4142
setattr(self._thread_local, self.name, value)

0 commit comments

Comments
 (0)