如何强制 mypy 的 reveal_type 显示超级类型?

How to force mypy's reveal_type to reveal super type?

Given:

from typing import TypeVar, Generic, Sequence

T = TypeVar("T")

class A(Generic[T]):
    pass

class B(A[Sequence[T]], Generic[T]):
    pass

b: B[int] = B()

reveal_type(b) 是预期的 B[int]reveal_type 有没有办法告诉我 A[Sequence[int]]?

在这个简单的例子中,这没有用,但在这种情况下,我正在调试知道如何在给定参数化子类型的情况下对超类型进行参数化,而无需手动连接点(并且可能与 mypy 推断的内容相矛盾) 会清理很多东西。

你可以写一个助手:

def as_a(a: A[T]) -> A[T]:
    return a

reveal_type(as_a(b))

在 mypy 操场上,这揭示了 main.A[typing.Sequence*[builtins.int*]]。 ( 星号标记在类型变量替换期间推断的类型。)