basedmypy icon indicating copy to clipboard operation
basedmypy copied to clipboard

type variables should be allowed on `ClassVar`s

Open KotlinIsland opened this issue 1 year ago • 2 comments

class A[T]:
    t: ClassVar[T]
A[int].t = 1

this is obviously an error, but:

class B(A[int]):
    pass
B.t = 1

this should be allowed

KotlinIsland avatar Sep 01 '24 23:09 KotlinIsland

also this

class A[T]:
    class B[TB = T]: ...
class C(A[int]: ...

A.B() # error
A[int].B() # ok
C.B() # ok

KotlinIsland avatar Sep 25 '24 07:09 KotlinIsland

the issue is with classmethod:

class A:
    a: ClassVar[T]
    @classvar
    def f(cls, t: T):
        cls.a = t
A[int].f(1)

A[int].f is stupid

but this is often how constructors are written:

    @classmethod
    def make(cls, t: T) -> Self:
        return cls(t)

so the question is: how can we support both cases safely

KotlinIsland avatar Feb 18 '25 13:02 KotlinIsland