Title: Feature Request: Type hint for Fixed Length Homogeneous Sequences · Issue #786 · python/typing · GitHub
Open Graph Title: Feature Request: Type hint for Fixed Length Homogeneous Sequences · Issue #786 · python/typing
X Title: Feature Request: Type hint for Fixed Length Homogeneous Sequences · Issue #786 · python/typing
Description: Feature A concise way to hint that a sequence of homogenous types is a set length. i.e The items in the sequence are all of the same type, the sequence is iterable, and cannot grow larger or smaller. Pitch Currently, the recommended way ...
Open Graph Description: Feature A concise way to hint that a sequence of homogenous types is a set length. i.e The items in the sequence are all of the same type, the sequence is iterable, and cannot grow larger or smalle...
X Description: Feature A concise way to hint that a sequence of homogenous types is a set length. i.e The items in the sequence are all of the same type, the sequence is iterable, and cannot grow larger or smalle...
Opengraph URL: https://github.com/python/typing/issues/786
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Feature Request: Type hint for Fixed Length Homogeneous Sequences","articleBody":"**Feature**\r\n\r\nA concise way to hint that a sequence of homogenous types is a set length. i.e The items in the sequence are all of the same type, the sequence is iterable, and cannot grow larger or smaller.\r\n\r\n**Pitch**\r\n\r\nCurrently, the recommended way to add type hints to fixed-length sequences is to use Tuples [\u003csup>1\u003c/sup>](https://stackoverflow.com/questions/44833822/specify-length-of-sequence-or-list-with-python-typing-module). i.e\r\n```python\r\ndef foo(ten_floats: Tuple[float,float,float,float,float,float,float,float,float,float]):\r\n ...\r\n````\r\nFor longer lists, this repetition is tedious and prone to error.\r\nIt would be nice to have a shorthand for defining homogeneous tuples. e.g\r\n```python\r\ndef foo(ten_floats: HomogeneousTuple[float, 10]):\r\n ...\r\n````\r\n\r\nAlthough the name `Tuple` does not signal intent very well. The real thing that we are trying to signal is that this is a sequence of floats of length 10. It does not necessarily have to be a Tuple, it just has to be iterable, and have 10 items, that cannot grow larger or smaller. If I wanted to pass the function a List, this should also pass static type checkers, as long as it has 10 floats.\r\n\r\n\r\n**Possible Implementation**\r\n\r\nI am not a core python developer, I have never even looked at the implementation of the typing module, and I'm not even that good of a python developer, so please forgive me if this is nonsense.\r\n\r\nIn Python 3.10 or newer \u003csup>[2]\u003c/sup> we will be able to use the unpack operator in subscript. So we might be able to do something like:\r\n\r\n```python\r\n_t=[float]*10\r\ndef foo(ten_floats: Tuple[*_t]):\r\n ...\r\n```\r\n\r\nUsing TypeVars, we could generalize this function to accept 10 of any type (as long as they are all the same type)\r\n\r\n```python\r\nT=TypeVar('T')\r\nTenTypeVars=[T]*10\r\ndef foo(ten_homogeneous_items: Tuple[*TenTypeVars]):\r\n ...\r\n```\r\n\r\n\r\nFollowing this logic, perhaps a generic type hint for Fixed Length Homogeneous Sequences could be implemented with\r\n\r\n```python\r\nclass FixedLengthHomogeneousSequence(Generic[T, N], Tuple[*[T]*N]):\r\n ...\r\n```\r\n\r\n`FixedLengthHomogeneousSequence` is a bit wordy. So perhaps `FixedList` would be a better name.\r\n\r\n[2] (I think this is true). Pylance gave me an error saying this was the case. I think it's related to https://www.python.org/dev/peps/pep-0622/","author":{"url":"https://github.com/craigh92","@type":"Person","name":"craigh92"},"datePublished":"2021-02-11T13:38:53Z","url":"https://github.com/python/typing/issues/786"}
| 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:986c6bed-3d99-025b-f953-49ef67229e34 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9204:37BEAD:1EBBFBD:29C5088:6B001666 |
| html-safe-nonce | 0a6840d9dcfcc5a69a3a3edd775240f3cfd1d622c4a0a41aa8fef274f624c66a |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5MjA0OjM3QkVBRDoxRUJCRkJEOjI5QzUwODg6NkIwMDE2NjYiLCJ2aXNpdG9yX2lkIjoiNzYzMjQwMDI2OTY5ODkzMDI3OCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 5c609494b8d4bcd5306bc8fe9c126538f42356a175951c9bc14b1a4740bcad20 |
| hovercard-subject-tag | issue:806398663 |
| 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/typing/786/issue_layout |
| twitter:image | https://opengraph.githubassets.com/36ee0860c8281dd4c2114c51e8bb7d135e1cd357ce3b29f732572b374b131165/python/typing/issues/786 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/36ee0860c8281dd4c2114c51e8bb7d135e1cd357ce3b29f732572b374b131165/python/typing/issues/786 |
| og:image:alt | Feature A concise way to hint that a sequence of homogenous types is a set length. i.e The items in the sequence are all of the same type, the sequence is iterable, and cannot grow larger or smalle... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | craigh92 |
| hostname | github.com |
| expected-hostname | github.com |
| None | a2dd2291b1b13cc9c06f27ec92fa3dbaf34116fc24c9347b8539db61884c59f4 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/typing git https://github.com/python/typing.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 24604008 |
| octolytics-dimension-repository_nwo | python/typing |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 24604008 |
| octolytics-dimension-repository_network_root_nwo | python/typing |
| 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 | 70121d4e855ee0bb7b75cf15d061d91f8863c0f8 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width