小编RAS*_*K A的帖子

Python 类型提示:Callable 后跟 TypeVar 是什么意思?

我试图理解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:

Run Code Online (Sandbox Code Playgroud)

非常感谢您的帮助,因为我一直为此而伤透脑筋。

原始源代码

原始源代码来自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, …
Run Code Online (Sandbox Code Playgroud)

python generics type-hinting python-typing

5
推荐指数
1
解决办法
9720
查看次数

标签 统计

generics ×1

python ×1

python-typing ×1

type-hinting ×1