-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Typing of multiprocessing.Value broken since mypy 0.981 #8799
Comments
I assume that this was introduced in #8330 by @hauntsaninja. Before that PR, the fourth overload was matched, which just returned
|
Array() has the same problem
https://docs.python.org/3.10/library/multiprocessing.html#multiprocessing.Array
|
I've been meaning to address this problem in a PR (as well as other problems in #4266), however, I have a couple of questions: The main issue is concerning the typeshed/stdlib/multiprocessing/sharedctypes.pyi Lines 90 to 91 in 9544447
Therefore, the correct usage would be: Unfortunately, I don't know how to type hint it so the _SCT = TypeVar("_SCT", bound=_SimpleCData[Any])
class Synchronized(Generic[_SCT]):
value: ?# What could go here? In C++ one could use decltype(self._obj.value), but there's no Py equivalent. The second question is about the current usage of
Is it really okay to use these, even though they are prefixed with an underscore? Seems to me like it would mark them as private. There's a related issue about this (#6018). Also, please note that --- TL;DR --- If the following questions are answered, I can start working on a PR to resolve this issue:
|
As far as I can see, |
python/typeshed#8799 that gives this error: parsl/executors/workqueue/executor.py:674: error: "SynchronizedBase[c_bool]" has no attribute "value" [attr-defined] this patch ignores that, until a fix becomes available upstream
python/typeshed#8799 that gives this error: parsl/executors/workqueue/executor.py:674: error: "SynchronizedBase[c_bool]" has no attribute "value" [attr-defined] this patch ignores that, until a fix becomes available upstream
python/typeshed#8799 that gives this error: parsl/executors/workqueue/executor.py:674: error: "SynchronizedBase[c_bool]" has no attribute "value" [attr-defined] this patch ignores that, until a fix becomes available upstream
typeshed issue revealed by a new mypy version. See: python/typeshed#8799
python/typeshed#8799 that gives this error: parsl/executors/workqueue/executor.py:674: error: "SynchronizedBase[c_bool]" has no attribute "value" [attr-defined] this patch ignores that, until a fix becomes available upstream
python/typeshed#8799 that gives this error: parsl/executors/workqueue/executor.py:674: error: "SynchronizedBase[c_bool]" has no attribute "value" [attr-defined] this patch ignores that, until a fix becomes available upstream
python/typeshed#8799 that gives this error: parsl/executors/workqueue/executor.py:674: error: "SynchronizedBase[c_bool]" has no attribute "value" [attr-defined] this patch ignores that, until a fix becomes available upstream
python/typeshed#8799 that gives this error: parsl/executors/workqueue/executor.py:674: error: "SynchronizedBase[c_bool]" has no attribute "value" [attr-defined] this patch ignores that, until a fix becomes available upstream
python/typeshed#8799 that gives this error: parsl/executors/workqueue/executor.py:674: error: "SynchronizedBase[c_bool]" has no attribute "value" [attr-defined] this patch ignores that, until a fix becomes available upstream
python/typeshed#8799 that gives this error: parsl/executors/workqueue/executor.py:674: error: "SynchronizedBase[c_bool]" has no attribute "value" [attr-defined] this patch ignores that, until a fix becomes available upstream
python/typeshed#8799 that gives this error: parsl/executors/workqueue/executor.py:674: error: "SynchronizedBase[c_bool]" has no attribute "value" [attr-defined] this patch ignores that, until a fix becomes available upstream
python/typeshed#8799 that gives this error: parsl/executors/workqueue/executor.py:674: error: "SynchronizedBase[c_bool]" has no attribute "value" [attr-defined] this patch ignores that, until a fix becomes available upstream
python/typeshed#8799 that gives this error: parsl/executors/workqueue/executor.py:674: error: "SynchronizedBase[c_bool]" has no attribute "value" [attr-defined] this patch ignores that, until a fix becomes available upstream
python/typeshed#8799 that gives this error: parsl/executors/workqueue/executor.py:674: error: "SynchronizedBase[c_bool]" has no attribute "value" [attr-defined] this patch ignores that, until a fix becomes available upstream
This is still an issue with |
python/typeshed#8799 that gives this error: parsl/executors/workqueue/executor.py:674: error: "SynchronizedBase[c_bool]" has no attribute "value" [attr-defined] this patch ignores that, until a fix becomes available upstream
python/typeshed#8799 that gives this error: parsl/executors/workqueue/executor.py:674: error: "SynchronizedBase[c_bool]" has no attribute "value" [attr-defined] this patch ignores that, until a fix becomes available upstream
python/typeshed#8799 that gives this error: parsl/executors/workqueue/executor.py:674: error: "SynchronizedBase[c_bool]" has no attribute "value" [attr-defined] this patch ignores that, until a fix becomes available upstream
python/typeshed#8799 that gives this error: parsl/executors/workqueue/executor.py:674: error: "SynchronizedBase[c_bool]" has no attribute "value" [attr-defined] this patch ignores that, until a fix becomes available upstream
python/typeshed#8799 that gives this error: parsl/executors/workqueue/executor.py:674: error: "SynchronizedBase[c_bool]" has no attribute "value" [attr-defined] this patch ignores that, until a fix becomes available upstream
As this bug still prevents us from upgrading mypy and using Python 3.12-generic classes: Is there anything I could to to support here? Sadly I'm not too familiar with inner workings of python typing, so I cannot answer any of the questions @angelo-daumas asked, but maybe there is something else to support? |
I found a fix that would fix the bug, but am not sure about possible side effects. In https://github.com/python/typeshed/blob/main/stdlib/multiprocessing/context.pyi#L84 (and Line 86) replace For users, using a manager works too, as the manager uses from multiprocessing import Value, Manager
import ctypes
manager = Manager()
mapper = manager.Value(ctypes.c_float, float("nan"))
print(mapper.value) |
could fix python#8799 and python#9898, but request review for side effects. Without this change, static type checkers would treat `Value(ctypes.c_float, 0.0)` like `ctypes.c_float`, but `Value` offers a field `value` for storage
@prauscher I'm using the following, with mypy happy: my_var: Synchronized[bool] = Synchronized(multiprocessing.Value(ctypes.c_bool, True)) I didn't observe any side effects. Since |
Pytest complains that Synchronized is not subscriptable, whether you get it direct from multiprocessing, or from typeshed. I haven't dug into why. I'd be curious to hear if anyone is using Pytest with a "subscripted" Synchronized type. I'm NOT using @f0rmiga's approach above, as I'm not going to wrap my already Synchronized Value in another Synchronized instance just to make the type checker happy. I am using mypy 1.10.0 which I think should support the use of Synchronized here. Same behavior on 3.9 and 3.12. The simplest way to illustrate the problem is with these two files: # define_type.py
import ctypes
import time
from multiprocessing import Value
from typing import Any
from multiprocessing.sharedctypes import Synchronized
def a_value() -> Synchronized[ctypes.c_double]:
val: Synchronized[ctypes.c_double] = Value(ctypes.c_double, lock=False)
val.value = time.time() # type: ignore[assignment]
return val # test_define_type.py
from define_type import a_value
def test_value():
v = a_value()
v.value = 1.2 # type: ignore[assignment]
assert v is not None This results in a world where mypy complains (unless told to ignore) about assigning a float to a c_double. But that's fine - the type checker is still helping me by making me check that the assignment makes sense. running pytest in a directory with these two files results in:
Note that mypy will complain about duplicate files if I try including |
Hello everyone,
after updating to mypy 0.981, i received a mypy error for multiprocessing.Value:
As far as I can tell, the issue is that
Value()
gives aSynchronizedBase
which is not typed to have avalue
attribute.Synchronized
however would offer this attribute, as it is present in the real world.Am I missing something or should
typeshed/stdlib/multiprocessing/context.pyi
Line 89 in 9544447
Synchronized
instead ofSynchronizedBase
?Update: The issue was not there in 0.971 and is now present in 0.981
The text was updated successfully, but these errors were encountered: