Skip to content

Commit 8c6b4c6

Browse files
committed
Add type annotations
1 parent 1dadd33 commit 8c6b4c6

File tree

5 files changed

+15
-14
lines changed

5 files changed

+15
-14
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99

10+
- Add type annotations to context's attach & detach
11+
([#4164](https://github.com/open-telemetry/opentelemetry-python/pull/4164))
12+
13+
1014
## Version 1.27.0/0.48b0 (2024-08-28)
1115

1216
- Implementation of Events API

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import logging
1616
import typing
17+
from contextvars import Token
1718
from os import environ
1819
from uuid import uuid4
1920

@@ -130,7 +131,7 @@ def get_current() -> Context:
130131
return _RUNTIME_CONTEXT.get_current()
131132

132133

133-
def attach(context: Context) -> object:
134+
def attach(context: Context) -> Token[Context]:
134135
"""Associates a Context with the caller's current execution unit. Returns
135136
a token that can be used to restore the previous Context.
136137
@@ -143,7 +144,7 @@ def attach(context: Context) -> object:
143144
return _RUNTIME_CONTEXT.attach(context)
144145

145146

146-
def detach(token: object) -> None:
147+
def detach(token: Token[Context]) -> None:
147148
"""Resets the Context associated with the caller's current execution unit
148149
to the value it had before attaching a specified Context.
149150
@@ -152,8 +153,6 @@ def detach(token: object) -> None:
152153
"""
153154
try:
154155
_RUNTIME_CONTEXT.detach(token)
155-
except TypeError:
156-
logger.exception("Expected an instance of Token, got None")
157156
except Exception: # pylint: disable=broad-exception-caught
158157
logger.exception("Failed to detach context")
159158

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import typing
1616
from abc import ABC, abstractmethod
17+
from contextvars import Token
1718

1819

1920
class Context(typing.Dict[str, object]):
@@ -29,7 +30,7 @@ class _RuntimeContext(ABC):
2930
"""
3031

3132
@abstractmethod
32-
def attach(self, context: Context) -> object:
33+
def attach(self, context: Context) -> Token[Context]:
3334
"""Sets the current `Context` object. Returns a
3435
token that can be used to reset to the previous `Context`.
3536
@@ -42,7 +43,7 @@ def get_current(self) -> Context:
4243
"""Returns the current `Context` object."""
4344

4445
@abstractmethod
45-
def detach(self, token: object) -> None:
46+
def detach(self, token: Token[Context]) -> None:
4647
"""Resets Context to a previous value
4748
4849
Args:

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
from contextvars import ContextVar
14+
from contextvars import ContextVar, Token
1515

1616
from opentelemetry.context.context import Context, _RuntimeContext
1717

@@ -28,7 +28,7 @@ def __init__(self) -> None:
2828
self._CONTEXT_KEY, default=Context()
2929
)
3030

31-
def attach(self, context: Context) -> object:
31+
def attach(self, context: Context) -> Token[Context]:
3232
"""Sets the current `Context` object. Returns a
3333
token that can be used to reset to the previous `Context`.
3434
@@ -41,16 +41,13 @@ def get_current(self) -> Context:
4141
"""Returns the current `Context` object."""
4242
return self._current_context.get()
4343

44-
def detach(self, token: object) -> None:
44+
def detach(self, token: Token[Context]) -> None:
4545
"""Resets Context to a previous value
4646
4747
Args:
4848
token: A reference to a previous Context.
4949
"""
50-
try:
51-
self._current_context.reset(token)
52-
except TypeError:
53-
raise TypeError
50+
self._current_context.reset(token)
5451

5552

5653
__all__ = ["ContextVarsRuntimeContext"]

opentelemetry-api/tests/context/base_context.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def test_attach(self):
6464
self.assertEqual("yyy", context.get_value("a"))
6565

6666
with self.assertLogs(level=ERROR):
67-
context.detach("some garbage")
67+
context.detach("some garbage") # type:ignore
6868

6969
def test_detach_out_of_order(self):
7070
t1 = context.attach(context.set_value("c", 1))

0 commit comments

Comments
 (0)