Skip to content

[red-knot] Disjointness for callable types #17094

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

Merged
merged 2 commits into from
Apr 1, 2025
Merged

Conversation

dhruvmanila
Copy link
Member

@dhruvmanila dhruvmanila commented Mar 31, 2025

Summary

Part of #15382, this PR adds support for disjointness between two callable types. They are never disjoint because there exists a callable type that's a subtype of all other callable types:

(*args: object, **kwargs: object) -> Never

The Never is a subtype of every fully static type thus a callable type that has the return type of Never means that it is a subtype of every return type.

Test Plan

Add test cases related to mixed parameter kinds, gradual form (...) and Never type.

@dhruvmanila dhruvmanila added the ty Multi-file analysis & type inference label Mar 31, 2025
Copy link
Contributor

github-actions bot commented Mar 31, 2025

mypy_primer results

No ecosystem changes detected ✅

@dhruvmanila dhruvmanila force-pushed the dhruv/callable-disjoint branch 2 times, most recently from ce725c5 to beb8d94 Compare March 31, 2025 16:06
Comment on lines 83 to 92
If a tuple type contains a `Never` element, it is considered similar to `Never` itself and is
disjoint from any other type.

```py
static_assert(is_disjoint_from(tuple[Never], tuple[Never]))
static_assert(is_disjoint_from(tuple[int, Never], tuple[int, Never]))
static_assert(is_disjoint_from(tuple[int, Never], tuple[Never, int]))
static_assert(is_disjoint_from(tuple[int, Never], tuple[int, int]))
```

Copy link
Member Author

Choose a reason for hiding this comment

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

This is not related to this PR but it helped my understanding when I discussed Never usage with Alex and David last week so I added some test cases for it as they were missing.

@dhruvmanila dhruvmanila marked this pull request as ready for review April 1, 2025 14:10
Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

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

Nice, this looks great. A couple of small points

@dhruvmanila dhruvmanila force-pushed the dhruv/callable-disjoint branch 2 times, most recently from ffca69f to 60b3189 Compare April 1, 2025 18:44
@dhruvmanila dhruvmanila force-pushed the dhruv/callable-disjoint branch from 60b3189 to 3544413 Compare April 1, 2025 18:51
@dhruvmanila dhruvmanila enabled auto-merge (squash) April 1, 2025 18:57
@dhruvmanila dhruvmanila merged commit 6be0a50 into main Apr 1, 2025
19 checks passed
@dhruvmanila dhruvmanila deleted the dhruv/callable-disjoint branch April 1, 2025 19:00
dcreager added a commit that referenced this pull request Apr 1, 2025
* main:
  [red-knot] Add property tests for callable types (#17006)
  [red-knot] Disjointness for callable types (#17094)
  [red-knot] Flatten `Type::Callable` into four `Type` variants (#17126)
  mdtest.py: do a full mdtest run immediately when the script is executed (#17128)
  [red-knot] Fix callable subtyping for standard parameters (#17125)
  [red-knot] Fix more `redundant-cast` false positives (#17119)
  Sync vendored typeshed stubs (#17106)
  [red-knot] support Any as a class in typeshed (#17107)
  Visit `Identifier` node as part of the `SourceOrderVisitor` (#17110)
  [red-knot] Don't infer Todo for quite so many tuple type expressions (#17116)
  CI: Run pre-commit on depot machine (#17120)
  Error instead of `panic!` when running Ruff from a deleted directory (#16903) (#17054)
  Control flow graph: setup (#17064)
  [red-knot] Playground improvements (#17109)
  [red-knot] IDE crate (#17045)
  Update dependency vite to v6.2.4 (#17104)
  [red-knot] Add redundant-cast error (#17100)
  [red-knot] Narrowing on `in tuple[...]` and `in str` (#17059)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ty Multi-file analysis & type inference
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants