Skip to content

Commit

Permalink
Add type hints to BaseInstrumentor (#3084)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kludex authored Dec 11, 2024
1 parent ecf5529 commit 7804e0a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand All @@ -40,7 +42,7 @@ def __str__(self):

def get_dist_dependency_conflicts(
dist: Distribution,
) -> Optional[DependencyConflict]:
) -> DependencyConflict | None:
instrumentation_deps = []
extra = "extra"
instruments = "instruments"
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 7804e0a

Please sign in to comment.