-
Notifications
You must be signed in to change notification settings - Fork 85
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
mocker.patch fails to find tensorlib _setup
attribute for Python 3.11
#2143
Comments
From chat @kratsg mentioned the following:
|
Hm. What is confusing is that this isn't listed in the docs at all for the |
Yeah, it is definitely an issue between # mock_debug.py
from unittest.mock import patch
import pyhf
tensor_backend = getattr(pyhf.tensor, "numpy_backend")(precision="64b")
with patch("pyhf.tensor.numpy_backend._setup") as mock_func:
print(f"{mock_func.call_count=}")
pyhf.set_backend(tensor_backend)
print(f"{mock_func.call_count=}") for Python 3.10 works $ python --version --version && python /tmp/mock_debug.py
Python 3.10.6 (main, Sep 20 2022, 17:04:13) [GCC 11.2.0]
mock_func.call_count=0
mock_func.call_count=1 but for Python 3.11 fails $ python --version --version && python /tmp/mock_debug.py
Python 3.11.1 (main, Feb 13 2023, 22:31:08) [GCC 11.3.0]
Traceback (most recent call last):
File "/tmp/mock_debug.py", line 7, in <module>
with patch("pyhf.tensor.numpy_backend._setup") as mock_func:
File "/home/feickert/.pyenv/versions/3.11.1/lib/python3.11/unittest/mock.py", line 1437, in __enter__
original, local = self.get_original()
^^^^^^^^^^^^^^^^^^^
File "/home/feickert/.pyenv/versions/3.11.1/lib/python3.11/unittest/mock.py", line 1410, in get_original
raise AttributeError(
AttributeError: <module 'pyhf.tensor.numpy_backend' from '/home/feickert/Code/GitHub/pyhf/src/pyhf/tensor/numpy_backend.py'> does not have the attribute '_setup' with the same The only registered change in the What's New in Python 3.11 for
so I'm confused as to the change in behavior. |
Okay the following works across both Python 3.10 and Python 3.11: # mock_debug.py
from unittest.mock import patch
import pyhf
tensor_backend = getattr(pyhf.tensor, "numpy_backend")(precision="64b")
with patch.object(pyhf.tensor.numpy_backend, "_setup") as mock_func:
print(f"{mock_func.call_count=}") # mock_func.call_count=0
pyhf.set_backend(tensor_backend)
print(f"{mock_func.call_count=}") # mock_func.call_count=1 |
Thanks @matthewfeickert, your investigation is really helpful! We are facing a similar issue in our project, so I found this issue. Finally, I discovered our issue is caused by python/cpython#18544 which changes the logic of import pkgutil
# _dot_lookup and _importer are copied from Python 3.10
def _dot_lookup(thing, comp, import_path):
try:
return getattr(thing, comp)
except AttributeError:
__import__(import_path)
return getattr(thing, comp)
def _importer(target):
components = target.split('.')
import_path = components.pop(0)
thing = __import__(import_path)
for comp in components:
import_path += ".%s" % comp
thing = _dot_lookup(thing, comp, import_path)
return thing
print(pkgutil.resolve_name('pyhf.tensor.numpy_backend'))
print(_importer('pyhf.tensor.numpy_backend')) Output:
Because Line 1 in bd8c21a
pyhf/src/pyhf/tensor/__init__.py Line 24 in bd8c21a
|
Thanks very much for this great summary @jiasli! 🙌 I'll have to revisit this myself in the near future. |
Now that TensorFlow v2.12.0 is out with Python 3.11 support I've been working to add Python 3.11 to the CI. Everything passes with the exception of the
tests/test_tensor.py
test_tensorlib_setup
testspyhf/tests/test_tensor.py
Lines 607 to 618 in 89b3e40
which all fail on
pyhf/tests/test_tensor.py
Line 615 in 89b3e40
with errors like
I'm not sure why as
and this is a Python 3.11 error only. :?
The text was updated successfully, but these errors were encountered: