basedpyright icon indicating copy to clipboard operation
basedpyright copied to clipboard

use the `instance`/`owner` type to report an error when a descriptor value is used in a class that it doesn't support

Open DetachHead opened this issue 1 year ago • 2 comments

from __future__ import annotations
from typing import final


class Foo:
    def __get__(self, instance: Bar | None, owner: type[Bar]) -> int: ...

@final
class Bar:
    asdf = Foo()

@final
class Baz:
    asdf = Foo() # should be an error

related: #181

DetachHead avatar Nov 15 '24 01:11 DetachHead

hmm you do get an error on the usage, so it's not as bad:

Bar.asdf  # no error
Baz.asdf  # error

DetachHead avatar Nov 15 '24 01:11 DetachHead

here's an example where you don't get an error on the usage

from typing import reveal_type


class Foo:
    def __set_name__(self, owner: type[str], value: str):
        reveal_type(owner())  # str in pyright, Bar at runtime


class Bar:
    asdf = Foo()

DetachHead avatar Apr 09 '25 02:04 DetachHead