Skip to content

Commit c1d4e28

Browse files
Kludexxrmx
authored andcommitted
Add type hints to BaseInstrumentor (open-telemetry#3084)
1 parent f78b935 commit c1d4e28

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

Diff for: opentelemetry-instrumentation/src/opentelemetry/instrumentation/dependencies.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from __future__ import annotations
16+
1517
from logging import getLogger
16-
from typing import Collection, Optional, Union
18+
from typing import Collection
1719

1820
from packaging.requirements import InvalidRequirement, Requirement
1921

@@ -27,10 +29,10 @@
2729

2830

2931
class DependencyConflict:
30-
required: str = None
31-
found: Optional[str] = None
32+
required: str | None = None
33+
found: str | None = None
3234

33-
def __init__(self, required, found=None):
35+
def __init__(self, required: str | None, found: str | None = None):
3436
self.required = required
3537
self.found = found
3638

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

4143
def get_dist_dependency_conflicts(
4244
dist: Distribution,
43-
) -> Optional[DependencyConflict]:
45+
) -> DependencyConflict | None:
4446
instrumentation_deps = []
4547
extra = "extra"
4648
instruments = "instruments"
@@ -57,8 +59,8 @@ def get_dist_dependency_conflicts(
5759

5860

5961
def get_dependency_conflicts(
60-
deps: Collection[Union[str, Requirement]],
61-
) -> Optional[DependencyConflict]:
62+
deps: Collection[str | Requirement],
63+
) -> DependencyConflict | None:
6264
for dep in deps:
6365
if isinstance(dep, Requirement):
6466
req = dep

Diff for: opentelemetry-instrumentation/src/opentelemetry/instrumentation/instrumentor.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717
OpenTelemetry Base Instrumentor
1818
"""
1919

20+
from __future__ import annotations
21+
2022
from abc import ABC, abstractmethod
2123
from logging import getLogger
22-
from typing import Collection, Optional
24+
from typing import Any, Collection
2325

2426
from opentelemetry.instrumentation._semconv import (
2527
_OpenTelemetrySemanticConventionStability,
@@ -33,7 +35,7 @@
3335

3436

3537
class BaseInstrumentor(ABC):
36-
"""An ABC for instrumentors
38+
"""An ABC for instrumentors.
3739
3840
Child classes of this ABC should instrument specific third
3941
party libraries or frameworks either by using the
@@ -74,18 +76,18 @@ def instrumentation_dependencies(self) -> Collection[str]:
7476
is present in the environment.
7577
"""
7678

77-
def _instrument(self, **kwargs):
79+
def _instrument(self, **kwargs: Any):
7880
"""Instrument the library"""
7981

8082
@abstractmethod
81-
def _uninstrument(self, **kwargs):
83+
def _uninstrument(self, **kwargs: Any):
8284
"""Uninstrument the library"""
8385

84-
def _check_dependency_conflicts(self) -> Optional[DependencyConflict]:
86+
def _check_dependency_conflicts(self) -> DependencyConflict | None:
8587
dependencies = self.instrumentation_dependencies()
8688
return get_dependency_conflicts(dependencies)
8789

88-
def instrument(self, **kwargs):
90+
def instrument(self, **kwargs: Any):
8991
"""Instrument the library
9092
9193
This method will be called without any optional arguments by the
@@ -117,7 +119,7 @@ def instrument(self, **kwargs):
117119
self._is_instrumented_by_opentelemetry = True
118120
return result
119121

120-
def uninstrument(self, **kwargs):
122+
def uninstrument(self, **kwargs: Any):
121123
"""Uninstrument the library
122124
123125
See ``BaseInstrumentor.instrument`` for more information regarding the

0 commit comments

Comments
 (0)