Skip to content

Semantic tokens doesn't classify properties with return annotations as properties #3066

@MichaReiser

Description

@MichaReiser

Summary

class Foo:
    @property
    def prop(self) -> int:
        return 4


foo = Foo()
w = foo.prop                                  # prop should be variable on an instance

Semantic tokens classify foo.prop as a variable instead of a property. The issue is that the classification is based on attribute_expression.inferred_type(...) but this returns a properties return type instead of the property (unless the property isn't annotated?).

I think we want to take an approach closer to basedpyright where we look at the declaration instead. This should also allow us to determine if the property is readonly or not.

https://github.com/DetachHead/basedpyright/blob/d9e115df6c3b0b6bac366dc12703756096cbf2d7/packages/pyright-internal/src/analyzer/semanticTokensWalker.ts#L586-L591

This came up in astral-sh/ruff#23841 (comment)

Version

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingserverRelated to the LSP server

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions