Title: Properties `.start`, `.stop`, `. step` of (generic) `slice[...]` should be optional (`| None`) · Issue #15526 · python/typeshed · GitHub
Open Graph Title: Properties `.start`, `.stop`, `. step` of (generic) `slice[...]` should be optional (`| None`) · Issue #15526 · python/typeshed
X Title: Properties `.start`, `.stop`, `. step` of (generic) `slice[...]` should be optional (`| None`) · Issue #15526 · python/typeshed
Description: Situation #13008 made slice.__new__ more precise. Generic slice[T] accepts T | None as arguments when creating a slice. However, properties .start, .stop, . step of (generic) slice[...] are typed as there respective types, i.e. T in abov...
Open Graph Description: Situation #13008 made slice.__new__ more precise. Generic slice[T] accepts T | None as arguments when creating a slice. However, properties .start, .stop, . step of (generic) slice[...] are typed a...
X Description: Situation #13008 made slice.__new__ more precise. Generic slice[T] accepts T | None as arguments when creating a slice. However, properties .start, .stop, . step of (generic) slice[...] are typed a...
Opengraph URL: https://github.com/python/typeshed/issues/15526
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Properties `.start`, `.stop`, `. step` of (generic) `slice[...]` should be optional (`| None`)","articleBody":"# Situation\n[#13008](https://github.com/python/typeshed/pull/13008) made `slice.__new__` more precise. Generic `slice[T]` accepts `T | None` as arguments when creating a `slice`.\nHowever, properties `.start`, `.stop`, `. step` of (generic) `slice[...]` are typed as there respective types, i.e. `T` in above example.\n# Consequence\nThis leads to typecheckers accepting:\n```\ndef test(s: slice[int]) -> None:\n assert_type(s.start, int)\n if s.start is None:\n assert_never(s.start)\n```\nwhereas below obviously breaks:\n```\ntest(slice(42))\n```\n# Solution\nTherefore, I suggest properties `.start`, `.stop`, `. step` should have optional (`| None`) return types:\n```\n @property\n def start(self) -> _StartT_co | None: ...\n @property\n def step(self) -> _StepT_co | None: ...\n @property\n def stop(self) -> _StopT_co | None: ...\n```\n# Alternative\nCurrently, `typeshed` uses `slice[...]` as `slice[T | None]` explicitly as per [#13007](https://github.com/python/typeshed/pull/13007), e.g.\n```\nclass str:\n def __getitem__(self, key: SupportsIndex | slice[SupportsIndex | None], /) -> str: ...\n```\nWhile that works/typechecks correctly:\n```\ndef test(s: slice[int | None]) -> None:\n assert_type(s.start, int | None)\n if s.start is None or isinstance(s.start, int):\n pass\n else:\n assert_never(s.start)\n\ntest(slice(42)) # okay\ntest(slice('x')) # error: Argument 1 to \"slice\" has incompatible type \"str\"; expected \"int | None\" [arg-type]\n```\nI see as downsides:\n1. it's error-prone / less ergonomic\n2. it renders part of [#13008](https://github.com/python/typeshed/pull/13008) `__new__` overloading complexity superfluous\n3. it made [#13007](https://github.com/python/typeshed/pull/13007) more complex\n# Question\n@Sachaa-Thanasius: Was the proposed solution considered for [#13007](https://github.com/python/typeshed/pull/13007)?","author":{"url":"https://github.com/Dutcho","@type":"Person","name":"Dutcho"},"datePublished":"2026-03-19T11:27:45Z","url":"https://github.com/python/typeshed/issues/15526"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:b1042b34-cc2a-45f8-f280-c9eb2b108fec |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CB6E:37BEAD:219C9FF:2DCD5B1:6B002601 |
| html-safe-nonce | f5ff5b82f810ed0d93ee36aa071865027c29abe7ff3285a43994a70e22f9a36f |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDQjZFOjM3QkVBRDoyMTlDOUZGOjJEQ0Q1QjE6NkIwMDI2MDEiLCJ2aXNpdG9yX2lkIjoiNjY0MjQ3MzM3MDk3NTc0MTQ0MSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 2899231ff280696a97e9939c36a2bf48890331218ef86a740780d9c782888f68 |
| hovercard-subject-tag | issue:4101053056 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/typeshed/15526/issue_layout |
| twitter:image | https://opengraph.githubassets.com/5b36263e168a1e5502926e16098cb8c9560b071d808f9fd3f6146312b12f7aef/python/typeshed/issues/15526 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/5b36263e168a1e5502926e16098cb8c9560b071d808f9fd3f6146312b12f7aef/python/typeshed/issues/15526 |
| og:image:alt | Situation #13008 made slice.__new__ more precise. Generic slice[T] accepts T | None as arguments when creating a slice. However, properties .start, .stop, . step of (generic) slice[...] are typed a... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Dutcho |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4eb4cba0d69f045374ac15de9666d22b0961e1d555cc3824f36b7734c00c78e0 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/typeshed git https://github.com/python/typeshed.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 31696383 |
| octolytics-dimension-repository_nwo | python/typeshed |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 31696383 |
| octolytics-dimension-repository_network_root_nwo | python/typeshed |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 44c658b24abf00491fb7ae5f56d42c9f38cbeab2 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width