Skip to content

mocker.patch fails to find tensorlib _setup attribute for Python 3.11 #2143

@matthewfeickert

Description

@matthewfeickert

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 tests

pyhf/tests/test_tensor.py

Lines 607 to 618 in 89b3e40

@pytest.mark.parametrize(
'tensorlib',
['numpy_backend', 'jax_backend', 'pytorch_backend', 'tensorflow_backend'],
)
@pytest.mark.parametrize('precision', ['64b', '32b'])
def test_tensorlib_setup(tensorlib, precision, mocker):
tb = getattr(pyhf.tensor, tensorlib)(precision=precision)
func = mocker.patch(f'pyhf.tensor.{tensorlib}._setup')
assert func.call_count == 0
pyhf.set_backend(tb)
assert func.call_count == 1

which all fail on

func = mocker.patch(f'pyhf.tensor.{tensorlib}._setup')

with errors like

___________________ test_tensorlib_setup[64b-numpy_backend] ____________________

tensorlib = 'numpy_backend', precision = '64b'
mocker = <pytest_mock.plugin.MockerFixture object at 0x7fc9557f2e90>

    @pytest.mark.parametrize(
        'tensorlib',
        ['numpy_backend', 'jax_backend', 'pytorch_backend', 'tensorflow_backend'],
    )
    @pytest.mark.parametrize('precision', ['64b', '32b'])
    def test_tensorlib_setup(tensorlib, precision, mocker):
        tb = getattr(pyhf.tensor, tensorlib)(precision=precision)
    
>       func = mocker.patch(f'pyhf.tensor.{tensorlib}._setup')

mocker     = <pytest_mock.plugin.MockerFixture object at 0x7fc9557f2e90>
precision  = '64b'
tb         = <pyhf.tensor.numpy_backend.numpy_backend object at 0x7fca3444fc80>
tensorlib  = 'numpy_backend'

tests/test_tensor.py:605: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
/opt/hostedtoolcache/Python/3.11.2/x64/lib/python3.11/site-packages/pytest_mock/plugin.py:404: in __call__
    return self._start_patch(
        autospec   = None
        create     = False
        kwargs     = {}
        new        = sentinel.DEFAULT
        new_callable = None
        self       = <pytest_mock.plugin.MockerFixture._Patcher object at 0x7fc9557f00d0>
        spec       = None
        spec_set   = None
        target     = 'pyhf.tensor.numpy_backend._setup'
/opt/hostedtoolcache/Python/3.11.2/x64/lib/python3.11/site-packages/pytest_mock/plugin.py:214: in _start_patch
    mocked = p.start()  # type: unittest.mock.MagicMock
        args       = ('pyhf.tensor.numpy_backend._setup',)
        kwargs     = {'autospec': None, 'create': False, 'new': sentinel.DEFAULT, 'new_callable': None, ...}
        mock_func  = <function patch at 0x7fcb82251580>
        p          = <unittest.mock._patch object at 0x7fc9557f2790>
        self       = <pytest_mock.plugin.MockerFixture._Patcher object at 0x7fc9557f00d0>
        warn_on_mock_enter = True
/opt/hostedtoolcache/Python/3.11.2/x64/lib/python3.11/unittest/mock.py:1585: in start
    result = self.__enter__()
        self       = <unittest.mock._patch object at 0x7fc9557f2790>
/opt/hostedtoolcache/Python/3.11.2/x64/lib/python3.11/unittest/mock.py:1437: in __enter__
    original, local = self.get_original()
        autospec   = None
        kwargs     = {}
        new        = sentinel.DEFAULT
        new_callable = None
        self       = <unittest.mock._patch object at 0x7fc9557f2790>
        spec       = None
        spec_set   = None
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <unittest.mock._patch object at 0x7fc9557f2790>

    def get_original(self):
        target = self.getter()
        name = self.attribute
    
        original = DEFAULT
        local = False
    
        try:
            original = target.__dict__[name]
        except (AttributeError, KeyError):
            original = getattr(target, name, DEFAULT)
        else:
            local = True
    
        if name in _builtins and isinstance(target, ModuleType):
            self.create = True
    
        if not self.create and original is DEFAULT:
>           raise AttributeError(
                "%s does not have the attribute %r" % (target, name)
            )
E           AttributeError: <module 'pyhf.tensor.numpy_backend' from '/opt/hostedtoolcache/Python/3.11.2/x64/lib/python3.11/site-packages/pyhf/tensor/numpy_backend.py'> does not have the attribute '_setup'

local      = False
name       = '_setup'
original   = sentinel.DEFAULT
self       = <unittest.mock._patch object at 0x7fc9557f2790>
target     = <module 'pyhf.tensor.numpy_backend' from '/opt/hostedtoolcache/Python/3.11.2/x64/lib/python3.11/site-packages/pyhf/tensor/numpy_backend.py'>

/opt/hostedtoolcache/Python/3.11.2/x64/lib/python3.11/unittest/mock.py:1410: AttributeError

I'm not sure why as

import pyhf
import pyhf.tensor.numpy_backend
pyhf.tensor.numpy_backend._setup  # <function numpy_backend._setup at 0x7f8466564680>

and this is a Python 3.11 error only. :?

Metadata

Metadata

Labels

bugSomething isn't workingtestspytest

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions