cost 311 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-2024 STACKOOM.COM