python / typeshed

Collection of library stubs for Python, with static types
Other
4.3k stars 1.73k forks source link

`builtins.slice` should be generic #8647

Open randolf-scholz opened 2 years ago

randolf-scholz commented 2 years ago

This is a port of https://github.com/python/typing/issues/159, which was closed as being out of scope, with https://github.com/srittau suggesting it maybe be more suitable for typeshed instead.

It would be useful to have slice being generic and allowing type hints a la

Syntax wise, I would advocate for the latter options slice[S, T]=slice[S, T, Any], slice[T]=slice[Any, T, Any] for the following reasons:

  1. It resembles the regular usage of slice:
    • slice(x) = slice(None, x, None),
    • slice(a, b) = slice(a, b, None)
  2. It has better compatible with datetime/timedelta variables, which might be a common use-case.
    • slice[datetime]=slice[datetime, datetime, datetime] would be an error, because `datetimes cannot be added, only timedeltas can be added to datetimes.

Real-world examples

TeamSpen210 commented 2 years ago

An example of heterogeneous types (although a little cute) is to do obj["keyword": value], effectively producing keyword arguments.

Gobot1234 commented 2 years ago

Related https://peps.python.org/pep-0696/#using-another-typevarlike-as-the-default which were my thoughts on the matter

AlexWaygood commented 1 year ago

Marking as deferred until and unless PEP-696 is accepted by the Steering Council and implemented by all major type checkers

lorenzo-w commented 7 months ago

Little status update:

srittau commented 7 months ago

Cf. #11422 for the feature tracker.