Is multiprocessing.Queue generic or not (pyright)?

140 views Asked by At

I started using pyright recently (over mypy) and it's finding a lot of issues that weren't coming up with mypy. For example

from multiprocessing import Queue

queue: Queue = Queue()  # Type of "queue" is "Queue[Unknown]" [reportUnknownVariableType]

This is surprising to me because multiprocessing.Queue doesn't appear to have a generic type argument when I inspect the source (python 3.9). Is this pyright making standard library types generic as a "feature" or am I just wrong about Queue somehow?

1

There are 1 answers

5
Egeau On BEST ANSWER

This is not a pyright feature and you're not wrong about how Queue works, rather, it's the result of the typeshed of multiprocessing.Queue having a generic argument, but the standard library implementation not having it.

In general type checkers will look at the .pyi files over the .py files, and use those for any information. Since the standard library does not have type hints in any of the .py files, it is then expected that the generic argument only shows up in the typeshed.

Because the standard library implementation of Queue does not have this generic argument, you need a from __future__ import annotations import to be able to use the Queue[str] from typeshed. The exact error you're currently getting is the type checker reporting that you did not do that, and kept it as an unspecified Queue instead. This is the same error you would get if you annotated a function with List

Lastly, as pointed out in the comments to your post, this not working properly with pyright (e.g. it giving a different error when you typehint with just Queue) is (or at least was, it is said to be fixed now) indeed a bug, but I don't think that is related to your issue.