Skip to content

Recent multiprocessing.Queue change breaks existing annotations and reduces typing precision #4313

Closed
@JukkaL

Description

@JukkaL

#4289 changed multiprocessing.Queue from a class to a function. This matches the implementation, but this has some possibly unanticipated side effects, as discussed in python/mypy#9100. Here's a summary of the points raised in the linked PR:

  • multiprocessing.Queue is documented as a class: https://docs.python.org/3/library/multiprocessing.html#multiprocessing.Queue

  • multiprocessing.queues.Queue can be used in annotations, but it seems to be undocumented. For a user to annotate code using queues, they need to either read the stubs or the stdlib implementation, which is unfortunate.

  • The return type of multiprocessing.Queue is Queue[Any], so the change loses some typing precision, even if a user can figure out the correct type. Since multiprocessing.queues.Queue is not very discoverable (as it's undocumented), for many new users this may effectively remove the ability to type check queue items.

  • The change breaks existing code that uses multiprocessing.Queue as a type, without an obvious fix.

The above considerations probably explain why multiprocessing.Queue used to be defined as a class, even thought it's not quite true.

I'm not sure what's the best way forward. Here are some ideas:

  • Revert the change to Queue and other similar types (at least for now).
  • Teach type checkers to suggest using multiprocessing.queues.Queue (etc.) in type annotations. However, if these are undocumented, maybe we shouldn't recommend these?
  • Change the return type of multiprocessing.Queue to Queue[T] to preserve type checking precision (this may not work on all type checkers).

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