Skip to content

Conversation

carljm
Copy link
Contributor

@carljm carljm commented Apr 3, 2025

For two non-disjoint types P and Q, the simplification of (P | Q) & ~Q is not P, but P & ~Q. In other words, the non-empty set P & Q is also excluded from the type.

The same applies for a constrained typevar [T: (P, Q)]: T & ~Q should simplify to P & ~Q, not just P.

Implementing this is actually purely a matter of removing code from the constrained typevar simplification logic; we just need to not bother removing the negations. If the negations are actually redundant (because the constraint types are disjoint), normal intersection simplification will already eliminate them (as shown in the added test.)

Copy link
Contributor

github-actions bot commented Apr 3, 2025

mypy_primer results

No ecosystem changes detected ✅

Copy link
Member

@dcreager dcreager left a comment

Choose a reason for hiding this comment

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

Thanks!

@carljm carljm merged commit bf03068 into main Apr 3, 2025
23 checks passed
@carljm carljm deleted the cjm/tvinter branch April 3, 2025 23:30
dcreager added a commit that referenced this pull request Apr 4, 2025
* main:
  [red-knot] Empty tuple is always-falsy (#17213)
  Run fuzzer with `--preview` (#17210)
  Bump 0.11.4 (#17212)
  [syntax-errors] Allow `yield` in base classes and annotations (#17206)
  Don't skip visiting non-tuple slice in `typing.Annotated` subscripts (#17201)
  [red-knot] mypy_primer: do not specify Python version (#17200)
  [red-knot] Add `Type.definition` method (#17153)
  Implement `Invalid rule provided` as rule RUF102 with `--fix` (#17138)
  [red-knot] Add basic on-hover to playground and LSP (#17057)
  [red-knot] don't remove negations when simplifying constrained typevars (#17189)
  [minor] Fix extra semicolon for clippy (#17188)
  [syntax-errors] Invalid syntax in annotations (#17101)
  [syntax-errors] Duplicate attributes in match class pattern (#17186)
  [syntax-errors] Fix multiple assignment for class keyword argument (#17184)
  use astral-sh/cargo-dist instead (#17187)
  Enable overindented docs lint (#17182)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants