Skip to content

Mark textmap.Getter and textmap.Setter Generic #2656

@alanisaac

Description

@alanisaac

Is your feature request related to a problem?
Currently, if I inherit a custom textmap.Getter or textmap.Setter, I can't properly type hint it. For example, take this class:

from opentelemetry.propagators.textmap import Getter
from typing import Dict, Optional, List

class SimpleGetter(Getter):
    def get(
        self, carrier: Dict[str, List[str]], key: str
    ) -> Optional[List[str]]:
        return carrier[key]

When checked with mypy==0.920:

main.py:6: error: Argument 1 of "get" is incompatible with supertype "Getter"; supertype defines the argument type as "CarrierT"
main.py:6: note: This violates the Liskov substitution principle
main.py:6: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides

If I try to type hint carrier with the CarrierT TypeVar:

main.py:9: error: Value of type "CarrierT" is not indexable
Found 1 error in 1 file (checked 1 source file)

Describe the solution you'd like
Could Getter and Setter should be marked / inherit from Generic[CarrierT], so the specific TCarrier type can be defined? Happy to contribute a PR.

Describe alternatives you've considered
N/A

Additional context

Note: I see from #1690 that this could be somewhat more complicated. Specifically #1690 (comment)

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions