我正在尝试理解以下代码中的类型提示Getter[T]
:
简化示例
T = TypeVar('T')
Getter = Callable[[T, str], str]
class AbstractClass(abc.ABC):
@abc.abstractmethod
def extract(
self,
get_from_carrier: Getter[T], # <---- See here
...
) -> Context:
非常感谢帮助,因为我已经在这上面费了很大的脑力。
原始源代码
原始源代码来自于OpenTelemetry项目文件"textmap.py":
import abc
import typing
from opentelemetry.context.context import Context
TextMapPropagatorT = typing.TypeVar("TextMapPropagatorT")
Setter = typing.Callable[[TextMapPropagatorT, str, str], None]
Getter = typing.Callable[[TextMapPropagatorT, str], typing.List[str]]
class TextMapPropagator(abc.ABC):
"""This class provides an interface that enables extracting and injecting
context into headers of HTTP requests.
...
"""
@abc.abstractmethod
def extract(
self,
get_from_carrier: Getter[TextMapPropagatorT],
carrier: TextMapPropagatorT,
context: typing.Optional[Context] = None,
) -> Context:
get_from_elem
的第二个参数具有get
函数期望的第一个参数类型。 - chepnerget_from_elem(get: C_, elem: str)
并不意味着get
和elem
之间有任何联系。 - chepnerget_from_elem(get: _C, elem: _T)
。_C
的额外下标似乎试图复制已经在_C
中的信息。 - Carcigenicatemypy
是否会对这两个进行区分,或者其中任何一个是否符合我的预期意图。 - chepner