-
-
Notifications
You must be signed in to change notification settings - Fork 145
feat(index): append #1282
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
feat(index): append #1282
Changes from 1 commit
390e5d9
3a34057
45e0ad3
6c8ba76
3844062
ac0857b
67e6bde
fa3c401
d7a538d
fe5f1a9
1c75df6
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 |
---|---|---|
|
@@ -401,7 +401,12 @@ class Index(IndexOpsMixin[S1]): | |
) -> Self: ... | ||
@overload | ||
def __getitem__(self, idx: int | tuple[np_ndarray_anyint, ...]) -> S1: ... | ||
def append(self, other): ... | ||
@overload | ||
def append(self, other: Index[Never]) -> Index: ... | ||
@overload | ||
def append(self, other: Index[S1] | Sequence[Index[S1]]) -> Index[S1]: ... | ||
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. it might be nicer here to have a second S1 here as the resulting Index can contain a mix of different types.
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. 45e0ad3, but this one works less well.
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.
While that is annoying for testing on the CI, I think that is the safer choice for user: rather expect a wider type that includes If from __future__ import annotations
from typing import TypeVar, reveal_type, Generic, Sequence
S1 = TypeVar("S1", bound=int | str, covariant=True)
S2 = TypeVar("S2", bound=int | str, covariant=True)
class Index(Generic[S1]):
def __init__(self, data: list[S1]) -> None: ...
def append(self: Index[S1], other: Sequence[Index[S2]]) -> Index[S1 | S2]: ...
strings = Index(["a"])
reveal_type(strings)
ints = Index([1])
reveal_type(ints)
reveal_type(strings.append([ints]))
reveal_type(ints.append([strings]))
string_ints = Index(["a", 1])
reveal_type(string_ints)
reveal_type(string_ints.append([ints]))
reveal_type(strings.append([string_ints]))
reveal_type(strings.append([ints]))
reveal_type(strings.append([strings, ints]))
reveal_type(strings.append([ints, strings, string_ints])) 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.
Hi, I am new to covariance / contravariance, but I read PEP484 (covariance-and-contravariance) and it says
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. We can't change from __future__ import annotations
from typing import TypeVar, reveal_type, Generic, Sequence
S1 = TypeVar("S1", bound=int | str)
IndexT = TypeVar("IndexT", bound="Index")
class Index(Generic[S1]):
def __init__(self, data: list[S1]) -> None: ...
def append(self: Index[S1], other: Sequence[IndexT]) -> Index[S1] | IndexT: ...
strings = Index(["a"])
reveal_type(strings)
ints = Index([1])
reveal_type(ints)
reveal_type(strings.append([ints]))
reveal_type(ints.append([strings]))
string_ints = Index(["a", 1])
reveal_type(string_ints)
reveal_type(string_ints.append([ints]))
reveal_type(strings.append([string_ints]))
reveal_type(strings.append([ints]))
reveal_type(strings.append([strings, ints]))
reveal_type(strings.append([ints, strings, string_ints])) 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. Hi, I ran the script myself. In the most complicated case, I see |
||
@overload | ||
def append(self, other: Index | Sequence) -> Index: ... | ||
def putmask(self, mask, value): ... | ||
def equals(self, other) -> bool: ... | ||
@final | ||
|
Uh oh!
There was an error while loading. Please reload this page.