pycharm - 在Python3.6(尤其是PyCharm)中输入同质队列的正确方法是什么?

标签 pycharm python-3.6 type-hinting mypy

我正在用 Python 3.6 编写一个分形生成器,并使用 multiprocessing.Queue 将消息从主线程传递给工作线程。这是我到目前为止所尝试过的,但 PyCharm 似乎无法推断从队列中获取的项目的属性类型:

from typing import NamedTuple, Any, Generic, TypeVar, Tuple
from multiprocessing import Process, Queue

T = TypeVar()


class Message(NamedTuple):
    method: str
    id: str
    data: Any = None


class TypedQueue(Generic[T]):
    def get(self) -> T:
        ...
    def put(self, m: T) -> None:
        ...


MessageQ = TypedQueue[Message]


class FractalWorker(Process):
    def __init__(self, work: MessageQ, results: MessageQ)
        super().__init__()
        self.work = work
        self.results = results

    @staticmethod
    def make_queues() -> Tuple[MessageQ, MessageQ]:
        work = cast(MessageQ, Queue())
        results = cast(MessageQ, Queue())
        return work, results

我希望 PyCharm 能够知道 self.work.get 结果的属性具有由 Message 类指定的类型。我还想知道是否有与此类似的类型提示队列的标准方法。

最佳答案

老问题,但我刚刚发现

P: "Queue[Path]" = Queue()

在 PyCharm 中使用 queue.Queuemultiprocessing.Queue

关于pycharm - 在Python3.6(尤其是PyCharm)中输入同质队列的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48956422/

相关文章:

python - 如何在pycharm中安装和配置httplib2?

python - 无法在 Windows 中为 Python 3.x 安装 Pillow - 需要 Zlib

python - 在 PyCharm 中使用 VPython

multiprocessing - 多处理命令列表

python - 避免可排序元素列表的类型警告

从其他函数注释派生的 Python 类型提示

python - 如何将 PyCharm 与 PySpark 链接?

python - 使用 python3 计算列表中多个整数的幂的最佳方法是什么?

python - Python 3.6 中的通用 NamedTuple

python - 如何指定返回类型与输入类型相同?