DetachHead / basedpyright

pyright fork with various type checking improvements, improved vscode support and pylance features built into the language server
http://docs.basedpyright.com/
Other
613 stars 13 forks source link

a way to use covariant generics in an input position in a safe way by converting them to `object` #413

Open DetachHead opened 3 weeks ago

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

use case:

class ImmutableList[out T]:
    data: list[object]
    def __contains__(self, it: UnsafeVariance[T]) -> bool:
        return it in self.data # pretty much the only thing you're allowed to do with it

this would allow us to create a better Mapping type with a covariant key type: #268