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

Trying to use mypy and pytest with a multiprocessing.Value - unable to use Synchronized as a type #12330

Closed
4 tasks done
davclark opened this issue May 15, 2024 · 3 comments
Closed
4 tasks done

Comments

@davclark
Copy link

davclark commented May 15, 2024

  • a detailed description of the bug or problem you are having
  • output of pip list from the virtual environment you are using
  • pytest and operating system versions
  • minimal example if possible

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 am using mypy 1.10.0 which I think should support the use of Synchronized here. Same behavior on 3.9 and 3.12. Both installed with brew on M1 macOS 14.4.1.

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:

___________________________________________ ERROR collecting test_define_type.py ___________________________________________
test_define_type.py:1: in <module>
    from define_type import a_value
define_type.py:7: in <module>
    def a_value() -> Synchronized[ctypes.c_double]:
E   TypeError: type 'Synchronized' is not subscriptable

Like I mentioned, I see this on 3.9 and 3.12 (the only versions I tested), but here's the pip list from the 3.12 venv:

Package           Version
----------------- -------
iniconfig         2.0.0
mypy              1.10.0
mypy-extensions   1.0.0
packaging         24.0
pip               24.0
pluggy            1.5.0
pytest            8.2.0
typing_extensions 4.11.0

Note that mypy will complain about duplicate files if I try including Synchronized from typeshed. So, this could be addressed by mypy somehow giving me a Synchronized type that will work with pytest. But maybe this is pytest's problem. I'm cross-posting with python/typeshed#8799 (comment), which includes more discussion of using Values with mypy.

@bluetech
Copy link
Member

The most likely explanation is that Synchronized is only generic in typeshed, but not in runtime, which makes Synchronized[double] valid for mypy but invalid at runtime. The fix is to quote it like "Synchronized[double]" or to add from __future__ import annotations to the top of your file.

@nicoddemus nicoddemus closed this as not planned Won't fix, can't repro, duplicate, stale May 15, 2024
@The-Compiler
Copy link
Member

Note that nothing is pytest specific about this - you'd get the exact same error when just running your function.

Python 3.12.3 (main, Apr 23 2024, 09:16:07) [GCC 13.2.1 20240417] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from multiprocessing.sharedctypes import Synchronized
>>> from multiprocessing import Value
>>> import ctypes
>>> val: Synchronized[ctypes.c_double] = Value(ctypes.c_double, lock=False)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: type 'Synchronized' is not subscriptable

@davclark
Copy link
Author

Ack! Thank you @The-Compiler. I obviously should have checked that.

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

No branches or pull requests

4 participants