Repository navigation
[multiprocessing] Fix return types of Value and Array - #16485
Conversation
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
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
srittau
left a comment
There was a problem hiding this comment.
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]: ... |
There was a problem hiding this comment.
This contradicts the types in .sharedctypes. I don't know which is correct, but it should be consistent.
There was a problem hiding this comment.
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.
Per review: these cases aren't complex enough to need regression tests.
|
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
Closes #10319.
Make
multiprocessing.Value/Arrayand theirmultiprocessing.sharedctypescounterparts match the runtime: they return the raw ctypes object (orctypes.Array) forlock=False,Synchronizedfor simple C types and typecode strings, andSynchronizedBasefor other ctypes such as structures. SeeValue/Arrayandsynchronized().Prepared with Claude Code.