Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added generic to textmap getter and setter #2657

Merged
merged 8 commits into from
May 27, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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.

Expand Down
4 changes: 2 additions & 2 deletions opentelemetry-api/src/opentelemetry/propagate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
24 changes: 12 additions & 12 deletions opentelemetry-api/src/opentelemetry/propagators/textmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""
Expand Down Expand Up @@ -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.
"""
Expand All @@ -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.
Expand All @@ -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,
Expand All @@ -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):
Expand All @@ -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.

Expand Down Expand Up @@ -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.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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.

Expand Down