Skip to content

Fix use of unexported Pool type in multiprocessing.pool - #6111

Merged
hauntsaninja merged 1 commit into
python:masterfrom
jakebailey:multiprocessing-pool-1
Oct 5, 2021
Merged

hauntsaninja merged 1 commit into
python:masterfrom
jakebailey:multiprocessing-pool-1

Conversation

@jakebailey

Copy link
Copy Markdown
Contributor

For #6110.

pool isn't exported from multiprocessing. Import Pool from multiprocessing.pool directly rather than relying on the side effect of it being loaded by multiprocessing at some point.

@hauntsaninja hauntsaninja left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I vaguely prefer import multiprocessing.pool as the solution, since it's one less lookup for anyone wondering what _Pool is.

@jakebailey

jakebailey commented Oct 4, 2021 •

Copy link
Copy Markdown
Contributor Author

It could be multiprocessing.pool, but only if multiprocessing always has pool, which it doesn't seem to. If it works, it's due to side effects of other imports that happen to make it work.

>>> import multiprocessing
>>> multiprocessing.pool
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'multiprocessing' has no attribute 'pool'

@github-actions

github-actions Bot commented Oct 4, 2021

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@hauntsaninja

Copy link
Copy Markdown
Collaborator

Yes, understood, I meant literally import multiprocessing.pool. So the file would look something like:

import ctypes
import multiprocessing
import multiprocessing.pool
import sys
...
def ... -> multiprocessing.pool.Pool: ...

Anyway was just a nit, thanks for fixing this :-)

@jakebailey

Copy link
Copy Markdown
Contributor Author

Ah, I see. I can do that.

@jakebailey

Copy link
Copy Markdown
Contributor Author

Hm, doing so doesn't make pyright happy. That seems like a bug. I'd rather merge this for now to unblock bumping 1.1.175 and then investigate that.

@Akuli

Akuli commented Oct 5, 2021

Copy link
Copy Markdown
Collaborator

At runtime, if any module anywhere in the program imports anything from multiprocessing.pool, then all modules that import multiprocessing will also be able to access multiprocessing.pool. But import multiprocessing.context does not affect whether multiprocessing.pool is available or not. It would if the implementation of multiprocessing.context also imported multiprocessing.pool, but it doesn't. On the other hand, the stub of multiprocessing.context needs to import multiprocessing.pool.

I had the same problem with tkinter stubs: because tkinter has functions that accept a tkinter.font.Font object, I need to import tkinter.font into tkinter/__init__.pyi, which makes mypy think that import tkinter also loads tkinter.font. That's not how it works at runtime, because tkinter/__init__.py does not import anything from tkinter/font.py. I didn't find any workaround for this.

@jakebailey

jakebailey commented Oct 5, 2021 •

Copy link
Copy Markdown
Contributor Author

FWIW pyright/pylance make no attempt to simulate these sorts of side effects; our old language server sort of did (to work around libraries which had been poorly structured or not explicit), and from the above mypy must too.

Originally, I was pretty pro-side effect simulation, but having had the exact opposite for well over a year now, I think it's worth avoiding the complexity (and preserving the stub authorship power of explicit exports). It'd be nice to get everything consistent on this front, though.

@JelleZijlstra

Copy link
Copy Markdown
Member

I appreciate pyright's behavior here. At work we had a few issues recently where a module was imported as a side effect by some unrelated module, and then when we removed that import, tests elsewhere in the codebase started failing because they relied on the import elsewhere.

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

Successfully merging this pull request may close these issues.

4 participants