basedpyright icon indicating copy to clipboard operation
basedpyright copied to clipboard

inlay hint for split def attribute only includes the initial part of the definition

Open KotlinIsland opened this issue 5 months ago • 2 comments

Description

class A:
    a`: int` = 1

    def f(self):
        self.a = "a"

i would expect : int | str

KotlinIsland avatar Sep 24 '25 04:09 KotlinIsland

context for anyone wondering why there's no error when assigning a str to self.a, it's because a's type is actually inferred as int | str but it displays the wrong inlay hint.

Code sample in basedpyright playground

from typing import reveal_type


class A:
    a = 1

    def f(self):
        self.a = "a"

reveal_type(A().a) # int | str

DetachHead avatar Sep 24 '25 09:09 DetachHead

interestingly, the same can't be said about ClassVars

Code sample in basedpyright playground

from typing import ClassVar, reveal_type


class A:
    a: ClassVar = 1

    @classmethod
    def f(cls):
        cls.a = "a"


A.f()

reveal_type(A().a)  # basedpyright: int, runtime: str

#1542

DetachHead avatar Oct 02 '25 00:10 DetachHead