Skip to content

Commit bbfcf6e

Browse files
authored
[ty] __class_getitem__ is a classmethod (#20192)
## Summary `__class_getitem__` is [implicitly a classmethod](https://docs.python.org/3/reference/datamodel.html#object.__class_getitem__). ## Test Plan Added regression test.
1 parent 5518c84 commit bbfcf6e

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

crates/ty_python_semantic/resources/mdtest/subscript/class.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ class Identity:
1919
reveal_type(Identity[0]) # revealed: str
2020
```
2121

22+
`__class_getitem__` is implicitly a classmethod, so it can be called like this:
23+
24+
```py
25+
reveal_type(Identity.__class_getitem__(0)) # revealed: str
26+
```
27+
2228
## Class getitem union
2329

2430
```py

crates/ty_python_semantic/src/types/function.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,10 @@ impl<'db> FunctionType<'db> {
725725
/// classmethod.
726726
pub(crate) fn is_classmethod(self, db: &'db dyn Db) -> bool {
727727
self.has_known_decorator(db, FunctionDecorators::CLASSMETHOD)
728-
|| self.name(db) == "__init_subclass__"
728+
|| matches!(
729+
self.name(db).as_str(),
730+
"__init_subclass__" | "__class_getitem__"
731+
)
729732
}
730733

731734
/// If the implementation of this function is deprecated, returns the `@warnings.deprecated`.

crates/ty_python_semantic/src/types/infer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9203,7 +9203,7 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
92039203
}
92049204
}
92059205

9206-
match ty.try_call(db, &CallArguments::positional([value_ty, slice_ty])) {
9206+
match ty.try_call(db, &CallArguments::positional([slice_ty])) {
92079207
Ok(bindings) => return bindings.return_type(db),
92089208
Err(CallError(_, bindings)) => {
92099209
if let Some(builder) =

0 commit comments

Comments
 (0)