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] Improve Type::is_disjoint_from() for KnownInstanceTypes #15261

Merged
merged 3 commits into from
Jan 5, 2025

Conversation

AlexWaygood
Copy link
Member

Summary

Two things are fixed here:

  • If we have a known instance (e.g. KnownInstance::TypingLiteral) that was an instance of typing._SpecialForm, we currently don't recognise that this type is disjoint from a class Foo that is a subclass of typing._SpecialForm. This is a bit theoretical, as KnownInstances in our model are generally instances of classes that cannot be subclassed (at least, not easily). However, fixing this bug revealed (thanks to the property tests) that...
  • We don't currently consider Type::Tuple([]) to be disjoint from Type::KnownInstance(KnownInstanceType::TypingLiteral).

Test Plan

  • Added a new unit test that ensures that Type::Tuple([]) is now considered disjoint from Type::KnownInstance(KnownInstanceType::TypingLiteral).
  • Ran QUICKCHECK_TESTS=200000 cargo test -p red_knot_python_semantic -- --ignored types::property_tests::stable to ensure that there are no new failures in the property tests

@AlexWaygood AlexWaygood added the red-knot Multi-file analysis & type inference label Jan 4, 2025
Copy link
Contributor

github-actions bot commented Jan 4, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@AlexWaygood
Copy link
Member Author

Argh, no. This does create new property test failures. I must have had a fluky success locally.

@AlexWaygood AlexWaygood marked this pull request as draft January 4, 2025 18:46
@AlexWaygood AlexWaygood marked this pull request as ready for review January 4, 2025 18:59
@AlexWaygood
Copy link
Member Author

AlexWaygood commented Jan 4, 2025

Okay, I fixed the new bug surfaced by the property tests. This time it was that we weren't recognising Type::KnownInstance(KnownInstanceType::TypingLiteral) as being disjoint from Type::SubclassOf(<builtins.object>).

@AlexWaygood AlexWaygood force-pushed the alex/known-instance-disjointness branch 2 times, most recently from c44a2e7 to 8cad5be Compare January 5, 2025 15:23
@AlexWaygood AlexWaygood force-pushed the alex/known-instance-disjointness branch from 8cad5be to deac0e4 Compare January 5, 2025 21:54
Copy link
Contributor

@carljm carljm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

Comment on lines 1211 to 1216
| (Type::Tuple(_), Type::KnownInstance(known_instance)) => KnownClass::Tuple
.to_class_literal(db)
.into_class_literal()
.is_some_and(|ClassLiteralType { class: tuple_class }| {
!known_instance.class().is_subclass_of(db, tuple_class)
}),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be less repetitive to delegate this to "the known instance type is disjoint from KnownClass::Tuple.to_instance(), which would end up in the prior case?

Copy link
Member Author

@AlexWaygood AlexWaygood Jan 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh, great catch! I've pushed that change. (I checked that it doesn't cause any new property test failures, as well.)

@AlexWaygood AlexWaygood enabled auto-merge (squash) January 5, 2025 22:46
@AlexWaygood AlexWaygood merged commit 0743838 into main Jan 5, 2025
20 checks passed
@AlexWaygood AlexWaygood deleted the alex/known-instance-disjointness branch January 5, 2025 22:49
dcreager added a commit that referenced this pull request Jan 6, 2025
* main: (60 commits)
  [`ruff`] Dataclass enums (`RUF049`) (#15299)
  Better error message when `--config` is given a table key and a non-inline-table value (#15266)
  Update pre-commit dependencies (#15289)
  Don't fix in ecosystem check (#15267)
  Update Rust crate itertools to 0.14.0 (#15287)
  Remove accidental empty block at the bottom of `split-static-string (SIM905)` doc (#15290)
  Update Rust crate clearscreen to v4 (#15288)
  Update Rust crate insta to v1.42.0 (#15286)
  Update NPM Development dependencies (#15285)
  Update dependency uuid to v11.0.4 (#15284)
  Update dependency ruff to v0.8.6 (#15283)
  Update Rust crate syn to v2.0.95 (#15282)
  Update Rust crate matchit to v0.8.6 (#15281)
  Update Rust crate bstr to v1.11.3 (#15280)
  [red-knot] Future-proof `Type::is_disjoint_from()` (#15262)
  [red-knot] Improve `Type::is_disjoint_from()` for `KnownInstanceType`s (#15261)
  [red-knot] Minor simplifications and improvements to constraint narrowing logic (#15270)
  Allow assigning ellipsis literal as parameter default value (#14982)
  [red-knot] fix control flow for assignment expressions in elif tests (#15274)
  [`refurb`] Mark fix as unsafe when the right-hand side is a string (`FURB171`) (#15273)
  ...
dcreager added a commit that referenced this pull request Jan 6, 2025
* main: (29 commits)
  [`ruff`] Dataclass enums (`RUF049`) (#15299)
  Better error message when `--config` is given a table key and a non-inline-table value (#15266)
  Update pre-commit dependencies (#15289)
  Don't fix in ecosystem check (#15267)
  Update Rust crate itertools to 0.14.0 (#15287)
  Remove accidental empty block at the bottom of `split-static-string (SIM905)` doc (#15290)
  Update Rust crate clearscreen to v4 (#15288)
  Update Rust crate insta to v1.42.0 (#15286)
  Update NPM Development dependencies (#15285)
  Update dependency uuid to v11.0.4 (#15284)
  Update dependency ruff to v0.8.6 (#15283)
  Update Rust crate syn to v2.0.95 (#15282)
  Update Rust crate matchit to v0.8.6 (#15281)
  Update Rust crate bstr to v1.11.3 (#15280)
  [red-knot] Future-proof `Type::is_disjoint_from()` (#15262)
  [red-knot] Improve `Type::is_disjoint_from()` for `KnownInstanceType`s (#15261)
  [red-knot] Minor simplifications and improvements to constraint narrowing logic (#15270)
  Allow assigning ellipsis literal as parameter default value (#14982)
  [red-knot] fix control flow for assignment expressions in elif tests (#15274)
  [`refurb`] Mark fix as unsafe when the right-hand side is a string (`FURB171`) (#15273)
  ...
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.

2 participants