Skip to content
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

add annotation for multiprocessing.{Value,Array} with special c-types #11833

Merged
merged 8 commits into from
Apr 25, 2024

Conversation

prauscher
Copy link
Contributor

This PR adds support for multiprocessing.Value with standard c-types like c_float. Before this PR, Code like

from multiprocessing import Value
from ctypes import c_float
a = Value(c_float, 0.0)
a.value = 1.2 

lead to incompatible types in assignment.

Also this PR resolves a Problem addressed in #4266 where

from multiprocessing import Array
from ctypes import c_char
a = Array(c_char, 40)
print(a.value)

lead to SynchronizedArray[c_char] has no attribute value, altrough documentation states that such arrays have a raw and a value field.

Happy for your comments, lets see what I missed :)

prauscher and others added 2 commits April 25, 2024 09:45

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
Comment on lines 87 to 89
def Value(self, typecode_or_type: _CT, *args: Any, lock: Literal[False]) -> Synchronized[_CT]: ...
@overload
def Value(self, typecode_or_type: _CT, *args: Any, lock: Literal[True] | _LockLike = True) -> Synchronized[_CT]: ...
Copy link
Collaborator

Choose a reason for hiding this comment

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

You removed the type "wrapper" here, while Array still has it, was this intentional?

Also, could you add some test cases to test_cases/stdlib? I think the multiprocessing stubs are sufficiently complex to warrant some.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this was intentional and a misstake, which is why i re-added them now. I agree with test-cases, let's see what I can come up with.

This comment has been minimized.

This comment has been minimized.

…hronizedArray is not ready yet

This comment has been minimized.

prauscher and others added 2 commits April 25, 2024 10:39
@prauscher
Copy link
Contributor Author

What I could not get working:

from multiprocessing import Array
from ctypes import c_float
a = Array(c_float, 2)
a[:] = [1.2, 4.1]  # Incompatible types: expect c_float, found float

Sadly, adding a new overload for this like it is done with Value breaks, so this requires a bit more thinking

This comment has been minimized.

Copy link
Collaborator

@srittau srittau left a comment

Choose a reason for hiding this comment

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

I suggest to add the examples from your initial comment to the tests, see below.

Copy link
Contributor

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

@srittau srittau merged commit 71f020d into python:main Apr 25, 2024
54 checks passed
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.

None yet

2 participants