Skip to content

Commit 38a8ac8

Browse files
authored
Fix auto-tracing Python 3.12 ParamSpec syntax (#1247)
1 parent 464203c commit 38a8ac8

File tree

4 files changed

+13
-15
lines changed

4 files changed

+13
-15
lines changed

logfire/_internal/ast_utils.py

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import ast
44
from dataclasses import dataclass
5-
from typing import cast
5+
from typing import Any, cast
66

77
from opentelemetry.util import types as otel_types
88

@@ -104,19 +104,10 @@ def rewrite_function(self, node: ast.FunctionDef | ast.AsyncFunctionDef, qualnam
104104
)
105105
new_body.append(span)
106106

107-
return ast.fix_missing_locations(
108-
ast.copy_location(
109-
type(node)( # type: ignore
110-
name=node.name,
111-
args=node.args,
112-
body=new_body,
113-
decorator_list=node.decorator_list,
114-
returns=node.returns,
115-
type_comment=node.type_comment,
116-
),
117-
node,
118-
)
119-
)
107+
kwargs: dict[str, Any] = {name: getattr(node, name, None) for name in node._fields}
108+
kwargs['body'] = new_body
109+
new_node = type(node)(**kwargs)
110+
return ast.fix_missing_locations(ast.copy_location(new_node, node))
120111

121112
def logfire_method_call_node(self, node: ast.FunctionDef | ast.AsyncFunctionDef, qualname: str) -> ast.Call:
122113
raise NotImplementedError()

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ include = ["/README.md", "CHANGELOG.md", "/Makefile", "/logfire", "/tests"]
216216
# https://beta.ruff.rs/docs/configuration/
217217
[tool.ruff]
218218
line-length = 120
219-
extend-exclude = ["logfire-api/logfire_api/*"]
219+
extend-exclude = ["logfire-api/logfire_api/*", "tests/auto_trace_samples/param_spec.py"]
220220

221221
[tool.ruff.lint]
222222
extend-select = [
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def check_param_spec_syntax[**P](*args: P.args, **kwargs: P.kwargs):
2+
return args, kwargs

tests/test_auto_trace.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,11 @@ def test_auto_trace_sample(exporter: TestExporter) -> None:
185185
]
186186
)
187187

188+
if sys.version_info >= (3, 12):
189+
from tests.auto_trace_samples import param_spec
190+
191+
assert param_spec.check_param_spec_syntax(1, 2, a=3, b=4) == ((1, 2), {'a': 3, 'b': 4}) # type: ignore
192+
188193

189194
def test_check_already_imported() -> None:
190195
# Check that nothing gets imported during this test,

0 commit comments

Comments
 (0)