
[英]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 端点,将两个参数定义为“正 ...
[英]Python: Type hinting a dictionary mapping types to functions
我正在尝试创建一个字典,将输入数据类型的元组映射到 function 以处理这些数据类型。 我一直没能找到一种方法来以满足 mypy 的方式对其进行类型提示。 在我禁用文件的 static 类型检查之前,我想我会问...... 这是一个 MWE: 它按预期工作,但mypy给出错误 其他条目也是如此。 ...
[英]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 ...
[英]Is it possible to have pixel hinting on vector graphics of an unknown size on a webpage?
屏幕字体中的字形通常使用提示将形状与屏幕像素对齐,因此结果具有锐利的边缘。 我可以对网页上的任意矢量图形做类似的事情吗? 我知道我可以将矢量图形中的线条与像素对齐,但这仅适用于默认大小及其 integer 的倍数。 我的想法是,该图形将具有类似于字体中使用的暗示,以在所有尺寸下都具有锐利的边缘。 ...
[英]Polymorphism in Callablle under python type checking (Pylance)
对于我的代码,我有一个聚合 class 需要为基类 class BaseC的每个子类定义验证方法,在本例中InheritC继承自BaseC 。 然后通过注册方法将验证方法传递到聚合 class 中。 请参阅以下简单示例 我在用于注册 function 的参数上添加了类型提示,这是一个 Callab ...
[英]How to enforce composed type hints in python?
有没有办法在 python 中强制执行组合类型提示? 我希望获得以下行为: 请注意,问题在于list[int] 。 当我只使用def my_fn(a: list) -> None:时,代码运行没有任何错误。 我在这里和这里看到了相关问题,但它们并没有让我更接近。 ...
[英]Type hints for the generic utility in the Django
这里的类型提示是什么——因为 function 将动态获取 Model 实例。 ...
[英]How to type hint a function, added to class by class decorator in Python
我有一个 class 装饰器,它为装饰的 class 添加了一些函数和字段。@mydecorator @dataclass class A: a: str = "" 添加(通过setattr() )是一个.save() function 和一组数据类字段的信息作为单独的字典。 我希望 V ...
[英]Typehinting async function and passing to asyncio.create_task
在我的研究中,我发现对于输入异步 function 的正确方式的普遍共识是Callable[..., Awaitable[Any]] 。 在 Pycharm 中,我尝试了这个并在传递给asyncio.create_task时遇到了这个问题import asyncio from typing imp ...
[英]type hint for generic attribute inside abstract class
我有一个复杂的类型问题,我无法解决。 假设我有一些抽象类和一个等效于此的泛型类型from abc import ABC from typing import Generic, TypeVar class Action(ABC): pass ActionTypeVar = TypeVa ...
[英]Specify return type of a wrapper function that calls an abstract method in Python
对于此示例,请考虑Solver将返回Solution的简化场景。 我们有Solution :class Solution(ABC): pass class AnalyticalSolution(Solution): pass class NumericalSolution(S ...
[英]Python type hinting for generic container constructor
下面标记的正确类型是什么???,我们将通用可迭代数据容器类型转换为不同类型的可迭代容器? ...
[英]Python Type Hint for Returning Concrete Class
我有一些代码(附在下面)与 Python 3.10 一起运行。 代码运行良好,但 VS Code 中的 pylance 标记了这些行的错误:books: list[SoftBack] = [softback_book_1, softback_book_2] processed_books = Bo ...
[英]Implement GenericAlias in a user-defined class (Python)
例如,从 PEP 585 可以指定列表中元素的类型: list[int]是GenericAlias类型,等同于GenericAlias(list, int) 。 我想在自定义 class 中实现此功能。例如: 如果我创建一个继承自列表的 class,它会起作用,所以我猜这是由于一个属性。 我已经 ...
[英]Type annotations for full class hierarchy in Python
假设我们有以下代码:class Base: a: int class Derived(Base): b: int print(Derived.__annotations__) 在 python 的所有最新版本中运行此脚本将打印{'b': <class 'int'>} ...
[英]How do I read extended annotations from Annotated?
PEP 593通过Annotated添加了扩展注释。 但是 PEP 和Annotated的文档都没有描述我们应该如何访问底层注释。 我们应该如何读取Annotated object 中存储的额外注释?from typing import Annotated class Foo: bar ...
[英]Require a tuple with a certain length in python function parameters
我正在写一个 function 需要接受一个有序的坐标对(用一个元组表示)。 问题是我不知道如何要求它在 function 的参数中恰好有两个元素。有没有更好的方法来处理这种情况(比如使用更好的数据类型)? 我想不出任何事。 ...
[英]How to properly specify argument type accepting dictionary values?
这里有几个功能:from typing import Sequence def avg(vals: Sequence[float]): return sum(val for val in vals) / len(vals) def foo(): the_dict = {'a': ...
[英]Difference between Callable and FunctionType
我正在尝试正确键入提示我的代码并遇到Callable和FunctionTypefrom typing import Callable def my_func() -> Callable: f = lambda: _ return f result = my_func() ty ...
[英]How to type only the first positional parameter of a Protocol method and let the others be untyped?
问题如何只键入协议方法的第一个位置参数并让其他人取消类型? 例如,有一个名为MyProtocol的协议,它有一个名为my_method的方法,只需要第一个位置参数是一个 int,同时让 rest 是无类型的。 以下 class 将正确无误地实现它:class Imp1(MyProtocol): ...