cost 71 ms
Python:类型提示字典映射类型到函数

[英]Python: Type hinting a dictionary mapping types to functions

我正在尝试创建一个字典,将输入数据类型的元组映射到 function 以处理这些数据类型。 我一直没能找到一种方法来以满足 mypy 的方式对其进行类型提示。 在我禁用文件的 static 类型检查之前,我想我会问...... 这是一个 MWE: 它按预期工作,但mypy给出错误 其他条目也是如此。 ...

遍历 arguments 列表时 mypy 的类型提示问题

[英]Type-hinting problem with mypy when iterating through a list of arguments

这是一个 MRE:def test(a: int | tuple[int, int]): print(a) if __name__ == "__main__": for b in [1, (1, 2)]: test(b) mypy 输出: 7: error: ...

2023-02-01 15:01:30   1   30    python / mypy  
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 类型:记录级别的文字作为类型?

[英]python types: Literal of logging level as type?

以下代码:import logging print(type(1)) print(type(logging.WARNING)) 印刷: 然而,根据 mypy,此代码片段的第一行是合法的,但第二行不是(变量“logging.WARNING”作为类型无效):OneOrTwo = Literal[1 ...

键入包含 unsignedinteger 的 numpy.ndarray 的提示

[英]Type hint for numpy.ndarray containing unsignedinteger

我有一个包含 unsignedinteger 的 numpy 数组,如下所示: 此arr将输入到 function。我想知道 function 参数的类型提示应该是什么样子? 我首先认为它应该是arr: np.ndarray 。 但随后mypy抱怨了。 Argument "arr" to "myfu ...

类型检查错误:views.py:24: error: "HttpRequest" has no attribute "tenant"

[英]Type checking error: views.py:24: error: "HttpRequest" has no attribute "tenant"

我正在创建一个多租户的 Django 应用程序。 我使用的自定义中间件将tenant object 附加到请求。 我的问题是在类型检查时,我的视图不知道HttpRequest class 上的额外属性。 我尝试创建一个扩展HttpRequest并添加租户属性的TenantHttpClass 。 编 ...

Python 解压类型提示 object

[英]Python type hints for unpacking object

我正在尝试为 object 解包实现类型提示。 这是我目前拥有的 印刷 但是,我希望 mypy 推断出x 、 y ( int 、 str )的正确类型。 我怎样才能做到这一点? ...

Python 带可选变量的模式匹配

[英]Python Pattern Matching with optional variables

我正在使用 mypy,我有两个类: 我有一些 db function 可以获取数据,最后得到这两个变量: 我想对两个变量的 None 或非 None 数据的每种情况进行模式匹配。 我想为每种情况做不同的事情,这样: 但是在第二种情况下,mypy 不会将 student 和 city 识别为非 Non ...

歧视联盟 Python

[英]Discriminated union in Python

赏金将在 6 天后到期。 此问题的答案有资格获得+400声望赏金。 mnowotka想让更多人关注这个问题: 我希望赏金获得者将提供一个尊重问题中提供的约束的解决方案。 具体来说,“为什么你做的完全不同”的答案将不会被接受,除非他们清楚地展示了一种替代方法,并提供了代码示例和关于优缺点的讨论。 ...

为什么不带字段和带 NotRequired 字段的 TypedDict 类型不兼容?

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

我正在尝试创建一些函数,这些函数将返回不同 TypedDict 类型的值。 它们中的大多数字段都是相同的,所以我想在所有情况下生成具有相同 function 的基本字典。 但是我被正确输入这个问题难住了。 我的想法是创建基本类型Parent并从中继承,只添加NotRequired字段。 然而,这失 ...

我应该如何类型提示调用另一个 class 方法来构造 class 实例的通用 class 方法?

[英]How should I type-hint a generic class method that calls another class method to construct an instance of the class?

我试图找出几个抽象类的类型提示,我想将它们用作类的基类, create function。具体来说,这是用于反序列化类型。 我的简单示例如下所示from abc import ABC, abstractmethod from typing import Type, TypeVar T = Ty ...

嵌套协议的使用(协议成员也是协议)

[英]Usage of nested protocol (member of protocol is also a protocol)

考虑一个 Python 协议属性,它也用协议注释。 我发现在那种情况下,即使我的自定义数据类型遵循嵌套协议,mypy 和 Pyright 也会报告错误。 例如,在下面的代码中, Outer遵循HasHasA协议,因为它具有hasa: HasA因为Inner遵循HasA协议。from datacla ...


 
粤ICP备18138465号  © 2020-2023 STACKOOM.COM