Skip to content

Deriving from multiprocessing.get_context().Process #9860

Closed as not planned
Closed as not planned

Description

@marcelm

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]]

Activity

  1. AlexWaygood commented on Mar 9, 2023

    @AlexWaygood
    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 :/

  2. AlexWaygood commented on Mar 9, 2023

    @AlexWaygood
    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
  3. marcelm commented on Mar 9, 2023

    @marcelm
    Author

    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.

  4. AlexWaygood commented on Mar 9, 2023

    @AlexWaygood
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions