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

--import-mode=importlib breaks assertion rewriting #12044

Open
y2kbugger opened this issue Mar 1, 2024 · 6 comments · May be fixed by #12313
Open

--import-mode=importlib breaks assertion rewriting #12044

y2kbugger opened this issue Mar 1, 2024 · 6 comments · May be fixed by #12313
Labels
topic: rewrite related to the assertion rewrite mechanism

Comments

@y2kbugger
Copy link

[tool.pytest.ini_options]
minversion = "8.0"
python_files = "*.test.py"
addopts = [
    "--import-mode=importlib",
    ]

Using --import-mode=importlib is more lenient than the default import-mode, and so it allowed me to use *.test.py rather than a valid module name e.g. *_.test.py.

Everything seemed to work correctly except explanations were missing:

    def test_answer():
>       assert inc(3) == 5
E       AssertionError

insync/db.test.py:34: AssertionError

instead of

    def test_answer():
>       assert inc(3) == 5
E       assert 4 == 5
E        +  where 4 = inc(3)

insync/db_test.py:34: AssertionError
@y2kbugger y2kbugger changed the title Bad python module name breaks assertion rewriting --import-mode=importlib breaks assertion rewriting Mar 12, 2024
@y2kbugger
Copy link
Author

Ok I was missing that after turning importlib back on, the issue returns, so in fact rewriting fails when importlib is turned on.

@Zac-HD Zac-HD added the topic: rewrite related to the assertion rewrite mechanism label Mar 12, 2024
@RonnyPfannschmidt
Copy link
Member

We need to verify what's up here

I suspect we are missing a bit where import lib will correctly fail for invalid module names, thus the assertions rewriter does as well, and then we incorrectly fall back to normal module loading

Most likely we try to find a test module inside a folder after not doing a sanity check on the filename, after that fails the normal import lib machinery takes over finding the module by path and working

I won't be able to verify before next week myself

@estyrke
Copy link

estyrke commented Apr 8, 2024

I am having this issue as well. It seems that it only happens if I have my tests in a package:

root/
  my_module/
    __init__.py
    file.py
  tests/
    __init__.py
    test_main.py

If I remove __init__.py in the tests folder the assert rewriting works even in importlib mode. I should also note that my test files are valid module names, unlike the OP's.

@tommie-lie
Copy link

I've had the same issue, the fix from @estyrke works, the test directories were erroneously packages, removing the __init__.pys fixes assertion rewriting.

mwchase added a commit to mwchase/camel that referenced this issue Apr 12, 2024
@isra17
Copy link

isra17 commented May 10, 2024

Hitting the same problems, we keep out tests in the packages, but we need importlib to work-around issues with namespaced package. Keeping our tests inside the namespaced package, even by removing the __init__.py from the test folder breaks assertion rewriting when we upgraded from 8.0.2 to 8.2.0.

Moving the tests outside the namespace package fix it, but it's an unfortunate limitation.

@isra17
Copy link

isra17 commented May 11, 2024

Actually, it only keep happening with the config:

[pytest]
consider_namespace_packages = true

Without this, removing the __init__.py from a test folder fix it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: rewrite related to the assertion rewrite mechanism
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants