-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Unexpected "error: Slice index must be an integer or None" #2410
Comments
You did not pass a callable to |
Oh, right, so we need to update the typeshed for slice ? Indeed, I can't imagine other people won't have this problem. Numpy comes to mind, since it uses slices containing tuples to mimic multi-dimensional slicing. |
I don't know. Passing a callable to slice instead of an integer is most likely an error: elems[:x.count] # oops, should be x.count() |
Maybe slice() needs to become generic?
…--Guido (mobile)
|
Sounds good to me. It might make existing annotation less safe in the short term, though (since |
I think that somebody proposed making |
Found it: python/typing#159 |
Perhaps we should have |
That sounds reasonable to me, as potentially 90+% of slice objects have integer indices, and it would also be backward compatible. |
@elazarg: I'd go for your solution. Indeed, my need to pass a callable to the slice is a niche, and numpy slicing is not very comon, so this allow the best of both worlds. The reason I allow callables into slices is that I am creating an object you can slice to do the equivalent of itertools.dropwhile and takewhiles as a shorcut. |
(Just as a side note, there's a discussion in python-ideas of using |
I work on several projects in the scientific computing space that use slice objects with non- I don't like implicitly assuming that In NumPy, it's natural to pass NumPy integers into a In pandas and xarray, |
So we should probably just bite the bullet and make Slice generic. It
doesn't look like it'll be too hard. Elazar, do you want to be point on
this?
|
I am not entirely sure what is the decision. Should we write the type of slice everywhere as |
Hopefully it can just be Slice[int]? There shouldn't be too many places
where this is needed.
Or do you really want to support Slice[datetime, datetime, timedelta]? (The
only example I can think of where the type of the difference isn't the same
as the base value type.)
|
@shoyer just gave another example - panda's I think it would be nice to have |
The current rules for generics don't allow that.
|
Meanwhile, there are much more mundane ways to trigger this message:
The message says "... integer or None" but visit_slice_expr seems to allow |
@dckc You're using |
Yes, I suppose I am. |
I wanted to add just another usecase of slice having non-int arguments. In pandas you quite usually have time index. So another valid case is having e.g.
|
This can also manifest itself in errors message like |
On the off chance that additional non-scientific use cases are required, I have a library that would also like to use slice syntax to represent a timeline: I am admittedly still a mypy newcomer and might be making some basic mistakes there. Notably, mypy typechecks this without error, but fails a typecheck of a call to this slice syntax in another package importing the above linked code. |
This actually triggers on something like from typing import Any
x: Any
x['a':'b'] which is clearly a bug. |
As @Artimi mentioned, there're many use cases in pandas that a time index is involved. |
datetime seems limited. A slice is represent a boundary, but boundaries can be established with practically anything. In fact, I quite like the idea of being able to pass a callable to __getitem__ mentioned by the OP: it lets you say "start to take elements when this condition arise, or stop taking elements when this one arises. You could even make a wrapper object like:
It's much more elegant than going for itertools.dropwhile and takewhile. I think slice() should allow library writers to do such experiments. |
So this issue is now starting to crop up on a semi-regular basis in the likes of numpy, scipy and pandas, It would already be a massive improvement if the rather restrictive In [1]: class Index:
...: def __init__(self, value: int) -> None:
...: self.value = value
...:
...: def __index__(self) -> int:
...: return self.value
...:
In [3]: a = range(10)
...: a[Index(0):Index(5)]
Out[3]: range(0, 5)
In [4]: b = '0123456789'
...: b[Index(0):Index(5)]
Out[4]: '01234'
In [5]: c = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
...: c[Index(0):Index(5)]
Out[5]: [0, 1, 2, 3, 4] |
I can take a look at the implications of such a suggested change. I do think that allowing |
python/typing#159 is closely related. |
Just |
Helps with python#2410, as suggested by BvB93 in python#2410 (comment) PEP 696 will be the real solution here, since it will allow us to make slice generic
Helps with #2410, as suggested by BvB93 in #2410 (comment) PEP 696 will be the real solution here, since it will allow us to make slice generic with few backward compatibility issues
If we have PEP 696 accepted, slice can become generic in type variable with |
Since this is still open, there could be two versions
If it is not possible to overload a generic type or provide defaults to a For numbers, Me personally, I've now and then wanted slices with floats, or strings, so I could slice arrays of floats or the keys of a dict. But so far, easily enough achievable with a regular method, instead of overloading the |
I don't know if this is related, but I landed here because I encountered this same mypy error with an implicit slice (is that a thing? not sure). Boils down to
This is valid but causes a mypy error: This does not create the error: With pandas 2.2.2 and mypy 1.10.0, so I think must still be an issue. Doing the latter is a relatively easy fix, but here's yet another instance of a non-integer slice. |
Despite having:
Running mypy triggers "error: Slice index must be an integer or None" for
g()[lambda x: x > 4:]
.The text was updated successfully, but these errors were encountered: