-
-
Notifications
You must be signed in to change notification settings - Fork 2k
[operator] Replace or explain Any #15597
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
Draft
srittau
wants to merge
5
commits into
python:main
Choose a base branch
from
srittau:operator-any
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+24
−5
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
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,11 @@ | ||
| from operator import methodcaller | ||
| from typing import Any | ||
| from typing_extensions import assert_type | ||
|
|
||
| m1 = methodcaller("foo") | ||
| assert_type(m1, methodcaller[[], Any]) | ||
| m2 = methodcaller("foo", 42, bar="") | ||
| # assert_type(m2, methodcaller[[int, Arg(str, "bar")], Any]) | ||
| m3: methodcaller[[], int] = methodcaller("foo") # ok | ||
| m4: methodcaller[[int], Any] = methodcaller("foo", 1) # ok | ||
| m5: methodcaller[[str], int] = methodcaller("foo", 1) # type: ignore | ||
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 |
|---|---|---|
|
|
@@ -53,14 +53,16 @@ from _operator import ( | |
| xor as xor, | ||
| ) | ||
| from _typeshed import SupportsGetItem | ||
| from typing import Any, Generic, TypeVar, final, overload | ||
| from typing_extensions import Self, TypeVarTuple, Unpack | ||
| from typing import Any, Generic, ParamSpec, TypeVar, final, overload | ||
| from typing_extensions import TypeVarTuple, Unpack | ||
|
|
||
| _T = TypeVar("_T") | ||
| _T_co = TypeVar("_T_co", covariant=True) | ||
| _T1 = TypeVar("_T1") | ||
| _T2 = TypeVar("_T2") | ||
| _Ts = TypeVarTuple("_Ts") | ||
| _P = ParamSpec("_P", default=...) | ||
| _R = TypeVar("_R", default=Any) | ||
|
|
||
| __all__ = [ | ||
| "abs", | ||
|
|
@@ -182,6 +184,8 @@ if sys.version_info >= (3, 11): | |
| # them here. | ||
| @final | ||
| class attrgetter(Generic[_T_co]): | ||
| # We can't determine the type of the attribute(s) being accessed statucally, | ||
| # so we have to use Any for the return type. | ||
| @overload | ||
| def __new__(cls, attr: str, /) -> attrgetter[Any]: ... | ||
| @overload | ||
|
|
@@ -192,6 +196,8 @@ class attrgetter(Generic[_T_co]): | |
| def __new__(cls, attr: str, attr2: str, attr3: str, attr4: str, /) -> attrgetter[tuple[Any, Any, Any, Any]]: ... | ||
| @overload | ||
| def __new__(cls, attr: str, /, *attrs: str) -> attrgetter[tuple[Any, ...]]: ... | ||
| # obj needs to have the named attribute(s) with the correct type. | ||
| # Unfortunately, we can't check that statically, so we need to use Any. | ||
| def __call__(self, obj: Any, /) -> _T_co: ... | ||
|
|
||
| @final | ||
|
|
@@ -212,6 +218,8 @@ class itemgetter(Generic[_T_co]): | |
| def __call__(self, obj: SupportsGetItem[Any, Any]) -> Any: ... | ||
|
|
||
| @final | ||
| class methodcaller: | ||
| def __new__(cls, name: str, /, *args: Any, **kwargs: Any) -> Self: ... | ||
| def __call__(self, obj: Any) -> Any: ... | ||
| class methodcaller(Generic[_P, _R]): | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Making this generic, makes it possible to use better m: methodcaller[[int], str] = methodcaller("foo") # error (missing argument)I'll add some tests. |
||
| def __new__(cls, name: str, /, *args: _P.args, **kwargs: _P.kwargs) -> methodcaller[_P, Any]: ... | ||
| # obj needs to have the named method with the correct signature. | ||
| # Unfortunately, we can't check that statically, so we need to use Any. | ||
| def __call__(self, obj: Any) -> _R: ... | ||
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.
Is there a way to specialize a ParamSpec's kwargs in older Python versions?