-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[red-knot] Support assert_type
#15194
base: main
Are you sure you want to change the base?
Conversation
|
This is largely a proof-of-concept. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you very much for working on this. I added a few initial review comments.
/// Returns true if this type and `other` are "exactly the same". | ||
/// | ||
/// This powers the `assert_type()` directive. | ||
pub(crate) fn is_equals_to(self, db: &'db dyn Db, other: Type<'db>) -> bool { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you go into a bit more detail in how this is different from is_equivalent_to
? The main difference seems to be that this can also handle non-fully-static types and return true
for something like Any ~ Any
? If so, could it handle those gradual types (+ intersections/unions/tuples) and then fall back to is_equivalent_to
for fully static types?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's quite possible that a function like this could help fix #14899, FWIW, so it doesn't seem like an unreasonable addition to me. I'm also not sure what name would clearly distinguish it from Type::is_equivalent_to()
, though 😆
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be clear, I was not suggesting that this function is not useful. I just think there's some overlap with is_equivalent_to
, and I'd like to understand it and possibly make use of it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another notable exception is type[Any] != type != type[object]
. I'm not too sure about the former, but I think it is better to be strict.
Also, as it currently is, int | Unknown | Any != int | Any
. This is supposedly due to Unknown
not being foldable into Any
, but int | Any
is just Any
; could such union types even be emitted in the first place?
crates/red_knot_python_semantic/resources/mdtest/directives/assert_type.md
Show resolved
Hide resolved
crates/red_knot_python_semantic/resources/mdtest/directives/assert_type.md
Outdated
Show resolved
Hide resolved
crates/red_knot_python_semantic/resources/mdtest/directives/assert_type.md
Show resolved
Hide resolved
crates/red_knot_python_semantic/resources/mdtest/directives/assert_type.md
Show resolved
Hide resolved
crates/red_knot_python_semantic/resources/mdtest/directives/assert_type.md
Show resolved
Hide resolved
3234f80
to
08fd1a5
Compare
Summary
See #15103.
Test Plan
Markdown tests and unit tests.