-
Notifications
You must be signed in to change notification settings - Fork 243
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
builtins.slice
should be generic
#159
Comments
(a) Have you seen such code in practice? |
(a) Numpy uses this, it has integer-like classes that don't subclass |
Indeed, in the scientific computing world, Pandas is another widely used library with objects that support label based slicing using non-integers. For example, you can write I think we are unlikely to add type annotations to pandas in the near future, but it does seem likely that some users will be interested in using numpy and pandas with type annotations. |
Adding |
If add generic
The type of
|
(I think you meant It seems verbose to have Are you aware of objects that take different types? |
In pandas, |
@shoyer To support that I would propose to have two generic types (or better a type and an alias). For example in typeshed we can define: # builtins.pyi
class slice(Generic[T, S, U]):
...
# typing.pyi
Slice = slice[T, T, T] At runtime This will deviate from the current common pattern, but this may be the case where practicality beats purity. @JukkaL I don't think this will deteriorate type safety. First, the type arguments will be inferred, so that |
Maybe we can assume that the type of |
I would like to see |
Maybe we should include |
I think this makes sense. @ambv |
You cannot assume that |
Anecdotal, but I've come across a use-case for separate T_start and T_stop types. It's a bit niche but here it goes: I was trying to create a utility class for generic function return type annotation, which could optionally associate a name with an output type. I would have liked the convenient syntax of # "useful_var_name" is not relevant in the application-level code,
# but an underlying framework could use the annotation to do things like generate docs
def my_function(...) -> Output["useful_var_name": MyOutputType]:
return MyOutputType() # Output should to be implemented such that the type checker does not complain here
# this should also be allowed and do the same thing as above, minus the var name
def my_function(...) -> Output[MyOutputType]:
return MyOutputType() # again, type checker should not complain here Making I guess the stub would be something like: T = TypeVar("T")
class Output(Generic[T]):
@overload
def __class_getitem__(cls, item: slice[str, Type[T]]):
...
def __class_getitem__(cls, item: Type[T]):
... Using the new variadic typevars, I imagine one could even make it do something like: def my_function(...) -> Output["useful_var_name": MyOutputType, "other_var": SecondOutputType]: # can arbitrarily add more types here
return MyOutputType(), SecondOutputType() From what I could tell, this did not make it into PEP 585 - any chance it'll make it into a future version? I'm also not too well-versed in the internals of the typing module, but is there a straightforward way one could already implement something like this without deeper changes to |
Hi,
? |
This is an issue for either typeshed and/or the CPython bug tracker, closing it here. |
It is entirely reasonable that some libraries would have slices with a component type other than
int
.The text was updated successfully, but these errors were encountered: