-
Notifications
You must be signed in to change notification settings - Fork 1.8k
[ty] Fix subtyping of type[Any] / type[T] and protocols
#21678
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
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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
crates/ty_python_semantic/resources/mdtest/libraries/numpy.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,67 @@ | ||
| # numpy | ||
|
|
||
| ```toml | ||
| [environment] | ||
| python-version = "3.14" | ||
| ``` | ||
|
|
||
| ## numpy's `dtype` | ||
|
|
||
| numpy functions often accept a `dtype` parameter. For example, one of `np.array`'s overloads accepts | ||
| a `dtype` parameter of type `DTypeLike | None`. Here, we build up something that resembles numpy's | ||
| internals in order to model the type `DTypeLike`. Many details have been left out. | ||
|
|
||
| `mini_numpy.py`: | ||
|
|
||
| ```py | ||
| from typing import TypeVar, Generic, Any, Protocol, TypeAlias, runtime_checkable, final | ||
| import builtins | ||
|
|
||
| _ItemT_co = TypeVar("_ItemT_co", default=Any, covariant=True) | ||
|
|
||
| class generic(Generic[_ItemT_co]): | ||
| @property | ||
| def dtype(self) -> _DTypeT_co: | ||
| raise NotImplementedError | ||
|
|
||
| _BoolItemT_co = TypeVar("_BoolItemT_co", bound=builtins.bool, default=builtins.bool, covariant=True) | ||
|
|
||
| class bool(generic[_BoolItemT_co], Generic[_BoolItemT_co]): ... | ||
|
|
||
| @final | ||
| class object_(generic): ... | ||
|
|
||
| _ScalarT = TypeVar("_ScalarT", bound=generic) | ||
| _ScalarT_co = TypeVar("_ScalarT_co", bound=generic, default=Any, covariant=True) | ||
|
|
||
| @final | ||
| class dtype(Generic[_ScalarT_co]): ... | ||
|
|
||
| _DTypeT_co = TypeVar("_DTypeT_co", bound=dtype, default=dtype, covariant=True) | ||
|
|
||
| @runtime_checkable | ||
| class _SupportsDType(Protocol[_DTypeT_co]): | ||
| @property | ||
| def dtype(self) -> _DTypeT_co: ... | ||
|
|
||
| # TODO: no errors here | ||
| # error: [invalid-type-arguments] "Type `typing.TypeVar` is not assignable to upper bound `generic[Any]` of type variable `_ScalarT_co@dtype`" | ||
| # error: [invalid-type-arguments] "Type `typing.TypeVar` is not assignable to upper bound `generic[Any]` of type variable `_ScalarT_co@dtype`" | ||
| _DTypeLike: TypeAlias = type[_ScalarT] | dtype[_ScalarT] | _SupportsDType[dtype[_ScalarT]] | ||
|
|
||
| DTypeLike: TypeAlias = _DTypeLike[Any] | str | None | ||
| ``` | ||
|
|
||
| Now we can make sure that a function which accepts `DTypeLike | None` works as expected: | ||
|
|
||
| ```py | ||
| import mini_numpy as np | ||
|
|
||
| def accepts_dtype(dtype: np.DTypeLike | None) -> None: ... | ||
|
|
||
| accepts_dtype(dtype=np.bool) | ||
| accepts_dtype(dtype=np.dtype[np.bool]) | ||
| accepts_dtype(dtype=object) | ||
| accepts_dtype(dtype=np.object_) | ||
| accepts_dtype(dtype="U") | ||
| ``` | ||
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These errors prevent us from running into the problem on
main. But on #21553, these errors will go away, and then we need to properly understand all of this code here, or otherwise we seenp.arraycalls failing all over the place.