Skip to content

Reasons behind reportInvalidTypeVarUse #2111

Answered by jakebailey
ethframe asked this question in Q&A
Discussion options

You must be logged in to vote

The V is only in one place, so it doesn't actually do anything. Your code could be written as:

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 V was never used to actually constrain any other parts of from_a.

Replies: 1 comment 7 replies

Comment options

You must be logged in to vote
7 replies
@erictraut
Comment options

@ethframe
Comment options

@erictraut
Comment options

@ethframe
Comment options

@erictraut
Comment options

Answer selected by ethframe
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants