-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
KeyError
fired from importlib
during collection when running with --import-mode=importlib
on a directory containing a same-named one
#12592
Comments
Feels like this might be an upstream issue; we should try to get a reproducer using stdlib code only. |
…in the namespace package causes a KeyError.(pytest-dev#12592)
…in the namespace package causes a KeyError.(pytest-dev#12592)
…in the namespace package causes a KeyError.(pytest-dev#12592)
…in the namespace package causes a KeyError.(pytest-dev#12592)
Directories inside a namespace package with the same name as the namespace package would cause a `KeyError` with `--import-mode=importlib`. Fixes #12592 Co-authored-by: Bruno Oliveira <[email protected]> (cherry picked from commit 6486c3f)
#12843) Directories inside a namespace package with the same name as the namespace package would cause a `KeyError` with `--import-mode=importlib`. Fixes #12592 Co-authored-by: Bruno Oliveira <[email protected]> (cherry picked from commit 6486c3f) Co-authored-by: dongfangtianyu <[email protected]>
cheers for fixing this, folks! I noticed a side-effect of this though: if pytest is running a test on a module that imports e.g.
and there is an |
Seeing a similar issue as #12592 (comment) Perhaps it's my misunderstanding of namespace packages, but I typically don't have any
|
Automatically Upgrade Dependencies - by upgrade-dependencies.yml The new version of pytest introduced an issue with files named the same as their module: pytest-dev/pytest#12592 (comment) --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: michael-christen <[email protected]>
Hi, @valeriupredoi @michael-christen I tried to reproduce an example, Is it the same as this? Please feel free to provide additional examples https://github.com/dongfangtianyu/pytest_importlib_issue |
Regression in #12716 In short: `PathFinder.find_spec` received the argument `/cow/moo` but loaded `/cow/moo/moo.py` instead. **Trigger conditions:** 1. `/cow/moo/moo.py` exists (a file and directory with the same name). 2. `/cow/moo/test_moo.py` exists (test case resides in the directory). When pytest loads test files in `importlib` mode, it continues recursive loading upward: - When loading `cow.moo`, it should return a namespace but unexpectedly returns a module. - When loading `cow.moo.moo`, it should return a module but unexpectedly returns a namespace. **Complete example:** [[GitHub repository](https://github.com/dongfangtianyu/pytest_importlib_issue)](https://github.com/dongfangtianyu/pytest_importlib_issue) - `main.py`: Reproduces the error. - `debug.py`: Demonstrates the behavior of `PathFinder.find_spec`. **Context:** #12592 (comment) #12592 (comment) --------- Co-authored-by: Bruno Oliveira <[email protected]>
Regression in #12716 In short: `PathFinder.find_spec` received the argument `/cow/moo` but loaded `/cow/moo/moo.py` instead. **Trigger conditions:** 1. `/cow/moo/moo.py` exists (a file and directory with the same name). 2. `/cow/moo/test_moo.py` exists (test case resides in the directory). When pytest loads test files in `importlib` mode, it continues recursive loading upward: - When loading `cow.moo`, it should return a namespace but unexpectedly returns a module. - When loading `cow.moo.moo`, it should return a module but unexpectedly returns a namespace. **Complete example:** [[GitHub repository](https://github.com/dongfangtianyu/pytest_importlib_issue)](https://github.com/dongfangtianyu/pytest_importlib_issue) - `main.py`: Reproduces the error. - `debug.py`: Demonstrates the behavior of `PathFinder.find_spec`. **Context:** #12592 (comment) #12592 (comment) --------- Co-authored-by: Bruno Oliveira <[email protected]> (cherry picked from commit 28e1e25)
Yep, that's the exact case I have, thanks @dongfangtianyu 🙏 |
…#13054) Regression in #12716 In short: `PathFinder.find_spec` received the argument `/cow/moo` but loaded `/cow/moo/moo.py` instead. **Trigger conditions:** 1. `/cow/moo/moo.py` exists (a file and directory with the same name). 2. `/cow/moo/test_moo.py` exists (test case resides in the directory). When pytest loads test files in `importlib` mode, it continues recursive loading upward: - When loading `cow.moo`, it should return a namespace but unexpectedly returns a module. - When loading `cow.moo.moo`, it should return a module but unexpectedly returns a namespace. **Complete example:** [[GitHub repository](https://github.com/dongfangtianyu/pytest_importlib_issue)](https://github.com/dongfangtianyu/pytest_importlib_issue) - `main.py`: Reproduces the error. - `debug.py`: Demonstrates the behavior of `PathFinder.find_spec`. **Context:** #12592 (comment) #12592 (comment) --------- Co-authored-by: Bruno Oliveira <[email protected]> (cherry picked from commit 28e1e25) Co-authored-by: dongfangtianyu <[email protected]>
A detailed description of the bug or problem you are having
When creating the following structure:
then
pytest --import-mode=importlib
fails with the following cryptic message:To trigger this bug:
Running with
--pdb
gives a slightly more useful backtrace:Output of
pip list
from the virtual environment you are usingI tried this on a minimal poetry env with just
pytest
pytest and operating system versions
pytest 8.2.2. Encountered this both with python 3.10 and python 3.12, so the python version doesn't seem to be a factor.
Running on Ubuntu 22.04.4 LTS.
cc @criemen who encountered this
The text was updated successfully, but these errors were encountered: