-
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] T | object == object
#16088
Conversation
crates/red_knot_python_semantic/resources/mdtest/union_types.md
Outdated
Show resolved
Hide resolved
Type::Never => {} | ||
// Adding `object` to a union results in `object`. | ||
ty if ty.is_object(self.db) => { |
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.
Could also be
ty if ty.is_object(self.db) => { | |
Type::Instance(instance) if instance.class.is_object(self.db) => { |
but I somehow doubt that it would be much faster(?)
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 wondered the same thing; I think it could be unpacked even further to match against class.known
of the InstanceType
and eliminate the if
entirely? But in the absence of evidence that that is faster, it just looks more verbose.
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.
Looks great!
crates/red_knot_python_semantic/resources/mdtest/union_types.md
Outdated
Show resolved
Hide resolved
/// Collapse the union to a single type: `object`. | ||
fn collapse_to_object(mut self) -> Self { | ||
self.elements.clear(); | ||
self.elements.push(Type::object(self.db)); | ||
self | ||
} |
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 also thought about extending the UnionBuilder
to keep track of the fact that it represented object
. It could then short-circuit any .add(…)
into a no-op. Not sure if it's worth the extra complexity though, as we probably don't union with object
a lot in reality.
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.
This is the sort of thing IMO best left to testing with actual benchmarks once we ideally have a broader ecosystem check that we can also benchmark.
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.
Nice!
* main: add diagnostic `Span` (couples `File` and `TextRange`) (#16101) Remove `Hash` and `Eq` from `AstNodeRef` for types not implementing `Eq` or `Hash` (#16100) Fix release build warning about unused todo type message (#16102) [`pydocstyle`] Handle arguments with the same names as sections (`D417`) (#16011) [red-knot] Reduce usage of `From<Type>` implementations when working with `Symbol`s (#16076) Transition to salsa coarse-grained tracked structs (#15763) [`pyupgrade`] Handle micro version numbers correctly (`UP036`) (#16091) [red-knot] `T | object == object` (#16088) [`ruff`] Skip singleton starred expressions for `incorrectly-parenthesized-tuple-in-subscript` (`RUF031`) (#16083) Delete left-over `verbosity.rs (#16081) [red-knot] User-level configuration (#16021) Add `user_configuration_directory` to `System` (#16020)
Summary
object
toobject
.Type::object(db)
constructor to abbreviateKnownClass::Object.to_instance(db)
in some places.Type::is_object
andClass::is_object
function to make some tests for a bit easier to read.Happy to revert the latter two changes if these are not desirable.
closes #16084
Test Plan
New Markdown tests.