Feature Request: Type hint for Fixed Length Homogeneous Sequences
維護者通常 1 天內回覆
還沒有人認領這個 Issue。
評估
研究方向
該 issue 未指出實作檔案、測試或進入點。首先檢視 typing 模組現有的 Tuple、TypeVar 和泛型序列行為,接著將提議的語法與所引用的 PEP 進行比較;完成的條件是確定一套達成共識的型別檢查設計、實作位置,以及涵蓋定長同質序列的測試。
由索引模型根據 Issue 內容生成。
描述
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 to add type hints to fixed-length sequences is to use Tuples 1. i.e
def foo(ten_floats: Tuple[float,float,float,float,float,float,float,float,float,float]):
...
For longer lists, this repetition is tedious and prone to error.
It would be nice to have a shorthand for defining homogeneous tuples. e.g
def foo(ten_floats: HomogeneousTuple[float, 10]):
...
Although 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.
Possible Implementation
I 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.
In Python 3.10 or newer [2] we will be able to use the unpack operator in subscript. So we might be able to do something like:
_t=[float]*10
def foo(ten_floats: Tuple[*_t]):
...
Using TypeVars, we could generalize this function to accept 10 of any type (as long as they are all the same type)
T=TypeVar('T')
TenTypeVars=[T]*10
def foo(ten_homogeneous_items: Tuple[*TenTypeVars]):
...
Following this logic, perhaps a generic type hint for Fixed Length Homogeneous Sequences could be implemented with
class FixedLengthHomogeneousSequence(Generic[T, N], Tuple[*[T]*N]):
...
FixedLengthHomogeneousSequence is a bit wordy. So perhaps FixedList would be a better name.
[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/
- 主要語言
- Python
- 星號
- 1.8k
- 分支
- 302
- 平均合併
- 20 小時 26 分鐘
- 30 天內合併 PR
- 10
環境準備
我們還沒有檢查這個專案的環境設定檔。先看它的 README,通用步驟見我們的新手貢獻指南。
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
python/typing 的其他 Issue
-
topic: typing spec
難度 2/5 1-3 小時 新手友好度 72/100
python/typing#2356 · 2 則留言 · 1 個 reaction ·
維護者通常 1 天內回覆
-
topic: typing spec
難度 2/5 1-3 小時 新手友好度 75/100
維護者通常 1 天內回覆
-
topic: documentation
難度 2/5 1-3 小時 新手友好度 76/100
維護者通常 1 天內回覆
-
topic: documentation
難度 2/5 1-3 小時 新手友好度 65/100
python/typing#2150 · 2 則留言 · 2 個 reaction ·
維護者通常 1 天內回覆
-
topic: conformance tests topic: typing spec
難度 3/5 1-2 天 新手友好度 72/100
維護者通常 1 天內回覆
相似的 Issue
-
needs triage
難度 2/5 1-3 小時 新手友好度 78/100
維護者通常 2 天內回覆
-
難度 2/5 1-3 小時 新手友好度 82/100
openvinotoolkit/openvino_notebooks#3665 ·
維護者通常 1 天內回覆
-
bug
難度 2/5 1-3 小時 新手友好度 86/100
維護者通常 1 天內回覆
-
docs
難度 2/5 1-3 小時 新手友好度 88/100
維護者通常 1 天內回覆
-
benchmark-gap
難度 2/5 1-3 小時 新手友好度 78/100
維護者通常 1 天內回覆