Skip to content

Commit 6bebdca

Browse files
patchback[bot]nightcitybladeDreamsorcerer
authored
[PR #12113/4d15c331 backport][3.14] fix: widen trace_request_ctx type to accept any object (#12118)
**This is a backport of PR #12113 as merged into master (4d15c33).** --------- Co-authored-by: nightcityblade <[email protected]> Co-authored-by: Sam Bull <[email protected]>
1 parent 58e7f8f commit 6bebdca

4 files changed

Lines changed: 18 additions & 8 deletions

File tree

CHANGES/10753.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Widened ``trace_request_ctx`` parameter type from ``Mapping[str, Any] | None`` to ``object`` to allow passing instances of user-defined classes as trace context -- by :user:`nightcityblade`.

aiohttp/client.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
Coroutine,
1515
Generator,
1616
Iterable,
17-
Mapping,
1817
Sequence,
1918
)
2019
from contextlib import suppress
@@ -194,7 +193,7 @@ class _RequestOptions(TypedDict, total=False):
194193
ssl: SSLContext | bool | Fingerprint
195194
server_hostname: str | None
196195
proxy_headers: LooseHeaders | None
197-
trace_request_ctx: Mapping[str, Any] | None
196+
trace_request_ctx: object
198197
read_bufsize: int | None
199198
auto_decompress: bool | None
200199
max_line_size: int | None
@@ -548,7 +547,7 @@ async def _request(
548547
ssl: SSLContext | bool | Fingerprint = True,
549548
server_hostname: str | None = None,
550549
proxy_headers: LooseHeaders | None = None,
551-
trace_request_ctx: Mapping[str, Any] | None = None,
550+
trace_request_ctx: object = None,
552551
read_bufsize: int | None = None,
553552
auto_decompress: bool | None = None,
554553
max_line_size: int | None = None,

aiohttp/tracing.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
from collections.abc import Mapping
21
from types import SimpleNamespace
3-
from typing import TYPE_CHECKING, TypeVar
2+
from typing import TYPE_CHECKING, Any, TypeVar
43

54
import attr
65
from aiosignal import Signal
@@ -86,9 +85,7 @@ def __init__(
8685

8786
self._trace_config_ctx_factory = trace_config_ctx_factory
8887

89-
def trace_config_ctx(
90-
self, trace_request_ctx: Mapping[str, str] | None = None
91-
) -> SimpleNamespace:
88+
def trace_config_ctx(self, trace_request_ctx: Any = None) -> SimpleNamespace:
9289
"""Return a new trace_config_ctx instance"""
9390
return self._trace_config_ctx_factory(trace_request_ctx=trace_request_ctx)
9491

tests/test_tracing.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,19 @@ def test_trace_config_ctx_request_ctx(self) -> None:
4242
)
4343
assert trace_config_ctx.trace_request_ctx is trace_request_ctx
4444

45+
def test_trace_config_ctx_custom_class(self) -> None:
46+
"""Custom class instances should be accepted as trace_request_ctx (#10753)."""
47+
48+
class MyContext:
49+
def __init__(self, request_id: int) -> None:
50+
self.request_id = request_id
51+
52+
ctx = MyContext(request_id=42)
53+
trace_config = TraceConfig()
54+
trace_config_ctx = trace_config.trace_config_ctx(trace_request_ctx=ctx)
55+
assert trace_config_ctx.trace_request_ctx is ctx
56+
assert trace_config_ctx.trace_request_ctx.request_id == 42
57+
4558
def test_freeze(self) -> None:
4659
trace_config = TraceConfig()
4760
trace_config.freeze()

0 commit comments

Comments
 (0)