From 7804e0a4e81f741607c9007226d0b39c697d7aea Mon Sep 17 00:00:00 2001 From: Marcelo Trylesinski Date: Wed, 11 Dec 2024 10:37:51 +0100 Subject: [PATCH] Add type hints to BaseInstrumentor (#3084) --- .../instrumentation/dependencies.py | 16 +++++++++------- .../instrumentation/instrumentor.py | 16 +++++++++------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/dependencies.py b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/dependencies.py index 7ebc88d647..8f0a383412 100644 --- a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/dependencies.py +++ b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/dependencies.py @@ -12,8 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. +from __future__ import annotations + from logging import getLogger -from typing import Collection, Optional, Union +from typing import Collection from packaging.requirements import InvalidRequirement, Requirement @@ -27,10 +29,10 @@ class DependencyConflict: - required: str = None - found: Optional[str] = None + required: str | None = None + found: str | None = None - def __init__(self, required, found=None): + def __init__(self, required: str | None, found: str | None = None): self.required = required self.found = found @@ -40,7 +42,7 @@ def __str__(self): def get_dist_dependency_conflicts( dist: Distribution, -) -> Optional[DependencyConflict]: +) -> DependencyConflict | None: instrumentation_deps = [] extra = "extra" instruments = "instruments" @@ -57,8 +59,8 @@ def get_dist_dependency_conflicts( def get_dependency_conflicts( - deps: Collection[Union[str, Requirement]], -) -> Optional[DependencyConflict]: + deps: Collection[str | Requirement], +) -> DependencyConflict | None: for dep in deps: if isinstance(dep, Requirement): req = dep diff --git a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/instrumentor.py b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/instrumentor.py index c612bfeceb..cf079dbfb7 100644 --- a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/instrumentor.py +++ b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/instrumentor.py @@ -17,9 +17,11 @@ OpenTelemetry Base Instrumentor """ +from __future__ import annotations + from abc import ABC, abstractmethod from logging import getLogger -from typing import Collection, Optional +from typing import Any, Collection from opentelemetry.instrumentation._semconv import ( _OpenTelemetrySemanticConventionStability, @@ -33,7 +35,7 @@ class BaseInstrumentor(ABC): - """An ABC for instrumentors + """An ABC for instrumentors. Child classes of this ABC should instrument specific third party libraries or frameworks either by using the @@ -74,18 +76,18 @@ def instrumentation_dependencies(self) -> Collection[str]: is present in the environment. """ - def _instrument(self, **kwargs): + def _instrument(self, **kwargs: Any): """Instrument the library""" @abstractmethod - def _uninstrument(self, **kwargs): + def _uninstrument(self, **kwargs: Any): """Uninstrument the library""" - def _check_dependency_conflicts(self) -> Optional[DependencyConflict]: + def _check_dependency_conflicts(self) -> DependencyConflict | None: dependencies = self.instrumentation_dependencies() return get_dependency_conflicts(dependencies) - def instrument(self, **kwargs): + def instrument(self, **kwargs: Any): """Instrument the library This method will be called without any optional arguments by the @@ -117,7 +119,7 @@ def instrument(self, **kwargs): self._is_instrumented_by_opentelemetry = True return result - def uninstrument(self, **kwargs): + def uninstrument(self, **kwargs: Any): """Uninstrument the library See ``BaseInstrumentor.instrument`` for more information regarding the