
[英]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'中或获取联合类型中的类型列表? ...