Skip to content

Commit 771d2ba

Browse files
stepanchegfacebook-github-bot
authored andcommitted
Prohibit [X] syntax for runtime types
Reviewed By: cjhopman Differential Revision: D48933761 fbshipit-source-id: 6f4e5dc6498ea86f3d561884ce79d98f9d8da1df
1 parent 13fbb62 commit 771d2ba

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

starlark/src/values/typing/type_compiled/compiled.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ enum TypingError {
7373
InvalidTypeAnnotation(String),
7474
#[error("`{{A: B}}` cannot be used as type, perhaps you meant `dict[A, B]`")]
7575
Dict,
76+
#[error("`[X]` cannot be used as type, perhaps you meant `list[X]`")]
77+
List,
7678
/// The given type annotation does not exist, but the user might have forgotten quotes around
7779
/// it
7880
#[error(r#"Found `{0}` instead of a valid type annotation. Perhaps you meant `"{1}"`?"#)]
@@ -420,12 +422,8 @@ impl<'v> TypeCompiled<Value<'v>> {
420422
/// Parse `[t1, t2, ...]` as type.
421423
fn from_list(t: &ListRef<'v>, heap: &'v Heap) -> anyhow::Result<TypeCompiled<Value<'v>>> {
422424
match t.content() {
423-
[] => Err(TypingError::InvalidTypeAnnotation(t.to_string()).into()),
424-
[t] => {
425-
let t = TypeCompiled::new(*t, heap)?;
426-
Ok(TypeCompiled::type_list_of(t, heap))
427-
}
428-
ts => {
425+
[] | [_] => Err(TypingError::List.into()),
426+
ts @ [_, _, ..] => {
429427
// A union type, can match any
430428
let ts = ts.try_map(|t| TypeCompiled::new(*t, heap))?;
431429
Ok(TypeCompiled::type_any_of(ts, heap))
@@ -473,6 +471,8 @@ impl TypeCompiled<FrozenValue> {
473471
fn invalid_type_annotation<'v>(ty: Value<'v>, heap: &'v Heap) -> TypingError {
474472
if DictRef::from_value(ty).is_some() {
475473
TypingError::Dict
474+
} else if ListRef::from_value(ty).is_some() {
475+
TypingError::List
476476
} else if let Some(name) = ty
477477
.get_attr("type", heap)
478478
.ok()

starlark/src/values/typing/type_compiled/tests.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ isinstance(None, None)
103103
isinstance(assert_type, "function")
104104
isinstance([], list[int])
105105
isinstance([], list[typing.Any])
106-
isinstance([1, 2, 3], [int])
106+
isinstance([1, 2, 3], list[int])
107107
isinstance(None, [None, int])
108108
isinstance('test', int | str)
109109
isinstance(('test', None), (str, None))
@@ -118,13 +118,13 @@ not isinstance({"test": 1, 8: 2}, dict[str, int])
118118
not isinstance({"test": 1, "more": None}, dict[str, int])
119119
120120
isinstance(1, "")
121-
isinstance([1,2,"test"], ["_a"])
121+
isinstance([1,2,"test"], list["_a"])
122122
"#,
123123
);
124124

125125
// Checking types fails for invalid types
126126
a.fail("isinstance(None, isinstance)", "not a valid type");
127-
a.fail("isinstance(None, [])", "not a valid type");
127+
a.fail("isinstance(None, [])", "cannot be used as type");
128128
a.fail(
129129
"isinstance(None, {'1': '', '2': ''})",
130130
"cannot be used as type",
@@ -180,7 +180,7 @@ fn test_type_compiled_display() {
180180

181181
t("typing.Any", "\"\"");
182182
t("list[typing.Any]", "list");
183-
t("list[typing.Any]", "[\"\"]");
183+
t("list[typing.Any]", "list[\"\"]");
184184
t("None", "None");
185185
t("\"a\" | \"b\"", "[\"a\", \"b\"]");
186186
}

0 commit comments

Comments
 (0)