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
12 changes: 6 additions & 6 deletions conformance/third_party/conformance.exp
Original file line number Diff line number Diff line change
Expand Up @@ -13742,21 +13742,21 @@
{
"code": -2,
"column": 18,
"concise_description": "`ClassVar` is not allowed in this context",
"description": "`ClassVar` is not allowed in this context",
"concise_description": "`object` is not assignable to `TypeForm[Any]`",
"description": "`object` is not assignable to `TypeForm[Any]`",
"line": 71,
"name": "invalid-annotation",
"name": "bad-assignment",
"severity": "error",
"stop_column": 31,
"stop_line": 71
},
{
"code": -2,
"column": 18,
"concise_description": "`Final` is not allowed in this context",
"description": "`Final` is not allowed in this context",
"concise_description": "`object` is not assignable to `TypeForm[Any]`",
"description": "`object` is not assignable to `TypeForm[Any]`",
"line": 72,
"name": "invalid-annotation",
"name": "bad-assignment",
"severity": "error",
"stop_column": 28,
"stop_line": 72
Expand Down
33 changes: 26 additions & 7 deletions pyrefly/lib/alt/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -937,13 +937,32 @@ impl<'ctx, 'answer, Ans: LookupAnswer> AnswersSolver<'ctx, 'answer, Ans> {
// already been checked.
check_deprecation = false;
}
self.subscript_infer(
&base,
&x.slice,
x.range(),
type_form_context.unwrap_or(TypeFormContext::TypeExpression),
errors,
)
// A qualifier like `NotRequired[int]` is a legal runtime value, which
// typeshed types as `object`.
if type_form_context.is_none()
&& let Type::Type(inner) = base.ty()
&& matches!(
**inner,
Type::SpecialForm(
SpecialForm::ClassVar
| SpecialForm::Final
| SpecialForm::ReadOnly
| SpecialForm::Required
| SpecialForm::NotRequired
)
)
{
self.expr_infer(&x.slice, errors);
TypeInfo::of_ty(self.heap.mk_class_type(self.stdlib.object().clone()))
} else {
self.subscript_infer(
&base,
&x.slice,
x.range(),
type_form_context.unwrap_or(TypeFormContext::TypeExpression),
errors,
)
}
}
}
Expr::Named(x) => match &*x.target {
Expand Down
5 changes: 5 additions & 0 deletions pyrefly/lib/alt/solve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,11 @@ impl<'ctx, 'answer, Ans: LookupAnswer> AnswersSolver<'ctx, 'answer, Ans> {
ty: Some(self.expr_infer(&x.slice, errors)),
}
}
// Check subscripts as type forms so that misplaced qualifiers are reported.
Expr::Subscript(_) => Annotation::new_type(
self.expr_infer_impl(x, None, errors, Some(TypeFormContext::ClassKeyword))
.into_ty(),
),
_ => Annotation::new_type(self.expr_infer(x, errors)),
}
}
Expand Down
20 changes: 20 additions & 0 deletions pyrefly/lib/test/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2940,6 +2940,26 @@ Z = Annotated[0, 'not a type'] # E: Expected a type form, got instance of `Lite
"#,
);

// https://github.com/facebook/pyrefly/issues/5429
testcase!(
test_qualifier_subscript_as_runtime_value,
r#"
from typing import Any, ClassVar, Final, assert_type
from typing_extensions import NotRequired, ReadOnly, Required
def mark_optional(hints: dict[str, Any]) -> dict[str, Any]:
return {key: NotRequired[hint] for key, hint in hints.items()}
def f(hint: Any) -> None:
assert_type(NotRequired[hint], object)
assert_type(Required[int], object)
assert_type(ReadOnly[int], object)
assert_type(ClassVar[int], object)
assert_type(Final[int], object)
NotRequired[undefined] # E: Could not find name `undefined`
X = NotRequired[int]
def g(x: X) -> None: ... # E: Expected a type form, got instance of `object`
"#,
);

testcase!(
test_typing_annotated_alias,
r#"
Expand Down
Loading