[英]How to make precise function annotation after Partial applied
給定一個 function:def foobar(foo: int, bar: str, spam: SpamService) -> str: return spam.serve(foo, bar) 這個 function,在外觀上類似於 FastAPI 端點,將兩個參數定義為“正 ...
[英]How to make precise function annotation after Partial applied
給定一個 function:def foobar(foo: int, bar: str, spam: SpamService) -> str: return spam.serve(foo, bar) 這個 function,在外觀上類似於 FastAPI 端點,將兩個參數定義為“正 ...
[英]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 ...
[英]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( ...
[英]How can I type hint a dictionary where the key is a specific tuple and the value is known?
如何鍵入提示字典,其中鍵是特定元組且值已知? 例如,我想像這樣輸入提示: 用例將從一組已知的基數到 go 到先前使用這些基數定義的 class。 ...
[英]How to add a key to each dictionary in a list of TypedDicts
問題 (Python 3.9) 我繼承了一些TypedDict ,如下:from typing import TypedDict class Base(TypedDict): key: str class Child(Base): extended: str 我有一個Base ...
[英]Why mypy is not throwing an error if interface and implementation has a different argument name
我今天在我們的代碼庫中發現了這個錯誤(簡化示例):from abc import ABC, abstractmethod class Interface(ABC): @abstractmethod def method(self, variable: str) -> str ...
[英]How to type hint a method that returns a subclass?
我有設置import typing class A: def get_b(self) -> B: # <- ERROR: B is not defined yet return b # <- and instance of B here class B( ...
[英]Python typing for a metaclass Singleton
我有一個 singleton 的 Python (3.8) 元類,如此處所示我試圖像這樣添加類型:from typing import Dict, Any, TypeVar, Type _T = TypeVar("_T", bound="Singleton") class Singleton ...
[英]Subclass overriding attribute with subtype of parent's attribute
class B: pass class InheritsB1(B): pass class InheritsB2(B): pass class A: prop: list[B] class InheritsA1(A): prop: list[I ...
[英]Missing type parameters for generic type "Callable"
向以下 function 添加類型提示的正確方法是什么?from typing import Callable def format_callback(f: Callable) -> Callable: """Function to wrap a function to use as ...
[英]Python Unit Test to Assert Type Annotation of Object
在 Python 版本 <3.11 中, assert_type ( source ) 不可用,如何通過單元測試TestCase unittest斷言類型注釋? 問題示例: 測試失敗並顯示以下 output: 我目前使用的方法如下: 這可行,但需要迭代整個object ,這對於較大的數據集來 ...
[英]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 ...
[英]Why I can't type hint that method takes instance attribute as argument?
我有這個 class,不管它做什么,所以這里是最小的例子:class DashboardMethods(BaseMethods): _time_templates = IntervalTemplates() async def get_kpi_for_interval(self, ...
[英]Python type hints for unpacking object
我正在嘗試為 object 解包實現類型提示。 這是我目前擁有的 印刷 但是,我希望 mypy 推斷出x 、 y ( int 、 str )的正確類型。 我怎樣才能做到這一點? ...
[英]Why are TypedDict types without field and with NotRequired field incompatible?
我正在嘗試創建一些函數,這些函數將返回不同 TypedDict 類型的值。 它們中的大多數字段都是相同的,所以我想在所有情況下生成具有相同 function 的基本字典。 但是我被正確輸入這個問題難住了。 我的想法是創建基本類型Parent並從中繼承,只添加NotRequired字段。 然而,這失 ...
[英]Get the main type out of a composite type in Python
假設我將類型定義為: 等等,如何通過分析兩種數據類型來獲得主要類型(如列表或集合)? 我試過了: 但它返回 False 任何的想法? ...
[英]Python PyDash 'find' function returns Any instead of passed data type
在我的案例中,邏輯要復雜得多,但歸結起來就是這樣。 我有一個數據類實例列表。 根據該列表,我正在進行搜索。 當我直接訪問實例時,屬性建議和類型提示起作用。 但是當我使用 PyDash(就像 JavaScript 中的 Lodash)的“find”方法時,無論內部傳遞什么,它都會返回“Any”類型。 ...
[英]How to enforce composed type hints in python?
有沒有辦法在 python 中強制執行組合類型提示? 我希望獲得以下行為: 請注意,問題在於list[int] 。 當我只使用def my_fn(a: list) -> None:時,代碼運行沒有任何錯誤。 我在這里和這里看到了相關問題,但它們並沒有讓我更接近。 ...
[英]Check if a type is Union type in Python
我定義了一個數據類: 我可以循環遍歷我的屬性類型: 但是我如何檢查'str'類型是否在'fieldtype'中或獲取聯合類型中的類型列表? ...