![](/img/trans.png)
[英]How can I type hint a dictionary where the key is a specific tuple and the value is known?
如何鍵入提示字典,其中鍵是特定元組且值已知? 例如,我想像這樣輸入提示: 用例將從一組已知的基數到 go 到先前使用這些基數定義的 class。 ...
[英]How can I type hint a dictionary where the key is a specific tuple and the value is known?
如何鍵入提示字典,其中鍵是特定元組且值已知? 例如,我想像這樣輸入提示: 用例將從一組已知的基數到 go 到先前使用這些基數定義的 class。 ...
[英]Mypy: define function working on every children of a TypedDict
我有一個關於 TypedDicts 的打字問題。 我想用“foo”方法定義一個抽象的 class,接受任何帶有鍵“a”的字典。 我想定義實現抽象 class 的具體類,使用 foo 方法接受第一個 class 的“a”和“b”以及第二個 class 的“a”和“c”。 我實現了它,如下所示。 但是 ...
[英]TypedDict class is an Incompatible Type for function expecting Dict
我創建了下面的 TypedDict class;class Appinfo(TypedDict): appid: int extended: dict[str, Any] config: dict[str, Any] depots: dict[str, Any] ...
[英]Python TypedDict: functional syntax when inheriting another TypedDict
鑒於這種類型: class TPerson(TypedDict): name: str address: str 我想要另一個TypedDict繼承前一個,例如: class TDealer(TPerson): db-id: int police_record: str ar ...
[英]How to have optional keys in TypedDict?
我正在嘗試使用可選鍵創建一個TypedDict ,但Optional NotRequired都不起作用。 (我根據這個 SO answer嘗試NotRequired ) 對於這兩個鍵,我得到了Parameter 'my_key1' unfilled Parameter 'my_key2' un ...
[英]How to access the type of an attribute in a TypedDict?
給定一個TypedDict ,您如何訪問/使用其屬性之一的類型? 例如:class Shape(TypedDict): kind: Literal['square', 'circle'] x: int y: int KindOfShape: TypeAlias = Shape.kin ...
[英]Validate `Dict` against `TypedDict` with MyPy
我有幾個數據處理器實現了一個接口,其方法“process”接管處理部分並返回結果。 結果應該是符合給定結構的字典。 這就是我為每個處理器結果定義 TypedDict 的原因。 也許我沒有正確理解 TypedDict,但我想實現這樣的目標: 如果我從“process”方法返回普通的 Dict,MyP ...
[英]Python typing: Avoid `cast TypedDict` when filtering dict
我有一個TypedDict定義,並希望類型檢查器識別通過過濾另一個字典動態創建的某個字典符合類型定義: 目前,我發現獲得我想要的東西的唯一方法是使用cast 。 有沒有辦法避免cast ? 理想情況下, 應該可以工作,但它給了我Argument of type "dict[str, Unknown ...
[英]Prevent TypedDict from accepting arbitrary parameters
我注意到TypedDict似乎讓你向它傳遞任何不太好的參數。 我想這是因為 TypedDict 僅適用於類型檢查器級別。 但是如果我仍然想在運行時防止這種情況發生,那么使用 TypedDict 的替代方法是什么? ...
[英]Python typehint subset(partial) of an TypedDict
在 typescript 中我們有Partial type ,所以我們可以這樣做:interface Foo { x:number y:number } const foo:Partial<Foo> = {x: 1} (有了這個,我們可以使接口的所有屬性成為可選的 ...
[英]TypedDict does not allow optional keys?
我想使用 pydantic 和 TypedDict 進行模式驗證來定義嵌套字典模式的一部分。 但是,我意識到如果在 TypedDict 類中指定Optional則它不起作用。 我讀到此類將根據需要呈現其中的所有鍵,並且使所有鍵成為可選的方法是在total=False 。 但是,我只希望其中一個鍵是 ...
[英]Can you give an example for a NESTED TypedDict in class notation?
我想輸入設置並遇到這個答案和 TypedDict 但無法讓它與嵌套結構一起工作。 假設以下 dict,我怎樣才能最好地添加打字? PS:我也有興趣,如果你有答案,如何用數據類做到這一點? ...
[英]Iterating over TypedDict's keys
這是一個例子:from typing import TypedDict class Foo(TypedDict): a: str b: int foo = Foo(a='2', b=4) for key in foo: print(foo[key]) 我明白了 我覺得 ...
[英]Type-checked conversion between Dataclasses and TypedDicts
我有一堆@dataclass es 和一堆相應的TypedDict s,我想促進它們之間的平滑和類型檢查轉換。 例如,考慮from dataclasses import dataclass from typing_extensions import TypedDict @dataclass cla ...
[英]Validate Python TypedDict at runtime
我在 Python 3.8+ Django/Rest-Framework 環境中工作,在新代碼中強制執行類型,但基於大量無類型的遺留代碼和數據。 我們廣泛使用 TypedDicts 來確保我們生成的數據以正確的數據類型傳遞到我們的 TypeScript 前端。 MyPy/PyCharm/等。 在 ...
[英]Python typing append string to class members
假設我有一個類型提示的 class ,如下所示: 是否可以聲明一個類型,使其具有與Foo相同的屬性,但例如_a附加到 static 類型檢查器可見的末尾? IE 換句話說,我想單獨聲明Foo ,然后自動生成Bar Bar的任何變量都會在代碼完成期間出現one_a等。 來自 TypeScript 背 ...
[英]Initialize a TypedDict and fill keys & values later
我有一個字典,其中鍵和值的類型是固定的。 我想在TypedDict中定義類型如下:class MyTable(TypedDict): caption: List[str] header: List[str] table: pd.DataFrame epilogue: ...
[英]How to make a TypedDict with integer keys?
是否可以使用帶有 TypedDict 的 integer 鍵(類似於 dict?)。 嘗試一個簡單的例子: from typing import TypedDict class Moves(TypedDict): 0: int=1 1: int=2 ...