basedmypy icon indicating copy to clipboard operation
basedmypy copied to clipboard

Make a `SafeVariance` annotation modifier

Open KotlinIsland opened this issue 4 years ago • 1 comments

class Base(Generic[T_co]):
    def foo(t: SafeVariance[T_co]):
        reveal_type(t) # SafeVariance[T_co] effective 'object'

class InvariantDerived(Base[T_co]):
    def foo(t: Invariant[T_co]): ...
        reveal_type(t) # Invariant[T_co]

class CovariantDerived(Base[T_co]):
    def foo(t: SafeVariance[T_co]): ...

b1: Base[object] = InvariantDerived[int]  # error
b2: Base[object] = CovariantDerived[int]  # no error

This will be very helpful in all sorts of scenarios, such as Mappings key type

KotlinIsland avatar Nov 05 '21 00:11 KotlinIsland

I think SafeVariance would be really good for tracking safe usage of variance (#224), so what if MaybeVariance instead?

On the other hand, maybe these are the same issue 🤔.

KotlinIsland avatar Mar 25 '22 00:03 KotlinIsland