Describe the bug
Creating an object of a multiprocessing Queue using multiprocessing.Queue() gives "Expected class type but received "(maxsize: int = ...) -> Queue[Unknown]"" and similar errors everywhere the queue object is used.
To Reproduce
Create a method that takes a multiprocessing Queue as argument and run pyright over it (see code).
Expected behavior
If possible, Pyright should understand that multiprocessing.Queue is a correct type as described here.
Screenshots or Code
import multiprocessing
def run(self, multiprocessing.Queue):
pass
EDIT: didn't check this example, here is a correct one:
import multiprocessing
def run(q : multiprocessing.Queue):
pass
complains in the method argument:
Expected class type but received "(maxsize: int = ...) -> Queue[Unknown]"
VS Code extension or command-line
VS Code extension version v1.1.55
Additional context
Possibly related.
Is there any solution other than type: ignore on every multiprocessing queue line? Thanks
I quickly figured out that:
from multiprocessing import Queue
def run(self, Queue):
pass
does not produce the error in pyright.
Where did I go wrong in the original example?
I'm not sure what you're trying to do here. In your first example, it looks like you're trying to define a function called run that has two parameters. The first parameter is named self and the second one is named multiprocessing.Queue, which is an illegal name for a parameter. In the second example, you provided a parameter name of Queue, which is legal, but I don't think it's doing what you intended here.
Yeah, I think you meant:
import multiprocessing
def run(self, queue: multiprocessing.Queue):
pass
# or
from multiprocessing import Queue
def run(self, queue: Queue):
pass
But I'm assuming with the self parameter that this is appearing in a class. Either way, you have to name the parameter then optionally give its type annotation.
Sorry about the self parameter, it was left over from the class that originally produced the problem. Just the plain vanilla:
import multiprocessing
def run(q : multiprocessing.Queue):
pass
gives me the error.
I don't see any error with that last sample you provided, at least in the basic type checking mode. Can you paste the exact error that you're receiving? Are you sure it's coming from Pyright and not some other linter? (You should see "Pyright" in light gray next to the error.)
If I enable strict type checking, Pyright complains that the type is partially unknown, since you haven't provided a type argument to the generic type Queue. (In other words, you've specified that it's a Queue, but you haven't specified the type of the items in the Queue.)
At the top of this issue, you said:
Creating an object of a multiprocessing Queue using multiprocessing.Queue()
But your code sample doesn't instantiate a queue. Could you provide the portion of the code that is generating the error?
Thanks for your quick replies. Here is an example that instantiates a queue:
import multiprocessing
def run(q : multiprocessing.Queue):
pass
my_q = multiprocessing.Queue()
run(my_q)
I forgot to mention this is Python 3.7.4, if relevant.
Here is a screenshot of the actual error in VS Code, with pyright in gray.

It looks like the type being used for the function argument is the signature of the constructor for Queue and not Queue itself, which seems like a bug.
OK, thanks for the additional details. I'm able to repro it now. This is a bug in the typeshed stubs for multiprocessing. It was recently fixed in the typeshed repo. I just updated the typeshed stubs in Pyright yesterday, and it now works as you would expect. So this bug will be fixed when I release the next version of Pyright.
@jakebailey, you'll want to update the stubs for Pylance as well.
Thanks for you quick replies. Looking forward then to the new version of this great tool!
Should be fixed in Pylance in the next release.
This is addressed in Pyright 1.1.56, which I just published.
Most helpful comment
OK, thanks for the additional details. I'm able to repro it now. This is a bug in the typeshed stubs for multiprocessing. It was recently fixed in the typeshed repo. I just updated the typeshed stubs in Pyright yesterday, and it now works as you would expect. So this bug will be fixed when I release the next version of Pyright.
@jakebailey, you'll want to update the stubs for Pylance as well.