'Understanding bound TypeVars with generic parameters
I'm trying to understand how bound variables work in TypeVars. I understand that any subclass of the bound class is allowed, but once I make the bound class a generic, things that I would expect to work don't:
from typing import Generic, TypeVar
class X:
pass
class Y(X):
pass
T = TypeVar("T", bound=X)
class A(Generic[T]):
def __init__(self, param: T):
self.param = param
class B(A[T]):
pass
S = TypeVar("S", bound=A[X])
def foo(bar: S) -> S:
return bar
foo(B(Y())) # Type "B[Y]" cannot be assigned to type "A[X]"
Could someone explain why this doesn't work, and if any workarounds are known?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
