0

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) is B[int] as expected. Is there any way for reveal_type to instead tell me A[Sequence[int]]?

In this simple example this isn't useful, but in the case I am debugging knowing how the supertype was parametrized given a parametrized subtype without having to manually connect the dots (and potentially contradict what mypy infers) would clear up things a lot.

Mario Ishac
  • 5,060
  • 3
  • 21
  • 52

1 Answers1

3

You could write a helper:

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

reveal_type(as_a(b))

On the mypy playground, this reveals main.A[typing.Sequence*[builtins.int*]]. (Apparently the asterisks mark types inferred during type variable substitution.)

user2357112
  • 260,549
  • 28
  • 431
  • 505