cost 259 ms
枚舉 class 實例的屬性值的類型注釋

[英]Type annotation for values of attributes of an Enum class instance

在 python 中,我有以下枚舉 class 的實例: 我有一個 function,它采用此 class 的屬性值,即Colour.Red.value 、 Colour.Blue.value或Colour.Green.value之一: 在這種情況下,如何為input_str定義類型注釋? Pyl ...

使用 Python 中的類型模塊為具有多個返回類型和條件的函數創建泛型類型提示的正確方法是什么?

[英]What is the correct way to create a generic type hint for functions with multiple return types and conditions using the typing module in Python?

在使用pyright工具進行類型檢查時,我遇到了以下問題。 (Python 3.11,版權 1.1.310) 我的代碼:class TypeCast(str, enum.Enum): STRING = str FLOAT = float def cast_int_to_str( ...

Python 為元類打字 Singleton

[英]Python typing for a metaclass Singleton

我有一個 singleton 的 Python (3.8) 元類,如此處所示我試圖像這樣添加類型:from typing import Dict, Any, TypeVar, Type _T = TypeVar("_T", bound="Singleton") class Singleton ...

Python 斷言類型注釋的單元測試 Object

[英]Python Unit Test to Assert Type Annotation of Object

在 Python 版本 <3.11 中, assert_type ( source ) 不可用,如何通過單元測試TestCase unittest斷言類型注釋? 問題示例: 測試失敗並顯示以下 output: 我目前使用的方法如下: 這可行,但需要迭代整個object ,這對於較大的數據集來 ...

似乎是 Pylance 類型檢查中的錯誤

[英]Seems like a bug in Pylance type checking

我得到了一個奇怪的錯誤, list[dict[str, str | int]] list[dict[str, str | int]]不能分配給Sequence[dict[str, str | float | int] | None] Sequence[dict[str, str | float | ...

如何注釋返回“屬性”的裝飾器

[英]How to annotate a decorator that returns a `property`

我想創建一個返回裝飾 function property的裝飾器,即:from typing import TYPE_CHECKING def make_prop(param): def wrapper(func) -> 'property(func)': retu ...

Python 解壓類型提示 object

[英]Python type hints for unpacking object

我正在嘗試為 object 解包實現類型提示。 這是我目前擁有的 印刷 但是,我希望 mypy 推斷出x 、 y ( int 、 str )的正確類型。 我怎樣才能做到這一點? ...

為什么不帶字段和帶 NotRequired 字段的 TypedDict 類型不兼容?

[英]Why are TypedDict types without field and with NotRequired field incompatible?

我正在嘗試創建一些函數,這些函數將返回不同 TypedDict 類型的值。 它們中的大多數字段都是相同的,所以我想在所有情況下生成具有相同 function 的基本字典。 但是我被正確輸入這個問題難住了。 我的想法是創建基本類型Parent並從中繼承,只添加NotRequired字段。 然而,這失 ...

Python PyDash 'find' function 返回 Any 而不是傳遞的數據類型

[英]Python PyDash 'find' function returns Any instead of passed data type

在我的案例中,邏輯要復雜得多,但歸結起來就是這樣。 我有一個數據類實例列表。 根據該列表,我正在進行搜索。 當我直接訪問實例時,屬性建議和類型提示起作用。 但是當我使用 PyDash(就像 JavaScript 中的 Lodash)的“find”方法時,無論內部傳遞什么,它都會返回“Any”類型。 ...

如何在 python 中強制執行組合類型提示?

[英]How to enforce composed type hints in python?

有沒有辦法在 python 中強制執行組合類型提示? 我希望獲得以下行為: 請注意,問題在於list[int] 。 當我只使用def my_fn(a: list) -> None:時,代碼運行沒有任何錯誤。 我在這里和這里看到了相關問題,但它們並沒有讓我更接近。 ...


 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM