Skip to content

multiprocessing Pool (and context manager) fixes/improvements #1562

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

Merged
merged 5 commits into from
Aug 29, 2017

Conversation

OddBloke
Copy link
Contributor

This fixes #1559.

Daniel Watkins added 5 commits August 18, 2017 18:24
Prior to this commit, the types for __enter__ and __exit__ were not
fully defined; this addresses that.
This is where the class is actually defined in the stdlib.
This ensures that:

```py
class MyPool(Pool):
    def my_method(self): pass

with MyPool() as pool:
    pool.my_method()
```

type-checks correctly.
And also add comments to note that it should have an identical signature
to multiprocessing.context.BaseContext.Pool (because it is just that
method partially applied).
@OddBloke
Copy link
Contributor Author

The example in #1559:

from multiprocessing import Pool  # Factory function
from multiprocessing import pool  # Submodule


def test(p: Pool) -> None:
    print(type(p))


test(pool.ThreadPool())


try:
    class MyPool(Pool): ...
except:
    print('Cannot subclass')

class MyRealPool(pool.Pool): ...

test(MyRealPool())

now renders:

1559-0.py:5: error: Invalid type "multiprocessing.Pool"
1559-0.py:6: error: No overload variant of "type" matches argument types [Pool?]
1559-0.py:13: error: Invalid type "multiprocessing.Pool"
1559-0.py:13: error: Invalid base class

Once the signature of test is fixed to def test(p: pool.Pool) -> None: as it now should be, we get:

1559-0.py:13: error: Invalid type "multiprocessing.Pool"
1559-0.py:13: error: Invalid base class

which matches what we would expect to happen if we tried to subclass a function/method.

@JelleZijlstra
Copy link
Member

Thanks for the detailed writeup!

@JelleZijlstra JelleZijlstra merged commit 85a788d into python:master Aug 29, 2017
@OddBloke OddBloke deleted the multiprocessing branch August 29, 2017 17:38
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.

multiprocessing.Pool mis-implemented
2 participants