diff --git a/CHANGELOG.md b/CHANGELOG.md index e6f75e7b965..6604cccea1d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased](https://github.com/open-telemetry/opentelemetry-python/compare/v1.12.0rc1-0.31b0...HEAD) +- Fix type hints for textmap `Getter` and `Setter` + ([#2657](https://github.com/open-telemetry/opentelemetry-python/pull/2657)) - Fix LogEmitterProvider.force_flush hanging randomly ([#2714](https://github.com/open-telemetry/opentelemetry-python/pull/2714)) - narrow protobuf dependencies to exclude protobuf >= 4 diff --git a/opentelemetry-api/src/opentelemetry/baggage/propagation/__init__.py b/opentelemetry-api/src/opentelemetry/baggage/propagation/__init__.py index d194acfe2ef..edba837b816 100644 --- a/opentelemetry-api/src/opentelemetry/baggage/propagation/__init__.py +++ b/opentelemetry-api/src/opentelemetry/baggage/propagation/__init__.py @@ -38,7 +38,7 @@ def extract( self, carrier: textmap.CarrierT, context: Optional[Context] = None, - getter: textmap.Getter = textmap.default_getter, + getter: textmap.Getter[textmap.CarrierT] = textmap.default_getter, ) -> Context: """Extract Baggage from the carrier. @@ -109,7 +109,7 @@ def inject( self, carrier: textmap.CarrierT, context: Optional[Context] = None, - setter: textmap.Setter = textmap.default_setter, + setter: textmap.Setter[textmap.CarrierT] = textmap.default_setter, ) -> None: """Injects Baggage into the carrier. diff --git a/opentelemetry-api/src/opentelemetry/propagate/__init__.py b/opentelemetry-api/src/opentelemetry/propagate/__init__.py index 5493c5f0882..f197f1f9149 100644 --- a/opentelemetry-api/src/opentelemetry/propagate/__init__.py +++ b/opentelemetry-api/src/opentelemetry/propagate/__init__.py @@ -84,7 +84,7 @@ def example_route(): def extract( carrier: textmap.CarrierT, context: typing.Optional[Context] = None, - getter: textmap.Getter = textmap.default_getter, + getter: textmap.Getter[textmap.CarrierT] = textmap.default_getter, ) -> Context: """Uses the configured propagator to extract a Context from the carrier. @@ -105,7 +105,7 @@ def extract( def inject( carrier: textmap.CarrierT, context: typing.Optional[Context] = None, - setter: textmap.Setter = textmap.default_setter, + setter: textmap.Setter[textmap.CarrierT] = textmap.default_setter, ) -> None: """Uses the configured propagator to inject a Context into the carrier. diff --git a/opentelemetry-api/src/opentelemetry/propagators/composite.py b/opentelemetry-api/src/opentelemetry/propagators/composite.py index b06e385b588..77330d94103 100644 --- a/opentelemetry-api/src/opentelemetry/propagators/composite.py +++ b/opentelemetry-api/src/opentelemetry/propagators/composite.py @@ -39,7 +39,7 @@ def extract( self, carrier: textmap.CarrierT, context: typing.Optional[Context] = None, - getter: textmap.Getter = textmap.default_getter, + getter: textmap.Getter[textmap.CarrierT] = textmap.default_getter, ) -> Context: """Run each of the configured propagators with the given context and carrier. Propagators are run in the order they are configured, if multiple @@ -56,7 +56,7 @@ def inject( self, carrier: textmap.CarrierT, context: typing.Optional[Context] = None, - setter: textmap.Setter = textmap.default_setter, + setter: textmap.Setter[textmap.CarrierT] = textmap.default_setter, ) -> None: """Run each of the configured propagators with the given context and carrier. Propagators are run in the order they are configured, if multiple diff --git a/opentelemetry-api/src/opentelemetry/propagators/textmap.py b/opentelemetry-api/src/opentelemetry/propagators/textmap.py index 0011315cf21..afadd35000b 100644 --- a/opentelemetry-api/src/opentelemetry/propagators/textmap.py +++ b/opentelemetry-api/src/opentelemetry/propagators/textmap.py @@ -21,7 +21,7 @@ CarrierValT = typing.Union[typing.List[str], str] -class Getter(abc.ABC): +class Getter(abc.ABC, typing.Generic[CarrierT]): """This class implements a Getter that enables extracting propagated fields from a carrier. """ @@ -54,7 +54,7 @@ def keys(self, carrier: CarrierT) -> typing.List[str]: """ -class Setter(abc.ABC): +class Setter(abc.ABC, typing.Generic[CarrierT]): """This class implements a Setter that enables injecting propagated fields into a carrier. """ @@ -71,8 +71,8 @@ def set(self, carrier: CarrierT, key: str, value: str) -> None: """ -class DefaultGetter(Getter): - def get( # type: ignore +class DefaultGetter(Getter[typing.Mapping[str, CarrierValT]]): + def get( self, carrier: typing.Mapping[str, CarrierValT], key: str ) -> typing.Optional[typing.List[str]]: """Getter implementation to retrieve a value from a dictionary. @@ -90,18 +90,18 @@ def get( # type: ignore return list(val) return [val] - def keys( # type: ignore - self, carrier: typing.Dict[str, CarrierValT] + def keys( + self, carrier: typing.Mapping[str, CarrierValT] ) -> typing.List[str]: """Keys implementation that returns all keys from a dictionary.""" return list(carrier.keys()) -default_getter = DefaultGetter() +default_getter: Getter[CarrierT] = DefaultGetter() # type: ignore -class DefaultSetter(Setter): - def set( # type: ignore +class DefaultSetter(Setter[typing.MutableMapping[str, CarrierValT]]): + def set( self, carrier: typing.MutableMapping[str, CarrierValT], key: str, @@ -117,7 +117,7 @@ def set( # type: ignore carrier[key] = value -default_setter = DefaultSetter() +default_setter: Setter[CarrierT] = DefaultSetter() # type: ignore class TextMapPropagator(abc.ABC): @@ -134,7 +134,7 @@ def extract( self, carrier: CarrierT, context: typing.Optional[Context] = None, - getter: Getter = default_getter, + getter: Getter[CarrierT] = default_getter, ) -> Context: """Create a Context from values in the carrier. @@ -162,7 +162,7 @@ def inject( self, carrier: CarrierT, context: typing.Optional[Context] = None, - setter: Setter = default_setter, + setter: Setter[CarrierT] = default_setter, ) -> None: """Inject values from a Context into a carrier. diff --git a/opentelemetry-api/src/opentelemetry/trace/propagation/tracecontext.py b/opentelemetry-api/src/opentelemetry/trace/propagation/tracecontext.py index 82cc078efcd..af16a08f0be 100644 --- a/opentelemetry-api/src/opentelemetry/trace/propagation/tracecontext.py +++ b/opentelemetry-api/src/opentelemetry/trace/propagation/tracecontext.py @@ -37,7 +37,7 @@ def extract( self, carrier: textmap.CarrierT, context: typing.Optional[Context] = None, - getter: textmap.Getter = textmap.default_getter, + getter: textmap.Getter[textmap.CarrierT] = textmap.default_getter, ) -> Context: """Extracts SpanContext from the carrier. @@ -90,7 +90,7 @@ def inject( self, carrier: textmap.CarrierT, context: typing.Optional[Context] = None, - setter: textmap.Setter = textmap.default_setter, + setter: textmap.Setter[textmap.CarrierT] = textmap.default_setter, ) -> None: """Injects SpanContext into the carrier.