Skip to content

adding type annotation  #128

Open
Open

Description

@martinResearch

Thank you for the library.
Unfortunately, having this library not typed prevents mypy from propagating types to do efficient static checks.
Is adding type annotation in the roadmap?
Maybe the stubs in here https://github.com/python/typeshed/tree/main/stdlib/multiprocessing could be reused?

Activity

  1. mmckerns commented on Dec 24, 2022

    @mmckerns
    Member

    No, adding type annotations is not currently in the roadmap. Thanks for the pointer to multiprocessing from typeshed. I'll have to look into whether it's realistic to use that as a base as opposed to the stdlib multiprocessing.

  2. martinResearch commented on Dec 30, 2022

    @martinResearch
    Author

    To be sure there is no confusion: It seems to me that we cannot really oppose the code in here https://github.com/python/cpython/blob/main/Lib/multiprocessing with the code in here https://github.com/python/typeshed/tree/main/stdlib/multiprocessing. It seems that the first one contains the library implementation and does not have any type annotation while the second contains only the annotations and no implementation.

  3. mmckerns commented on Dec 30, 2022

    @mmckerns
    Member

    👍 there's no confusion

  4. Jylpah commented on Jan 24, 2023

    @Jylpah

    Second vote for type hints. Type hints and mypy make Python programming for more robust since the linter will notice many (most of) issues that would otherwise be runtime errors. But I can image it would be quite an effort to add.

  5. mcrespoae commented on May 30, 2024

    @mcrespoae

    Following up on this issue: Third vote for type hints.

  6. chdominguez commented on Jun 25, 2024

    @chdominguez

    As a workaround, you can cast multiprocess as multiprocessing for type annotations during development using the typing module:

    if typing.TYPE_CHECKING:
        import multiprocessing as mp
    else:
        import multiprocess as mp
    

    or

    import multiprocess as mp
    if typing.TYPE_CHECKING:
       import multiprocessing
       mp = typing.Cast(multiprocessing, mp)
    
  7. zhou13 commented on Dec 1, 2024

    @zhou13
  8. codethief commented on Jul 3, 2025

    @codethief

    @zhou13 Your stubs seem to import symbols from multiprocessing instead of multiprocess, though? E.g. in https://github.com/zhou13/multiprocess-stubs/blob/master/src/multiprocess/__init__.pyi you import Process from multiprocessing.context instead of multiprocess.context, meaning a type checker will validate usages of multiprocess.Process() against multiprocessing.context.Process, not multiprocess.context.Process, whcih seems equivalent to @chdominguez's workaround.

  9. codethief commented on Jul 3, 2025

    @codethief

    @chdominguez I believe in your second workaround there's a typo: Cast should be cast.

    Either way, for some weird reason it doesn't seem to work in Pylance/Pyright, unfortunately: In expressions like mp.Process() go-to-definition in VSCode will work (well, it will take you to multiprocessing.Process instead of multiprocess.Process) but Pylance will still underline Process and, likewise, Pyright will complain on the command line:

    foo.py:34:22 - error: "Process" is not a known attribute of module "multiprocess"
    

    Meanwhile, your first workaround works without issues.

  10. zhou13 commented on Jul 3, 2025

    @zhou13
  11. codethief commented on Aug 12, 2025

    @codethief

    @zhou13 My apologies for the delay, the notification about your message must have gotten burried.

    Do you still have issues using it?

    My point was that your stubs would probably be better if they referenced multiprocess instead of multiprocessing. (You can probably just run a search & replace.) Otherwise, like in @chdominguez's workaround, type checkers will validate usages against multiprocessing[0], and go-to-definition (ctrl+click) for e.g. multiprocess.Process will take you to the source code of multiprocessing.Process. (At least that's what happens for me in VSCode with Pylance.)

    [0]: Obviously, this is fine as long as the interfaces of multiprocess and multiprocessing are exactly the same.

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions