Skip to content
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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

InSyncWithFoo
Copy link
Contributor

Summary

See #15103.

Test Plan

Markdown tests and unit tests.

Copy link
Contributor

github-actions bot commented Dec 30, 2024

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@InSyncWithFoo
Copy link
Contributor Author

This is largely a proof-of-concept. is_equals_to() is not a thing in the specification, and I'm quite sure its name is not the best.

@MichaReiser MichaReiser added the red-knot Multi-file analysis & type inference label Dec 30, 2024
Copy link
Contributor

@sharkdp sharkdp left a 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.

Comment on lines 1415 to 1435
/// 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 {
Copy link
Contributor

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?

Copy link
Member

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 😆

Copy link
Contributor

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.

Copy link
Contributor Author

@InSyncWithFoo InSyncWithFoo Jan 3, 2025

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
red-knot Multi-file analysis & type inference
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants