basedpyright
basedpyright copied to clipboard
generic on private/protected mutable attribute incorrectly treated as covariant instead of invariant
Description
Code sample in basedpyright playground
from __future__ import annotations
from typing import reveal_type
class Foo[T]:
def __init__(self, value: T) -> None:
self.__value: T = value
def foo(self):
a = Foo(1)
b: Foo[object] = a
self.sus(b)
reveal_type(a.__value) # bpr: int, runtime: str
def sus(self, foo: Foo[object]):
foo.__value = ""
Foo(1).foo()
related: #698
i disagree, we just need to not materialize private members
Or just ban accessing private members outside of the instance like I suggested in https://github.com/DetachHead/basedpyright/issues/698#issuecomment-3345263658, because the fact that you can even do this at all is really stupid