basedpyright icon indicating copy to clipboard operation
basedpyright copied to clipboard

generic on private/protected mutable attribute incorrectly treated as covariant instead of invariant

Open DetachHead opened this issue 5 months ago • 2 comments

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

DetachHead avatar Sep 29 '25 06:09 DetachHead

i disagree, we just need to not materialize private members

KotlinIsland avatar Sep 29 '25 14:09 KotlinIsland

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

DetachHead avatar Sep 29 '25 21:09 DetachHead