Conversation
crates/red_knot_python_semantic/resources/mdtest/union_types.md
Outdated
Show resolved
Hide resolved
| // Adding `Never` to a union is a no-op. | ||
| Type::Never => {} | ||
| // Adding `object` to a union results in `object`. | ||
| ty if ty.is_object(self.db) => { |
There was a problem hiding this comment.
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.
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.
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.
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.
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.
* 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
objecttoobject.Type::object(db)constructor to abbreviateKnownClass::Object.to_instance(db)in some places.Type::is_objectandClass::is_objectfunction 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.