Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion pyrefly/lib/state/semantic_tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use pyrefly_python::short_identifier::ShortIdentifier;
use pyrefly_python::symbol_kind::SymbolKind;
use pyrefly_python::sys_info::SysInfo;
use pyrefly_types::callable::FunctionKind;
use pyrefly_types::literal::Lit;
use pyrefly_types::types::Type;
use pyrefly_util::visit::Visit as _;
use ruff_python_ast::Arguments;
Expand Down Expand Up @@ -289,8 +290,10 @@ impl SemanticTokenBuilder {
x.recurse(&mut |x| self.process_expr(x, get_type_of_attribute, get_symbol_kind));
}
Expr::Attribute(attr) => {
// todo(samzhou19815): if the class's base is Enum, it should be ENUM_MEMBER
let kind = match get_type_of_attribute(attr.range()) {
Some(Type::Literal(lit)) if matches!(lit.value, Lit::Enum(_)) => {
SemanticTokenType::ENUM_MEMBER
}
Some(ty) if ty.is_toplevel_callable() => {
let is_method = ty.visit_toplevel_func_metadata(&|meta| {
matches!(&meta.kind, FunctionKind::Def(func) if func.cls.is_some())
Expand Down
38 changes: 38 additions & 0 deletions pyrefly/lib/test/lsp/semantic_tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,44 @@ token-type: property
);
}

#[test]
fn enum_member_attribute_test() {
let code = r#"
from enum import Enum

class Color(Enum):
RED = 1

Color.RED
"#;
assert_full_semantic_tokens(
&[("main", code)],
r#"
# main.py
line: 1, column: 5, length: 4, text: enum
token-type: namespace

line: 1, column: 17, length: 4, text: Enum
token-type: class

line: 3, column: 6, length: 5, text: Color
token-type: class

line: 3, column: 12, length: 4, text: Enum
token-type: class

line: 4, column: 4, length: 3, text: RED
token-type: variable, token-modifiers: [readonly]

line: 6, column: 0, length: 5, text: Color
token-type: class

line: 6, column: 6, length: 3, text: RED
token-type: enumMember
"#,
);
}

#[test]
fn type_alias_test() {
let code = r#"
Expand Down