basedmypy icon indicating copy to clipboard operation
basedmypy copied to clipboard

an `UnsafeVariance` modifier

Open KotlinIsland opened this issue 3 years ago • 0 comments

class A(Generic[T_co]):
    def foo(self, t: T_co):
        print(t)

This code is 100% type safe, the usage of t is safe. It would be nice if there was a form of generic where safe usages can be guaranteed, a stop gap would be to introduce an UnsafeVariance modifier that just allows these usages.

    def foo(self, t: UnsafeVariance[T_co]):

egg

class A[out T]:
    def foo(self, t: UnsafeVariance[T]):
        reveal_type(t)  # object
a = A[int]
a.foo("")  # error, expected int, found str

Related: #62

KotlinIsland avatar Feb 24 '22 01:02 KotlinIsland