![](/img/trans.png)
[英]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 結構不變。 ...