From 58f2d161d4b772ce6e62d9f40ba9de16445f4193 Mon Sep 17 00:00:00 2001 From: Aaron Abbott Date: Fri, 17 Jan 2025 10:53:48 -0500 Subject: [PATCH] Fix span context manager typing by using ParamSpec from typing_extensions (#4389) --- CHANGELOG.md | 2 ++ .../src/opentelemetry/util/_decorator.py | 14 ++++++++------ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ed24f385057..325b288ccfc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ([#4353](https://github.com/open-telemetry/opentelemetry-python/pull/4353)) - sdk: don't log or print warnings when the SDK has been disabled ([#4371](https://github.com/open-telemetry/opentelemetry-python/pull/4371)) +- Fix span context manager typing by using ParamSpec from typing_extensions + ([#4389](https://github.com/open-telemetry/opentelemetry-python/pull/4389)) ## Version 1.29.0/0.50b0 (2024-12-11) diff --git a/opentelemetry-api/src/opentelemetry/util/_decorator.py b/opentelemetry-api/src/opentelemetry/util/_decorator.py index 5bf88552b85..cddc395feb8 100644 --- a/opentelemetry-api/src/opentelemetry/util/_decorator.py +++ b/opentelemetry-api/src/opentelemetry/util/_decorator.py @@ -15,18 +15,20 @@ import asyncio import contextlib import functools -import typing -from typing import Callable, Generic, Iterator, TypeVar +from typing import TYPE_CHECKING, Callable, Generic, Iterator, TypeVar V = TypeVar("V") R = TypeVar("R") # Return type Pargs = TypeVar("Pargs") # Generic type for arguments Pkwargs = TypeVar("Pkwargs") # Generic type for arguments -if hasattr(typing, "ParamSpec"): - # only available in python 3.10+ - # https://peps.python.org/pep-0612/ - P = typing.ParamSpec("P") # Generic type for all arguments +# We don't actually depend on typing_extensions but we can use it in CI with this conditional +# import. ParamSpec can be imported directly from typing after python 3.9 is dropped +# https://peps.python.org/pep-0612/. +if TYPE_CHECKING: + from typing_extensions import ParamSpec + + P = ParamSpec("P") # Generic type for all arguments class _AgnosticContextManager(