-
Notifications
You must be signed in to change notification settings - Fork 1.8k
[ty] Fix implementation of Top[Callable[..., object]]
#22145
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
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
24404d1
Use gradual form (...) for parameters in Callable bottom type
charliermarsh 9e58e6f
Use a top
charliermarsh 280a0a7
Use assignability
charliermarsh cc08d11
Make call on Top an error
charliermarsh 85d3226
Address feedback
charliermarsh c7f927e
Review feedback
charliermarsh 2ebc586
Use is_top_materialization
charliermarsh d75a93d
Allow Top to be assignable to (...) -> object
charliermarsh 5201bc3
Address review comments
charliermarsh 2bb397c
Update crates/ty_python_semantic/resources/mdtest/narrow/callable.md
charliermarsh f215c39
Update crates/ty_python_semantic/src/types/diagnostic.rs
charliermarsh 81475b2
Regenerate
charliermarsh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
95 changes: 95 additions & 0 deletions
95
crates/ty_python_semantic/resources/mdtest/narrow/callable.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| # Narrowing for `callable()` | ||
|
|
||
| ## Basic narrowing | ||
|
|
||
| The `callable()` builtin returns `TypeIs[Callable[..., object]]`, which narrows the type to the | ||
| intersection with `Top[Callable[..., object]]`. The `Top[...]` wrapper indicates this is a fully | ||
| static type representing the top materialization of a gradual callable. | ||
|
|
||
| Since all callable types are subtypes of `Top[Callable[..., object]]`, intersections with `Top[...]` | ||
| simplify to just the original callable type. | ||
|
|
||
| ```py | ||
| from typing import Any, Callable | ||
|
|
||
| def f(x: Callable[..., Any] | None): | ||
| if callable(x): | ||
| # The intersection simplifies because `(...) -> Any` is a subtype of | ||
| # `Top[(...) -> object]` - all callables are subtypes of the top materialization. | ||
| reveal_type(x) # revealed: (...) -> Any | ||
| else: | ||
| # Since `(...) -> Any` is a subtype of `Top[(...) -> object]`, the intersection | ||
| # with the negation is empty (Never), leaving just None. | ||
| reveal_type(x) # revealed: None | ||
| ``` | ||
|
|
||
| ## Narrowing with other callable types | ||
|
|
||
| ```py | ||
| from typing import Any, Callable | ||
|
|
||
| def g(x: Callable[[int], str] | None): | ||
| if callable(x): | ||
| # All callables are subtypes of `Top[(...) -> object]`, so the intersection simplifies. | ||
| reveal_type(x) # revealed: (int, /) -> str | ||
| else: | ||
| reveal_type(x) # revealed: None | ||
|
|
||
| def h(x: Callable[..., int] | None): | ||
| if callable(x): | ||
| reveal_type(x) # revealed: (...) -> int | ||
| else: | ||
| reveal_type(x) # revealed: None | ||
| ``` | ||
|
|
||
| ## Narrowing from object | ||
|
|
||
| ```py | ||
| from typing import Callable | ||
|
|
||
| def f(x: object): | ||
| if callable(x): | ||
| reveal_type(x) # revealed: Top[(...) -> object] | ||
| else: | ||
| reveal_type(x) # revealed: ~Top[(...) -> object] | ||
| ``` | ||
|
|
||
| ## Calling narrowed callables | ||
|
|
||
| The narrowed type `Top[Callable[..., object]]` represents the set of all possible callable types | ||
| (including, e.g., functions that take no arguments and functions that require arguments). While such | ||
| objects *are* callable (they pass `callable()`), no specific set of arguments can be guaranteed to | ||
| be valid. | ||
|
|
||
| ```py | ||
| import typing as t | ||
|
|
||
| def call_with_args(y: object, a: int, b: str) -> object: | ||
| if isinstance(y, t.Callable): | ||
| # error: [call-top-callable] | ||
| return y(a, b) | ||
| return None | ||
| ``` | ||
|
|
||
| ## Assignability of narrowed callables | ||
|
|
||
| A narrowed callable `Top[Callable[..., object]]` should be assignable to `Callable[..., Any]`. This | ||
| is important for decorators and other patterns where we need to pass the narrowed callable to | ||
| functions expecting gradual callables. | ||
|
|
||
| ```py | ||
| from typing import Any, Callable, TypeVar | ||
| from ty_extensions import static_assert, Top, is_assignable_to | ||
|
|
||
| static_assert(is_assignable_to(Top[Callable[..., bool]], Callable[..., int])) | ||
|
|
||
| F = TypeVar("F", bound=Callable[..., Any]) | ||
|
|
||
| def wrap(f: F) -> F: | ||
| return f | ||
|
|
||
| def f(x: object): | ||
| if callable(x): | ||
| # x has type `Top[(...) -> object]`, which should be assignable to `Callable[..., Any]` | ||
| wrap(x) | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.