
[英]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给出错误 其他条目也是如此。 ...
[英]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: ...
[英]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 ...
[英]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 ...
[英]python types: Literal of logging level as type?
以下代码:import logging print(type(1)) print(type(logging.WARNING)) 印刷: 然而,根据 mypy,此代码片段的第一行是合法的,但第二行不是(变量“logging.WARNING”作为类型无效):OneOrTwo = Literal[1 ...
[英]Type hint for numpy.ndarray containing unsignedinteger
我有一个包含 unsignedinteger 的 numpy 数组,如下所示: 此arr将输入到 function。我想知道 function 参数的类型提示应该是什么样子? 我首先认为它应该是arr: np.ndarray 。 但随后mypy抱怨了。 Argument "arr" to "myfu ...
[英]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 ...
[英]Narrower types for pytest fixtures that perform actions without returning objects?
考虑像这样的夹具@pytest.fixture def mock_database(monkeypatch: MonkeyPatch) -> None: ... 它在测试中使用def test_with_mock_database(mock_database: None) -> ...
[英]Type checking error: views.py:24: error: "HttpRequest" has no attribute "tenant"
我正在创建一个多租户的 Django 应用程序。 我使用的自定义中间件将tenant object 附加到请求。 我的问题是在类型检查时,我的视图不知道HttpRequest class 上的额外属性。 我尝试创建一个扩展HttpRequest并添加租户属性的TenantHttpClass 。 编 ...
[英]Python type hints for unpacking object
我正在尝试为 object 解包实现类型提示。 这是我目前拥有的 印刷 但是,我希望 mypy 推断出x 、 y ( int 、 str )的正确类型。 我怎样才能做到这一点? ...
[英]Python Pattern Matching with optional variables
我正在使用 mypy,我有两个类: 我有一些 db function 可以获取数据,最后得到这两个变量: 我想对两个变量的 None 或非 None 数据的每种情况进行模式匹配。 我想为每种情况做不同的事情,这样: 但是在第二种情况下,mypy 不会将 student 和 city 识别为非 Non ...
[英]Discriminated union in Python
赏金将在 6 天后到期。 此问题的答案有资格获得+400声望赏金。 mnowotka想让更多人关注这个问题: 我希望赏金获得者将提供一个尊重问题中提供的约束的解决方案。 具体来说,“为什么你做的完全不同”的答案将不会被接受,除非他们清楚地展示了一种替代方法,并提供了代码示例和关于优缺点的讨论。 ...
[英]Why are TypedDict types without field and with NotRequired field incompatible?
我正在尝试创建一些函数,这些函数将返回不同 TypedDict 类型的值。 它们中的大多数字段都是相同的,所以我想在所有情况下生成具有相同 function 的基本字典。 但是我被正确输入这个问题难住了。 我的想法是创建基本类型Parent并从中继承,只添加NotRequired字段。 然而,这失 ...
[英]mypy "Incompatible return value type" with dict comprehension and isinstance
我的 mypy 出现问题,出现以下错误: Incompatible return value type (got "Dict[str, Pin]", expected "Union[Dict[str, Input], Dict[str, Output]]") [return-value]mypy ...
[英]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 ...
[英]Overriding a method, mypy throws an "incompatible with super type" error when changing return type to child class type
对于以下代码:class Person: number: int def __init__(self) -> None: self.number = 0 def __add__(self, other: 'Person') -> 'Person ...
[英]Usage of nested protocol (member of protocol is also a protocol)
考虑一个 Python 协议属性,它也用协议注释。 我发现在那种情况下,即使我的自定义数据类型遵循嵌套协议,mypy 和 Pyright 也会报告错误。 例如,在下面的代码中, Outer遵循HasHasA协议,因为它具有hasa: HasA因为Inner遵循HasA协议。from datacla ...
[英]Mypy error Item "None" of "Optional[Dict[str, str]]" has no attribute "get"
下面的代码运行良好 - 但是当我在上面运行 mypy 时,出现以下错误 我错过了什么吗? 我想保持 input_list 结构不变。 ...