Skip to content

[multiprocessing] Fix return types of Value and Array - #16485

Merged
srittau merged 7 commits into
python:mainfrom
anistark:fix-multiprocessing-value-types
Nov 15, 2026
Merged

srittau merged 7 commits into
python:mainfrom
anistark:fix-multiprocessing-value-types

Conversation

@anistark

@anistark anistark commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

Closes #10319.

Make multiprocessing.Value/Array and their multiprocessing.sharedctypes counterparts match the runtime: they return the raw ctypes object (or ctypes.Array) for lock=False, Synchronized for simple C types and typecode strings, and SynchronizedBase for other ctypes such as structures. See Value/Array and synchronized().

Prepared with Claude Code.

The stubs did not match what CPython returns:

- With `lock=False`, `Value` and `Array` return the raw ctypes object.
  `BaseContext.Value` claimed `Synchronized[_CT]` instead of `_CT`, and
  both `Array` stubs returned something other than `ctypes.Array[_CT]`.
- `sharedctypes.Value` returned `SynchronizedBase` for simple C types
  and typecode strings, hiding the `value` attribute that the runtime
  `Synchronized` wrapper provides.
- `BaseContext.Value` returned `Synchronized[_CT]` for other ctypes
  such as structures. These are wrapped in a `SynchronizedBase`
  subclass that has no `value` attribute.

Make both modules match the runtime and each other.

Fixes python#10319
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@srittau srittau 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.

Please remove the tests. In typeshed, we only add regression tests for functions and classes which are known to have caused complex problems in the past, or where stubs are difficult to get right. 100% test coverage for typeshed is neither necessary nor desirable, as it would lead to code duplication.

See tests/REGRESSION.md for more information.

self, typecode_or_type: type[_SimpleCData[_T]], size_or_initializer: int | Sequence[Any], *, lock: Literal[False]
) -> SynchronizedArray[_T]: ...
self, typecode_or_type: type[_CT], size_or_initializer: int | Sequence[Any], *, lock: Literal[False]
) -> ctypes.Array[_CT]: ...

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.

This contradicts the types in .sharedctypes. I don't know which is correct, but it should be consistent.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

After this PR both modules return ctypes.Array[_CT] for lock=False. The sharedctypes.pyi change is further down in the diff (on main it returned _CT, which was also wrong). They have to agree, since BaseContext.Array/Value just forward to the sharedctypes functions (context.py). At runtime, Array(c_int, 3, lock=False) returns a c_int_Array_3 instance from both.

anistark and others added 2 commits November 15, 2026 13:49
Per review: these cases aren't complex enough to need regression tests.
@github-actions

Copy link
Copy Markdown
Contributor

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

@anistark
anistark requested a review from srittau November 15, 2026 08:30
@srittau
srittau merged commit c81d161 into python:main Nov 15, 2026
88 checks passed
@anistark
anistark deleted the fix-multiprocessing-value-types branch November 15, 2026 13:00
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.Value.value needs to be defined

2 participants