Reasons behind reportInvalidTypeVarUse #2111
-
I have the following code: from typing import Generic, Optional, TypeVar
T = TypeVar('T')
V = TypeVar('V')
class A(Generic[T]):
def __init__(self, a: int, b: Optional[T]):
self.a = a
self.b = b
@classmethod
def from_a(cls, other: "A[V]") -> "A[T]":
return A(other.a, None) It's intended use is as follows: a = A(1, 'a') # Type of "a" is "A[str]"
b = A[int].from_a(a) # Type of "b" is "A[int]" However, pyright complains about a type variable
I don't understand why there is such a restriction on the use of type variables. |
Beta Was this translation helpful? Give feedback.
Answered by
jakebailey
Jul 22, 2021
Replies: 1 comment 7 replies
-
The from typing import Generic, Optional, TypeVar
T = TypeVar('T')
class A(Generic[T]):
def __init__(self, a: int, b: Optional[T]):
self.a = a
self.b = b
@classmethod
def from_a(cls, other: "A[Any]") -> "A[T]":
return A(other.a, None) Because |
Beta Was this translation helpful? Give feedback.
7 replies
Answer selected by
ethframe
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The
V
is only in one place, so it doesn't actually do anything. Your code could be written as:Because
V
was never used to actually constrain any other parts offrom_a
.