From 08fd1a5fe264c37a61488ae871f01f0e25f33400 Mon Sep 17 00:00:00 2001 From: InSyncWithFoo Date: Fri, 3 Jan 2025 20:50:05 +0000 Subject: [PATCH] Per review --- .../mdtest/directives/assert_type.md | 2 -- crates/red_knot_python_semantic/src/types.rs | 27 ++++++++++++++++++- .../src/types/diagnostic.rs | 2 +- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/crates/red_knot_python_semantic/resources/mdtest/directives/assert_type.md b/crates/red_knot_python_semantic/resources/mdtest/directives/assert_type.md index d996197b5d7a26..0939102c451784 100644 --- a/crates/red_knot_python_semantic/resources/mdtest/directives/assert_type.md +++ b/crates/red_knot_python_semantic/resources/mdtest/directives/assert_type.md @@ -1,5 +1,3 @@ -from typing import reveal_type - # `assert_type` ## Basic diff --git a/crates/red_knot_python_semantic/src/types.rs b/crates/red_knot_python_semantic/src/types.rs index 14ce18575ba400..3c2ac4be76f8e3 100644 --- a/crates/red_knot_python_semantic/src/types.rs +++ b/crates/red_knot_python_semantic/src/types.rs @@ -1408,6 +1408,29 @@ impl<'db> Type<'db> { /// Returns true if this type and `other` are "exactly the same". /// + /// Two types are "exactly the same" when: + /// + /// * Both are either `Any` or `Unknown`, or + /// * If they are not tuples, generics, unions or intersections: + /// * They share the same internal representation. + /// * If they are tuples: + /// * They have the same length, and + /// * Each element pair are "exactly the same". + /// * If they are generics: + /// * The "origins" are "exactly the same". + /// * The argument types are "exactly the same" when compared as tuples. + /// * If they are unions: + /// * They have the same length, and + /// * For each element of the first type, + /// there exist one element of the second + /// such that they are "exactly the same"; + /// each element must not be paired more than once. + /// * If they are intersections: + /// * The positive parts are "exactly the same" when compared as unions. + /// * The negative parts are "exactly the same" when compared as unions. + /// + /// Note: `Todo != Todo`. + /// /// This powers the `assert_type()` directive. pub(crate) fn is_equals_to(self, db: &'db dyn Db, other: Type<'db>) -> bool { let equal = |(first, second): (&Type<'db>, &Type<'db>)| first.is_equals_to(db, *second); @@ -3194,7 +3217,9 @@ impl KnownFunction { pub fn constraint_function(self) -> Option { match self { Self::ConstraintFunction(f) => Some(f), - Self::RevealType | Self::Len | Self::Final | Self::NoTypeCheck | Self::AssertType => None, + Self::RevealType | Self::Len | Self::Final | Self::NoTypeCheck | Self::AssertType => { + None + } } } diff --git a/crates/red_knot_python_semantic/src/types/diagnostic.rs b/crates/red_knot_python_semantic/src/types/diagnostic.rs index f80d6c2269344e..9c67d0193fb52e 100644 --- a/crates/red_knot_python_semantic/src/types/diagnostic.rs +++ b/crates/red_knot_python_semantic/src/types/diagnostic.rs @@ -498,7 +498,7 @@ declare_lint! { pub(crate) static TYPE_ASSERTION_FAILURE = { summary: "detects failed type assertions", status: LintStatus::preview("1.0.0"), - default_level: Level::Warn, + default_level: Level::Error, } }