Skip to content

Commit

Permalink
[red-knot] Added collections infer tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Lexxxzy committed Oct 15, 2024
1 parent ec0be47 commit 1b657b2
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 131 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Dictionaries

## Empty dictionary

```py
x = {}
reveal_type(x) # revealed: dict
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Lists

## Empty list

```py
x = []
reveal_type(x) # revealed: list
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Sets

## Basic set

```py
x = {1, 2}
reveal_type(x) # revealed: set
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Tuples

## Empty tuple

```py
x = ()
reveal_type(x) # revealed: tuple[()]
```

## Heterogeneous tuple

```py
x = (1, 'a')
y = (1, (2, 3))
z = (x, 2)

reveal_type(x) # revealed: tuple[Literal[1], Literal["a"]]
reveal_type(y) # revealed: tuple[Literal[1], tuple[Literal[2], Literal[3]]]
reveal_type(z) # revealed: tuple[tuple[Literal[1], Literal["a"]], Literal[2]]
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Tuple subscripts

## Basic

```py
t = (1, 'a', 'b')

a = t[0]
b = t[1]
c = t[-1]
d = t[-2]
e = t[4] # error: [index-out-of-bounds]
f = t[-4] # error: [index-out-of-bounds]

reveal_type(a) # revealed: Literal[1]
reveal_type(b) # revealed: Literal["a"]
reveal_type(c) # revealed: Literal["b"]
reveal_type(d) # revealed: Literal["a"]
reveal_type(e) # revealed: Unknown
reveal_type(f) # revealed: Unknown
```
131 changes: 0 additions & 131 deletions crates/red_knot_python_semantic/src/types/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3909,103 +3909,6 @@ mod tests {
Ok(())
}

#[test]
fn empty_tuple_literal() -> anyhow::Result<()> {
let mut db = setup_db();

db.write_dedented(
"/src/a.py",
"
x = ()
",
)?;

assert_public_ty(&db, "/src/a.py", "x", "tuple[()]");

Ok(())
}

#[test]
fn tuple_heterogeneous_literal() -> anyhow::Result<()> {
let mut db = setup_db();

db.write_dedented(
"/src/a.py",
"
x = (1, 'a')
y = (1, (2, 3))
z = (x, 2)
",
)?;

assert_public_ty(&db, "/src/a.py", "x", r#"tuple[Literal[1], Literal["a"]]"#);
assert_public_ty(
&db,
"/src/a.py",
"y",
"tuple[Literal[1], tuple[Literal[2], Literal[3]]]",
);
assert_public_ty(
&db,
"/src/a.py",
"z",
r#"tuple[tuple[Literal[1], Literal["a"]], Literal[2]]"#,
);

Ok(())
}

#[test]
fn list_literal() -> anyhow::Result<()> {
let mut db = setup_db();

db.write_dedented(
"/src/a.py",
"
x = []
",
)?;

// TODO should be a generic type
assert_public_ty(&db, "/src/a.py", "x", "list");

Ok(())
}

#[test]
fn set_literal() -> anyhow::Result<()> {
let mut db = setup_db();

db.write_dedented(
"/src/a.py",
"
x = {1, 2}
",
)?;

// TODO should be a generic type
assert_public_ty(&db, "/src/a.py", "x", "set");

Ok(())
}

#[test]
fn dict_literal() -> anyhow::Result<()> {
let mut db = setup_db();

db.write_dedented(
"/src/a.py",
"
x = {}
",
)?;

// TODO should be a generic type
assert_public_ty(&db, "/src/a.py", "x", "dict");

Ok(())
}

#[test]
fn nonlocal_name_reference() -> anyhow::Result<()> {
let mut db = setup_db();
Expand Down Expand Up @@ -4579,38 +4482,4 @@ mod tests {
);
Ok(())
}

#[test]
fn subscript_tuple() -> anyhow::Result<()> {
let mut db = setup_db();

db.write_dedented(
"/src/a.py",
"
t = (1, 'a', 'b')
a = t[0]
b = t[1]
c = t[-1]
d = t[-2]
e = t[4]
f = t[-4]
",
)?;

assert_public_ty(&db, "/src/a.py", "a", "Literal[1]");
assert_public_ty(&db, "/src/a.py", "b", "Literal[\"a\"]");
assert_public_ty(&db, "/src/a.py", "c", "Literal[\"b\"]");
assert_public_ty(&db, "/src/a.py", "d", "Literal[\"a\"]");
assert_public_ty(&db, "/src/a.py", "e", "Unknown");
assert_public_ty(&db, "/src/a.py", "f", "Unknown");

assert_file_diagnostics(
&db,
"src/a.py",
&["Index 4 is out of bounds for tuple of type `tuple[Literal[1], Literal[\"a\"], Literal[\"b\"]]` with length 3", "Index -4 is out of bounds for tuple of type `tuple[Literal[1], Literal[\"a\"], Literal[\"b\"]]` with length 3"],
);

Ok(())
}
}

0 comments on commit 1b657b2

Please sign in to comment.