-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
new protocol, overloads to list.sort, sorted; prep. to fix #4051, #4155
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
Changes from all commits
356c64d
c02b783
7f5601a
3542d39
a5a0289
76e0907
e46d4c1
6ddefc0
eb0297b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,12 @@ if sys.version_info >= (3, 8): | |
else: | ||
from typing_extensions import Literal | ||
|
||
class _SupportsIndex(Protocol): | ||
def __index__(self) -> int: ... | ||
|
||
class _SupportsLessThan(Protocol): | ||
def __lt__(self, other: Any) -> bool: ... | ||
|
||
_T = TypeVar('_T') | ||
_T_co = TypeVar('_T_co', covariant=True) | ||
_KT = TypeVar('_KT') | ||
|
@@ -37,9 +43,7 @@ _T3 = TypeVar('_T3') | |
_T4 = TypeVar('_T4') | ||
_T5 = TypeVar('_T5') | ||
_TT = TypeVar('_TT', bound='type') | ||
|
||
class _SupportsIndex(Protocol): | ||
def __index__(self) -> int: ... | ||
_LT = TypeVar('_LT', bound=_SupportsLessThan) | ||
|
||
class object: | ||
__doc__: Optional[str] | ||
|
@@ -943,7 +947,10 @@ class list(MutableSequence[_T], Generic[_T]): | |
def remove(self, __value: _T) -> None: ... | ||
def reverse(self) -> None: ... | ||
if sys.version_info >= (3,): | ||
def sort(self, *, key: Optional[Callable[[_T], Any]] = ..., reverse: bool = ...) -> None: ... | ||
@overload | ||
def sort(self: List[_SupportsLessThan], *, key: None = ..., reverse: bool = ...) -> None: ... | ||
@overload | ||
def sort(self, *, key: Callable[[_T], _SupportsLessThan], reverse: bool = ...) -> None: ... | ||
else: | ||
def sort(self, cmp: Callable[[_T, _T], Any] = ..., key: Callable[[_T], Any] = ..., reverse: bool = ...) -> None: ... | ||
|
||
|
@@ -1539,8 +1546,13 @@ else: | |
def round(number: SupportsFloat, ndigits: int) -> float: ... | ||
def setattr(__obj: Any, __name: Text, __value: Any) -> None: ... | ||
if sys.version_info >= (3,): | ||
@overload | ||
def sorted(__iterable: Iterable[_LT], *, | ||
key: None = ..., | ||
reverse: bool = ...) -> List[_LT]: ... | ||
@overload | ||
def sorted(__iterable: Iterable[_T], *, | ||
key: Optional[Callable[[_T], Any]] = ..., | ||
key: Callable[[_T], _LT], | ||
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. Same here. 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. I will implement this change as well. Thanks, @srittau ! 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. I agree with @srittau here. A typevar used in a single position doesn't make semantic sense and type checkers could reasonably throw an error for code like this. 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. But here there is no invariance problem, so the |
||
reverse: bool = ...) -> List[_T]: ... | ||
else: | ||
def sorted(__iterable: Iterable[_T], *, | ||
|
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.
This must actually be
List[_LT]
to avoid the invariance issue mentioned in my comment.