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

[4.6] Define create_module()/exec_module() in AssertionRewritingHook to avoid ImportWarning #8236

Closed
wants to merge 1 commit into from

Conversation

hroncok
Copy link
Member

@hroncok hroncok commented Jan 11, 2021

__________ ERROR collecting testing/python/show_fixtures_per_test.py ___________
/usr/lib/python3.10/site-packages/py/_path/local.py:704: in pyimport
    __import__(modname)
<frozen importlib._bootstrap>:1026: in _find_and_load
    ???
<frozen importlib._bootstrap>:1005: in _find_and_load_unlocked
    ???
<frozen importlib._bootstrap>:681: in _load_unlocked
    ???
E   ImportWarning: AssertionRewritingHook.exec_module() not found; falling back to load_module()
!!!!!!!!!!!!!!!!!!! Interrupted: 59 errors during collection !!!!!!!!!!!!!!!!!!!
=========================== 59 error in 3.65 seconds ===========================

…id ImportWarning

    __________ ERROR collecting testing/python/show_fixtures_per_test.py ___________
    /usr/lib/python3.10/site-packages/py/_path/local.py:704: in pyimport
        __import__(modname)
    <frozen importlib._bootstrap>:1026: in _find_and_load
        ???
    <frozen importlib._bootstrap>:1005: in _find_and_load_unlocked
        ???
    <frozen importlib._bootstrap>:681: in _load_unlocked
        ???
    E   ImportWarning: AssertionRewritingHook.exec_module() not found; falling back to load_module()
    !!!!!!!!!!!!!!!!!!! Interrupted: 59 errors during collection !!!!!!!!!!!!!!!!!!!
    =========================== 59 error in 3.65 seconds ===========================
@hroncok hroncok marked this pull request as ready for review January 11, 2021 11:19
@hroncok
Copy link
Member Author

hroncok commented Jan 11, 2021

This might need some tests to be adapted thou :(

=================================== FAILURES ===================================
_____________ TestAssertionRewriteHookDetails.test_reload_reloads ______________

self = <test_assertrewrite.TestAssertionRewriteHookDetails object at 0x7f9bc9d46e20>
testdir = <Testdir local('/tmp/pytest-of-mockbuild/pytest-3/test_reload_reloads0')>

    def test_reload_reloads(self, testdir):
        """Reloading a module after change picks up the change."""
        testdir.tmpdir.join("file.py").write(
            textwrap.dedent(
                """
            def reloaded():
                return False
    
            def rewrite_self():
                with open(__file__, 'w') as self:
                    self.write('def reloaded(): return True')
        """
            )
        )
        testdir.tmpdir.join("pytest.ini").write(
            textwrap.dedent(
                """
            [pytest]
            python_files = *.py
        """
            )
        )
    
        testdir.makepyfile(
            test_fun="""
            import sys
            try:
                from imp import reload
            except ImportError:
                pass
    
            def test_loader():
                import file
                assert not file.reloaded()
                file.rewrite_self()
                reload(file)
                assert file.reloaded()
            """
        )
        result = testdir.runpytest("-s")
>       result.stdout.fnmatch_lines(["* 1 passed*"])
E       Failed: nomatch: '* 1 passed*'
E           and: '============================= test session starts =============================='
E           and: 'platform linux -- Python 3.10.0a4, pytest-4.6.11, py-1.10.0, pluggy-0.13.1'
E           and: 'rootdir: /tmp/pytest-of-mockbuild/pytest-3/test_reload_reloads0, inifile: pytest.ini'
E           and: 'plugins: hypothesis-5.41.3'
E           and: 'collected 1 item'
E           and: ''
E           and: 'test_fun.py F'
E           and: ''
E           and: '=================================== FAILURES ==================================='
E           and: '_________________________________ test_loader __________________________________'
E           and: ''
E           and: '    def test_loader():'
E           and: '        import file'
E           and: '        assert not file.reloaded()'
E           and: '        file.rewrite_self()'
E           and: '        reload(file)'
E           and: '>       assert file.reloaded()'
E           and: 'E       AssertionError: assert False'
E           and: 'E        +  where False = <function reloaded at 0x7f9bc9d65790>()'
E           and: "E        +    where <function reloaded at 0x7f9bc9d65790> = <module 'file' (<_pytest.assertion.rewrite.AssertionRewritingHook object at 0x7f9bc817eeb0>)>.reloaded"
E           and: ''
E           and: 'test_fun.py:12: AssertionError'
E           and: '=========================== 1 failed in 0.01 seconds ==========================='
E           and: ''
E       remains unmatched: '* 1 passed*'

/builddir/build/BUILD/pytest-4.6.11/testing/test_assertrewrite.py:1142: Failed
----------------------------- Captured stdout call -----------------------------
============================= test session starts ==============================
platform linux -- Python 3.10.0a4, pytest-4.6.11, py-1.10.0, pluggy-0.13.1
rootdir: /tmp/pytest-of-mockbuild/pytest-3/test_reload_reloads0, inifile: pytest.ini
plugins: hypothesis-5.41.3
collected 1 item

test_fun.py F

=================================== FAILURES ===================================
_________________________________ test_loader __________________________________

    def test_loader():
        import file
        assert not file.reloaded()
        file.rewrite_self()
        reload(file)
>       assert file.reloaded()
E       AssertionError: assert False
E        +  where False = <function reloaded at 0x7f9bc9d65790>()
E        +    where <function reloaded at 0x7f9bc9d65790> = <module 'file' (<_pytest.assertion.rewrite.AssertionRewritingHook object at 0x7f9bc817eeb0>)>.reloaded

test_fun.py:12: AssertionError
=========================== 1 failed in 0.01 seconds ===========================

@hroncok hroncok self-assigned this Jan 11, 2021
@hroncok hroncok marked this pull request as draft January 11, 2021 11:22
@hroncok
Copy link
Member Author

hroncok commented Jan 11, 2021

It seems that the test is doing what it is supposed to do, but when the two new methods are added, it breaks. I have no idea how to proceed :(

@asottile
Copy link
Member

there's two competing import hook specifications, PEP302 (which is what pytest 4.6.x uses) and PEP 451 which is the new style imports. pytest moved to PEP 451 in pytest 5.0 when dropping python 2.

the two PEPs are slightly incompatible with each other and python 2 dos not support PEP 451 so this isn't backportable

I'd suggest ignoring the warning in python3.10 for now, and/or consider dropping pytest 4.6 and using a modern version instead

the related bpo / PR are here:

@hroncok
Copy link
Member Author

hroncok commented Jan 13, 2021

consider dropping pytest 4.6 and using a modern version instead

We have pytest 4.6 in Fedora only for pytest-relaxed (needed by invoke and paramiko). All other packages use pytest 6.
I guess I'll just drop it eventually anyway, but I try to keep up.

@hroncok
Copy link
Member Author

hroncok commented Jan 13, 2021

See also bitprophet/pytest-relaxed#12

@hroncok hroncok closed this Mar 17, 2021
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

Successfully merging this pull request may close these issues.

2 participants