Skip to content
Closed
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
1 change: 1 addition & 0 deletions crates/pyrefly_python/src/dunder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub const GET: Name = Name::new_static("__get__");
pub const GETATTR: Name = Name::new_static("__getattr__");
pub const GETATTRIBUTE: Name = Name::new_static("__getattribute__");
pub const GETITEM: Name = Name::new_static("__getitem__");
pub const CLASS_GETITEM: Name = Name::new_static("__class_getitem__");
pub const GT: Name = Name::new_static("__gt__");
pub const HASH: Name = Name::new_static("__hash__");
pub const INIT: Name = Name::new_static("__init__");
Expand Down
42 changes: 36 additions & 6 deletions pyrefly/lib/alt/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1855,12 +1855,42 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
)
}
}
Type::ClassDef(cls) => Type::type_form(self.specialize(
&cls,
xs.map(|x| self.expr_untype(x, TypeFormContext::TypeArgument, errors)),
range,
errors,
)),
Type::ClassDef(cls) => {
let metadata = self.get_metadata_for_class(&cls);
let class_getitem_result = if self.get_class_tparams(&cls).is_empty()
&& !metadata.has_base_any()
&& !metadata.is_new_type()
{
let class_ty = Type::ClassDef(cls.dupe());
if self.has_attr(&class_ty, &dunder::CLASS_GETITEM) {
let cls_value = self.promote_silently(&cls);
let call_args = [CallArg::ty(&cls_value, range), CallArg::expr(slice)];
Some(self.call_method_or_error(
&class_ty,
&dunder::CLASS_GETITEM,
range,
&call_args,
&[],
errors,
Some(&|| ErrorContext::Index(self.for_display(class_ty.clone()))),
))
} else {
None
}
} else {
None
};
if let Some(result) = class_getitem_result {
result
} else {
Type::type_form(self.specialize(
&cls,
xs.map(|x| self.expr_untype(x, TypeFormContext::TypeArgument, errors)),
range,
errors,
))
}
}
Type::Type(box Type::SpecialForm(special)) => {
self.apply_special_form(special, slice, range, errors)
}
Expand Down
13 changes: 13 additions & 0 deletions pyrefly/lib/test/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1737,6 +1737,19 @@ def f(condition: bool):
"#,
);

testcase!(
test_class_getitem_magic_dunder,
r#"
from typing import assert_type

class Foo:
def __class_getitem__(cls, item: int) -> str:
return str(item)

assert_type(Foo[0], str)
"#,
);

testcase!(test_panic_docstring, "\"\"\" F\n\u{85}\"\"\"",);

testcase!(
Expand Down