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
8 changes: 4 additions & 4 deletions conformance/third_party/conformance.exp
Original file line number Diff line number Diff line change
Expand Up @@ -9910,7 +9910,7 @@
"code": -2,
"column": 1,
"concise_description": "TODO: Expr::attr_infer_for_type attribute base undefined for type: TypeAlias[TA1, type[type[Any]]] (trying to access unknown)",
"description": "TODO: Expr::attr_infer_for_type attribute base undefined for type: TypeAlias[TA1, type[type[Any]]] (trying to access unknown)\n Sorry, Pyrefly encountered an internal error, this is always a bug in Pyrefly itself\n Please report the bug at https://fb.workplace.com/groups/pyreqa",
"description": "TODO: Expr::attr_infer_for_type attribute base undefined for type: TypeAlias[TA1, type[type[Any]]] (trying to access unknown)\n Sorry, Pyrefly encountered an internal error, this is always a bug in Pyrefly itself\n Please report the bug at https://github.com/facebook/pyrefly/issues/new",
"line": 143,
"name": "internal-error",
"severity": "error",
Expand All @@ -9921,7 +9921,7 @@
"code": -2,
"column": 1,
"concise_description": "TODO: Expr::attr_infer_for_type attribute base undefined for type: TypeAlias[TA2, type[type[Any]]] (trying to access unknown)",
"description": "TODO: Expr::attr_infer_for_type attribute base undefined for type: TypeAlias[TA2, type[type[Any]]] (trying to access unknown)\n Sorry, Pyrefly encountered an internal error, this is always a bug in Pyrefly itself\n Please report the bug at https://fb.workplace.com/groups/pyreqa",
"description": "TODO: Expr::attr_infer_for_type attribute base undefined for type: TypeAlias[TA2, type[type[Any]]] (trying to access unknown)\n Sorry, Pyrefly encountered an internal error, this is always a bug in Pyrefly itself\n Please report the bug at https://github.com/facebook/pyrefly/issues/new",
"line": 144,
"name": "internal-error",
"severity": "error",
Expand All @@ -9932,7 +9932,7 @@
"code": -2,
"column": 1,
"concise_description": "TODO: Expr::attr_infer_for_type attribute base undefined for type: TypeAlias[TA3, type[type[Any]]] (trying to access unknown)",
"description": "TODO: Expr::attr_infer_for_type attribute base undefined for type: TypeAlias[TA3, type[type[Any]]] (trying to access unknown)\n Sorry, Pyrefly encountered an internal error, this is always a bug in Pyrefly itself\n Please report the bug at https://fb.workplace.com/groups/pyreqa",
"description": "TODO: Expr::attr_infer_for_type attribute base undefined for type: TypeAlias[TA3, type[type[Any]]] (trying to access unknown)\n Sorry, Pyrefly encountered an internal error, this is always a bug in Pyrefly itself\n Please report the bug at https://github.com/facebook/pyrefly/issues/new",
"line": 145,
"name": "internal-error",
"severity": "error",
Expand All @@ -9943,7 +9943,7 @@
"code": -2,
"column": 1,
"concise_description": "TODO: Expr::attr_infer_for_type attribute base undefined for type: TypeAlias[TA4, type[type[Any]]] (trying to access unknown)",
"description": "TODO: Expr::attr_infer_for_type attribute base undefined for type: TypeAlias[TA4, type[type[Any]]] (trying to access unknown)\n Sorry, Pyrefly encountered an internal error, this is always a bug in Pyrefly itself\n Please report the bug at https://fb.workplace.com/groups/pyreqa",
"description": "TODO: Expr::attr_infer_for_type attribute base undefined for type: TypeAlias[TA4, type[type[Any]]] (trying to access unknown)\n Sorry, Pyrefly encountered an internal error, this is always a bug in Pyrefly itself\n Please report the bug at https://github.com/facebook/pyrefly/issues/new",
"line": 146,
"name": "internal-error",
"severity": "error",
Expand Down
24 changes: 19 additions & 5 deletions pyrefly/lib/alt/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2014,11 +2014,11 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
}
Type::ClassDef(cls) => {
let metadata = self.get_metadata_for_class(&cls);
let class_getitem_result = if self.get_class_tparams(&cls).is_empty()
let class_ty = Type::ClassDef(cls.dupe());
let allow_dunder_lookup = self.get_class_tparams(&cls).is_empty()
&& !metadata.has_base_any()
&& !metadata.is_new_type()
{
let class_ty = Type::ClassDef(cls.dupe());
&& !metadata.is_new_type();
let class_getitem_result = if allow_dunder_lookup {
// TODO(stroxler): Add a new API, similar to `type_of_attr_get` but returning a
// LookupResult or an Optional type, that we could use here to avoid the double lookup.
if self.has_attr(&class_ty, &dunder::CLASS_GETITEM) {
Expand All @@ -2039,7 +2039,21 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
} else {
None
};
if let Some(result) = class_getitem_result {
let metaclass_getitem_result =
if class_getitem_result.is_none() && allow_dunder_lookup {
self.call_magic_dunder_method(
&class_ty,
&dunder::GETITEM,
range,
&[CallArg::expr(slice)],
&[],
errors,
Some(&|| ErrorContext::Index(self.for_display(class_ty.clone()))),
)
} else {
None
};
if let Some(result) = class_getitem_result.or(metaclass_getitem_result) {
result
} else {
Type::type_form(self.specialize(
Expand Down
16 changes: 16 additions & 0 deletions pyrefly/lib/test/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1882,6 +1882,22 @@ assert_type(Foo[0], str)
"#,
);

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

class Meta(type):
def __getitem__(self, item: int) -> str:
return str(item)

class Foo(metaclass=Meta):
pass

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

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

testcase!(
Expand Down
Loading