diff --git a/crates/red_knot_python_semantic/resources/mdtest/literal/collections/dictionary.md b/crates/red_knot_python_semantic/resources/mdtest/literal/collections/dictionary.md new file mode 100644 index 0000000000000..dc4aa3197fb65 --- /dev/null +++ b/crates/red_knot_python_semantic/resources/mdtest/literal/collections/dictionary.md @@ -0,0 +1,8 @@ +# Dictionaries + +## Empty dictionary + +```py +x = {} +reveal_type(x) # revealed: dict +``` diff --git a/crates/red_knot_python_semantic/resources/mdtest/literal/collections/list.md b/crates/red_knot_python_semantic/resources/mdtest/literal/collections/list.md new file mode 100644 index 0000000000000..67b3e1f907b18 --- /dev/null +++ b/crates/red_knot_python_semantic/resources/mdtest/literal/collections/list.md @@ -0,0 +1,8 @@ +# Lists + +## Empty list + +```py +x = [] +reveal_type(x) # revealed: list +``` diff --git a/crates/red_knot_python_semantic/resources/mdtest/literal/collections/set.md b/crates/red_knot_python_semantic/resources/mdtest/literal/collections/set.md new file mode 100644 index 0000000000000..dcd1da384b57c --- /dev/null +++ b/crates/red_knot_python_semantic/resources/mdtest/literal/collections/set.md @@ -0,0 +1,8 @@ +# Sets + +## Basic set + +```py +x = {1, 2} +reveal_type(x) # revealed: set +``` diff --git a/crates/red_knot_python_semantic/resources/mdtest/literal/collections/tuple.md b/crates/red_knot_python_semantic/resources/mdtest/literal/collections/tuple.md new file mode 100644 index 0000000000000..159da51d8e852 --- /dev/null +++ b/crates/red_knot_python_semantic/resources/mdtest/literal/collections/tuple.md @@ -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]] +``` diff --git a/crates/red_knot_python_semantic/resources/mdtest/subscript/tuple.md b/crates/red_knot_python_semantic/resources/mdtest/subscript/tuple.md new file mode 100644 index 0000000000000..f837d989d2be6 --- /dev/null +++ b/crates/red_knot_python_semantic/resources/mdtest/subscript/tuple.md @@ -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 +``` diff --git a/crates/red_knot_python_semantic/src/types/infer.rs b/crates/red_knot_python_semantic/src/types/infer.rs index 8cd4ee21ede57..24d292af63067 100644 --- a/crates/red_knot_python_semantic/src/types/infer.rs +++ b/crates/red_knot_python_semantic/src/types/infer.rs @@ -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(); @@ -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(()) - } }