From aefb60740559abf5daf88ba3f79a0a5852f3563d Mon Sep 17 00:00:00 2001 From: InSync Date: Wed, 15 Jan 2025 01:57:23 +0700 Subject: [PATCH] [red-knot] Migrate `is_equivalent_to` unit tests to Markdown tests (#15470) ## Summary Part of #15397, built on top of #15469. ## Test Plan Markdown tests. --- .../type_properties/is_equivalent_to.md | 35 +++++++++++++++++++ crates/red_knot_python_semantic/src/types.rs | 27 -------------- 2 files changed, 35 insertions(+), 27 deletions(-) create mode 100644 crates/red_knot_python_semantic/resources/mdtest/type_properties/is_equivalent_to.md diff --git a/crates/red_knot_python_semantic/resources/mdtest/type_properties/is_equivalent_to.md b/crates/red_knot_python_semantic/resources/mdtest/type_properties/is_equivalent_to.md new file mode 100644 index 0000000000000..bb4d713303d78 --- /dev/null +++ b/crates/red_knot_python_semantic/resources/mdtest/type_properties/is_equivalent_to.md @@ -0,0 +1,35 @@ +# Equivalence relation + +`is_equivalent_to` implements [the equivalence relation] for fully static types. + +Two types `A` and `B` are equivalent iff `A` is a subtype of `B` and `B` is a subtype of `A`. + +## Basic + +```py +from typing import Any +from typing_extensions import Literal +from knot_extensions import Unknown, is_equivalent_to, static_assert + +static_assert(is_equivalent_to(Literal[1, 2], Literal[1, 2])) +static_assert(is_equivalent_to(type[object], type)) + +static_assert(not is_equivalent_to(Any, Any)) +static_assert(not is_equivalent_to(Unknown, Unknown)) +static_assert(not is_equivalent_to(Any, None)) +static_assert(not is_equivalent_to(Literal[1, 2], Literal[1, 0])) +static_assert(not is_equivalent_to(Literal[1, 2], Literal[1, 2, 3])) +``` + +## Equivalence is commutative + +```py +from typing_extensions import Literal +from knot_extensions import is_equivalent_to, static_assert + +static_assert(is_equivalent_to(type, type[object])) +static_assert(not is_equivalent_to(Literal[1, 0], Literal[1, 2])) +static_assert(not is_equivalent_to(Literal[1, 2, 3], Literal[1, 2])) +``` + +[the equivalence relation]: https://typing.readthedocs.io/en/latest/spec/glossary.html#term-equivalent diff --git a/crates/red_knot_python_semantic/src/types.rs b/crates/red_knot_python_semantic/src/types.rs index 6c08cbdcdbe74..aaf770b52f30b 100644 --- a/crates/red_knot_python_semantic/src/types.rs +++ b/crates/red_knot_python_semantic/src/types.rs @@ -4343,33 +4343,6 @@ pub(crate) mod tests { } } - #[test_case( - Ty::Union(vec![Ty::IntLiteral(1), Ty::IntLiteral(2)]), - Ty::Union(vec![Ty::IntLiteral(1), Ty::IntLiteral(2)]) - )] - #[test_case(Ty::SubclassOfBuiltinClass("object"), Ty::BuiltinInstance("type"))] - fn is_equivalent_to(from: Ty, to: Ty) { - let db = setup_db(); - let from = from.into_type(&db); - let to = to.into_type(&db); - assert!(from.is_equivalent_to(&db, to)); - assert!(to.is_equivalent_to(&db, from)); - } - - #[test_case(Ty::Any, Ty::Any)] - #[test_case(Ty::Any, Ty::None)] - #[test_case(Ty::Unknown, Ty::Unknown)] - #[test_case(Ty::Todo, Ty::Todo)] - #[test_case(Ty::Union(vec![Ty::IntLiteral(1), Ty::IntLiteral(2)]), Ty::Union(vec![Ty::IntLiteral(1), Ty::IntLiteral(0)]))] - #[test_case(Ty::Union(vec![Ty::IntLiteral(1), Ty::IntLiteral(2)]), Ty::Union(vec![Ty::IntLiteral(1), Ty::IntLiteral(2), Ty::IntLiteral(3)]))] - fn is_not_equivalent_to(from: Ty, to: Ty) { - let db = setup_db(); - let from = from.into_type(&db); - let to = to.into_type(&db); - assert!(!from.is_equivalent_to(&db, to)); - assert!(!to.is_equivalent_to(&db, from)); - } - #[test_case(Ty::Never, Ty::Never)] #[test_case(Ty::Never, Ty::None)] #[test_case(Ty::Never, Ty::BuiltinInstance("int"))]