Bug 2081968

Summary: python-rpmautospec fails to build with Python 3.11: unittest.mock.InvalidSpecError: Cannot spec a Mock object.
Product: [Fedora] Fedora Reporter: Tomáš Hrnčiar <thrnciar>
Component: python-rpmautospecAssignee: Nils Philippsen <nphilipp>
Status: CLOSED RAWHIDE QA Contact: Fedora Extras Quality Assurance <extras-qa>
Severity: unspecified Docs Contact:
Priority: unspecified    
Version: rawhideCC: asaleh, epel-packagers-sig, infra-sig, mhroncok, michel, nphilipp, thrnciar
Target Milestone: ---   
Target Release: ---   
Hardware: Unspecified   
OS: Unspecified   
Whiteboard:
Fixed In Version: python-rpmautospec-0.2.8-1.fc37 Doc Type: If docs needed, set a value
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2022-05-16 16:31:48 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:
Bug Depends On: 2081969    
Bug Blocks: 2016048    

Description Tomáš Hrnčiar 2022-05-05 06:40:41 UTC
python-rpmautospec fails to build with Python 3.11.0a7.

=================================== FAILURES ===================================
_____________________________ test_main_valid_args _____________________________
[gw0] linux -- Python 3.11.0 /usr/bin/python3

pkg_converter_mock = <MagicMock name='PkgConverter' id='139757742069840'>
specfile = PosixPath('/tmp/pytest-of-mockbuild/pytest-0/popen-gw0/test_main_valid_args0/test/test.spec')

    @unittest.mock.patch("rpmautospec.subcommands.convert.PkgConverter")
    def test_main_valid_args(pkg_converter_mock, specfile):
>       pkg_converter = unittest.mock.Mock(spec=convert.PkgConverter)()

tests/rpmautospec/subcommands/test_convert.py:75: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
/usr/lib64/python3.11/unittest/mock.py:1090: in __init__
    _safe_super(CallableMixin, self).__init__(
/usr/lib64/python3.11/unittest/mock.py:442: in __init__
    self._mock_add_spec(spec, spec_set, _spec_as_instance, _eat_self)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <[AttributeError('_mock_methods') raised in repr()] Mock object at 0x7f1be36ae950>
spec = <MagicMock name='PkgConverter' id='139757742069840'>, spec_set = None
_spec_as_instance = False, _eat_self = False

    def _mock_add_spec(self, spec, spec_set, _spec_as_instance=False,
                       _eat_self=False):
        if _is_instance_mock(spec):
>           raise InvalidSpecError(f'Cannot spec a Mock object. [object={spec!r}]')
E           unittest.mock.InvalidSpecError: Cannot spec a Mock object. [object=<MagicMock name='PkgConverter' id='139757742069840'>]

/usr/lib64/python3.11/unittest/mock.py:493: InvalidSpecError
=========================== short test summary info ============================
FAILED tests/rpmautospec/subcommands/test_convert.py::test_main_valid_args - ...
======================== 1 failed, 359 passed in 32.56s ========================

bpo-43478: Mocks can no longer be provided as the specs for other Mocks. As a result, an already-mocked object cannot be passed to mock.Mock(). This can uncover bugs in tests since these Mock-derived Mocks will always pass certain tests (e.g. isinstance) and builtin assert functions (e.g. assert_called_once_with) will unconditionally pass.

https://bugs.python.org/issue?@action=redirect&bpo=43478
https://docs.python.org/3.11/whatsnew/3.11.html

For the build logs, see:
https://copr-be.cloud.fedoraproject.org/results/@python/python3.11/fedora-rawhide-x86_64/04366736-python-rpmautospec/

For all our attempts to build python-rpmautospec with Python 3.11, see:
https://copr.fedorainfracloud.org/coprs/g/python/python3.11/package/python-rpmautospec/

Testing and mass rebuild of packages is happening in copr. You can follow these instructions to test locally in mock if your package builds with Python 3.11:
https://copr.fedorainfracloud.org/coprs/g/python/python3.11/

Let us know here if you have any questions.

Python 3.11 is planned to be included in Fedora 37. To make that update smoother, we're building Fedora packages with all pre-releases of Python 3.11.
A build failure prevents us from testing all dependent packages (transitive [Build]Requires), so if this package is required a lot, it's important for us to get it fixed soon.
We'd appreciate help from the people who know this package best, but if you don't want to work on this now, let us know so we can try to work around it on our side.

Comment 1 Miro Hrončok 2022-05-05 10:56:06 UTC
Replacing:

    pkg_converter = unittest.mock.Mock(spec=convert.PkgConverter)()

...with:

     pkg_converter = convert.PkgConverter()

...does the trick. convert.PkgConverter is already a mock.

Comment 2 Nils Philippsen 2022-05-16 15:55:13 UTC
(In reply to Miro Hrončok from comment #1)
> Replacing:
> 
>     pkg_converter = unittest.mock.Mock(spec=convert.PkgConverter)()
> 
> ...with:
> 
>      pkg_converter = convert.PkgConverter()
> 
> ...does the trick. convert.PkgConverter is already a mock.

Yeah, but that just seems to hide issues. I didn't write the test myself and I'm still figuring out how it's supposed to work but it looks pretty much as if things should be run through the real `PkgConverter` object and the mock just exists to be able to inspect what was called and such.

Comment 3 Nils Philippsen 2022-05-16 16:10:23 UTC
Hmm, I guess I was wrong with my assumption, but there's still a way to make this nicer.