Skip to content

Commit bc8efa2

Browse files
authored
[ty] Classify cls as class parameter (#21944)
1 parent 4249736 commit bc8efa2

File tree

2 files changed

+32
-20
lines changed

2 files changed

+32
-20
lines changed

crates/ty_ide/src/semantic_tokens.rs

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -302,17 +302,25 @@ impl<'db> SemanticTokenVisitor<'db> {
302302
let parsed = parsed_module(db, definition.file(db));
303303
let ty = parameter.node(&parsed.load(db)).inferred_type(&model);
304304

305-
if let Some(ty) = ty
306-
&& let Type::TypeVar(type_var) = ty
307-
{
308-
match type_var.typevar(db).kind(db) {
309-
TypeVarKind::TypingSelf => {
310-
return Some((SemanticTokenType::SelfParameter, modifiers));
305+
if let Some(ty) = ty {
306+
let type_var = match ty {
307+
Type::TypeVar(type_var) => Some((type_var, false)),
308+
Type::SubclassOf(subclass_of) => {
309+
subclass_of.into_type_var().map(|var| (var, true))
311310
}
312-
TypeVarKind::Legacy
313-
| TypeVarKind::ParamSpec
314-
| TypeVarKind::Pep695ParamSpec
315-
| TypeVarKind::Pep695 => {}
311+
_ => None,
312+
};
313+
314+
if let Some((type_var, is_cls)) = type_var
315+
&& matches!(type_var.typevar(db).kind(db), TypeVarKind::TypingSelf)
316+
{
317+
let kind = if is_cls {
318+
SemanticTokenType::ClsParameter
319+
} else {
320+
SemanticTokenType::SelfParameter
321+
};
322+
323+
return Some((kind, modifiers));
316324
}
317325
}
318326

@@ -1203,7 +1211,7 @@ class MyClass:
12031211
"
12041212
class MyClass:
12051213
@classmethod
1206-
def method(cls, x): pass
1214+
def method(cls, x): print(cls)
12071215
",
12081216
);
12091217

@@ -1215,6 +1223,8 @@ class MyClass:
12151223
"method" @ 41..47: Method [definition]
12161224
"cls" @ 48..51: ClsParameter [definition]
12171225
"x" @ 53..54: Parameter [definition]
1226+
"print" @ 57..62: Function
1227+
"cls" @ 63..66: ClsParameter
12181228
"#);
12191229
}
12201230

@@ -1246,7 +1256,7 @@ class MyClass:
12461256
class MyClass:
12471257
def method(instance, x): pass
12481258
@classmethod
1249-
def other(klass, y): pass
1259+
def other(klass, y): print(klass)
12501260
def complex_method(instance, posonly, /, regular, *args, kwonly, **kwargs): pass
12511261
",
12521262
);
@@ -1262,13 +1272,15 @@ class MyClass:
12621272
"other" @ 75..80: Method [definition]
12631273
"klass" @ 81..86: ClsParameter [definition]
12641274
"y" @ 88..89: Parameter [definition]
1265-
"complex_method" @ 105..119: Method [definition]
1266-
"instance" @ 120..128: SelfParameter [definition]
1267-
"posonly" @ 130..137: Parameter [definition]
1268-
"regular" @ 142..149: Parameter [definition]
1269-
"args" @ 152..156: Parameter [definition]
1270-
"kwonly" @ 158..164: Parameter [definition]
1271-
"kwargs" @ 168..174: Parameter [definition]
1275+
"print" @ 92..97: Function
1276+
"klass" @ 98..103: ClsParameter
1277+
"complex_method" @ 113..127: Method [definition]
1278+
"instance" @ 128..136: SelfParameter [definition]
1279+
"posonly" @ 138..145: Parameter [definition]
1280+
"regular" @ 150..157: Parameter [definition]
1281+
"args" @ 160..164: Parameter [definition]
1282+
"kwonly" @ 166..172: Parameter [definition]
1283+
"kwargs" @ 176..182: Parameter [definition]
12721284
"#);
12731285
}
12741286

crates/ty_python_semantic/src/types/subclass_of.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ impl<'db> SubclassOfType<'db> {
119119
subclass_of.is_type_var()
120120
}
121121

122-
pub(crate) const fn into_type_var(self) -> Option<BoundTypeVarInstance<'db>> {
122+
pub const fn into_type_var(self) -> Option<BoundTypeVarInstance<'db>> {
123123
self.subclass_of.into_type_var()
124124
}
125125

0 commit comments

Comments
 (0)