Skip to content

Deriving from multiprocessing.get_context().Process #9860

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
marcelm opened this issue Mar 9, 2023 · 4 comments
Closed

Deriving from multiprocessing.get_context().Process #9860

marcelm opened this issue Mar 9, 2023 · 4 comments

Comments

@marcelm
Copy link

marcelm commented Mar 9, 2023

This code:

import multiprocessing

context = multiprocessing.get_context()

class MyProcess(context.Process):
    pass

fails to typecheck with the message error: Name "context.Process" is not defined [name-defined].

I tried to see whether I could contribute a PR, but have to admit I don’t even understand the error message. According to reveal_type, the context variable is of type multiprocessing.context.DefaultContext, but that class appears to have a Process attribute:

class DefaultContext(BaseContext):
Process: ClassVar[type[Process]]

@AlexWaygood
Copy link
Member

Mypy's error message here is terrible, but it has a legitimate complaint: you're using an object annotated as a dynamic variable (Process) as a base class. That's problematic from a type-checking point of view.

This is the same issue as:

I'll check later to see if there's anything we can do to improve things, but I believe the stubs are probably correct here, in which case there's probably not much we can do here :/

@AlexWaygood
Copy link
Member

Yes, there's nothing typeshed can do here to fix the error mypy emits on your current code :/

I wondered if changing the annotation here to Process: TypeAlias = Process would help us at all, but it doesn't look like it:

Process: ClassVar[type[Process]]

The mypy error message is terrible (and the error itself is probably a pain to work around), but those are issues for mypy, not typeshed.

I guess my best suggestion is to do an if TYPE_CHECKING hack, something like the below:

import multiprocessing
import multiprocessing.context
import typing

context = multiprocessing.get_context()

if typing.TYPE_CHECKING:
    class MyProcess(multiprocessing.context.Process):
        pass
else:
    class MyProcess(context.Process):
        pass

@AlexWaygood AlexWaygood closed this as not planned Won't fix, can't repro, duplicate, stale Mar 9, 2023
@marcelm
Copy link
Author

marcelm commented Mar 9, 2023

Thanks! I understand now where the error message comes from. I’ll add a comment to the linked MyPy issue. Naively, this appears to be something that should work (considering this is code that works and that is even in the stdlib).

Thanks also for the workaround; I would not have come up with it myself so quickly.

@AlexWaygood
Copy link
Member

I gave a little bit more context here FYI, if that's helpful for understanding why mypy emits this error here!

pallets-eco/flask-sqlalchemy#1112 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants