basedpyright icon indicating copy to clipboard operation
basedpyright copied to clipboard

incorrect inlay hint on overridden attributes

Open DetachHead opened this issue 8 months ago • 2 comments

Description

Code sample in basedpyright playground

class Animal:
    pass

class Snake(Animal):
    pass

class Cat(Animal):
    pass

class Foo:
    A: Animal = Snake()

class Bar(Foo):
    A = Cat() # inlay hint says Cat

reveal_type(Bar().A) # actually Animal

related: #1247

DetachHead avatar May 18 '25 08:05 DetachHead

But why do you consider it a bug? At class level A is still Cat, and that's intended. Why should inlay hint show a type "as seen by foreign callers" and not "as seen in the nearest scope"?

class Animal:
    pass

class Snake(Animal):
    pass

class Cat(Animal):
    pass

class Foo:
    A: Animal = Snake()

class Bar(Foo):
    A = Cat() # inlay hint says Cat
    reveal_type(A)  # N: Type of "A" is "Cat"

reveal_type(Bar().A) # N: Type of "Bar().A" is "Animal"

https://basedpyright.com/?typeCheckingMode=all&reportUnannotatedClassAttribute=false&code=MYGwhgzhAECCB2BLAtmEAuAsAKGn6ADpBDjqMdAMrxgDWApgBQIpoCUWu%2BRUp25UaAGEwAF2ZJUIDjnyFifATABiAe1Wc5sdHElpoAXio0GjNovCCAQmABOjNaplc8sQ8LFnoAYmiJ44ACe0AAW-qLQEGCBMCKisvi29ABu9GgA%2BqKBBEywbHi%2BAHI6ACrZ9NCqAGbQAESwtX4wtXG1fEmpGVk5jDb2bAB0eT7QxdBlOZU1tX1mQ42IzSxSbdhAA

sterliakov avatar May 21 '25 00:05 sterliakov

i think it's an edge case that you want to access the attribute like that. 99% of the time it would be accessed from outside of the class (or inside the class using self.A/cls.A).

i guess the best solution here would just be to not show an inlay hint at all, because either way it can be considered misleading

DetachHead avatar May 21 '25 01:05 DetachHead