-
-
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
Module code is executed twice when collecting doctests #11306
Comments
Thanks for the detailed example! However, it seems to fail initially with:
after fixing that: diff --git i/pyproject.toml w/pyproject.toml
index ecb2f55..c797e64 100644
--- i/pyproject.toml
+++ w/pyproject.toml
@@ -7,7 +7,7 @@ name = 'pytest-doctest-import-twice'
version = '1.0'
description = 'pytest reproducer for doctest import behavior'
authors = [{ name = 'Philipp A.', email = 'flying-sheep@web.de' }]
-readme = 'README.md'
+readme = 'README.rst'
license = 'GPL-3.0-or-later'
requires-python = '>=3.11'
dependencies = [ it looks like it also needs an I ended up creating a virtualenv manually and using However, that looks like it just happens to expose this problem, and isn't actually the culprit itself. |
Or equivalent. If you do a regular install and run Python in safepath ( $ hatch run python -Pm site
sys.path = [
'/usr/lib/python311.zip',
'/usr/lib/python3.11',
'/usr/lib/python3.11/lib-dynload',
'<venv>/lib/python3.11/site-packages',
]
[…]
$ hatch run python -Pm pytest
[…]
collected 1 item / 1 error
[…] the same applies to doing a regular install and running the pytest binary (which is probably equivalent to safepath mode anyway) $ hatch run pytest
[…]
collected 1 item / 1 error
[…] |
The takeaway of all this is that this is not an issue of setting the PYTHONPATH wrong: It’s an issue with the way Lines 524 to 527 in 556e075
/edit: wait a second, that code path doesn’t exist in my local copy |
yes, I'm not saying this is caused by
|
That’s shouldn’t be the case. As said, hatch should just make a venv, dev-install the project into it, and then run the script. Are you using an old hatch version (≠1.7.0)? Maybe try manually creating a venv? #!/bin/bash
test -d .venv || python -m virtualenv .venv
source .venv/bin/activate
pip install -e . # or pip install .
pytest It shouldn’t matter (as said, both PYTHONPATH and regular install reproduce the bug) but it’s worrying that something on our systems behaves so differently. You can also try to recreate the env using |
wait a second, the code path I saw above has not been released. It’s part of #11148 I updated the test code to use pytest master and add some context about what pytest is doing. Seems like that code didn’t quite fix the problem, I see:
shouldn’t that cause the |
@nicoddemus any idea how it’s possible that the line throws a |
Hi @flying-sheep sorry not a glance, I think this might need debugging to understand what is happening. |
I hope I provided a good enough reproducer for that! Glad it’s on your radar. |
Oh sorry I was not clear, I did not mean I will be working on this soon, I'm on vacation at the moment. |
Haha no worries, enjoy! |
Hia, is there a chance you could take a look at this? Would be great to be able to run our doctests |
For packages, `import_path` receives the path to the package's `__init__.py` file, however module names (as they live in `sys.modules`) should not include the `__init__` part. For example, `app/core/__init__.py` should be imported as `app.core`, not as `app.core.__init__`. Fix pytest-dev#11306
@flying-sheep thanks for the ping and the reproducible example. Opened #11390 with the fix. 👍 |
For packages, `import_path` receives the path to the package's `__init__.py` file, however module names (as they live in `sys.modules`) should not include the `__init__` part. For example, `app/core/__init__.py` should be imported as `app.core`, not as `app.core.__init__`. Fix pytest-dev#11306
For packages, `import_path` receives the path to the package's `__init__.py` file, however module names (as they live in `sys.modules`) should not include the `__init__` part. For example, `app/core/__init__.py` should be imported as `app.core`, not as `app.core.__init__`. Fix pytest-dev#11306
For packages, `import_path` receives the path to the package's `__init__.py` file, however module names (as they live in `sys.modules`) should not include the `__init__` part. For example, `app/core/__init__.py` should be imported as `app.core`, not as `app.core.__init__`. Fix #11306
This PR contains the following updates: | Package | Update | Change | |---|---|---| | [pytest](https://docs.pytest.org/en/latest/) ([source](https://github.com/pytest-dev/pytest), [changelog](https://docs.pytest.org/en/stable/changelog.html)) | patch | `==7.4.0` -> `==7.4.2` | --- ### Release Notes <details> <summary>pytest-dev/pytest (pytest)</summary> ### [`v7.4.2`](https://github.com/pytest-dev/pytest/releases/tag/7.4.2): pytest 7.4.2 (2023-09-07) [Compare Source](pytest-dev/pytest@7.4.1...7.4.2) ### Bug Fixes - [#​11237](pytest-dev/pytest#11237): Fix doctest collection of `functools.cached_property` objects. - [#​11306](pytest-dev/pytest#11306): Fixed bug using `--importmode=importlib` which would cause package `__init__.py` files to be imported more than once in some cases. - [#​11367](pytest-dev/pytest#11367): Fixed bug where `user_properties` where not being saved in the JUnit XML file if a fixture failed during teardown. - [#​11394](pytest-dev/pytest#11394): Fixed crash when parsing long command line arguments that might be interpreted as files. ### Improved Documentation - [#​11391](pytest-dev/pytest#11391): Improved disclaimer on pytest plugin reference page to better indicate this is an automated, non-curated listing. ### [`v7.4.1`](https://github.com/pytest-dev/pytest/releases/tag/7.4.1): pytest 7.4.1 (2023-09-02) [Compare Source](pytest-dev/pytest@7.4.0...7.4.1) ## Bug Fixes - [#​10337](pytest-dev/pytest#10337): Fixed bug where fake intermediate modules generated by `--import-mode=importlib` would not include the child modules as attributes of the parent modules. - [#​10702](pytest-dev/pytest#10702): Fixed error assertion handling in `pytest.approx` when `None` is an expected or received value when comparing dictionaries. - [#​10811](pytest-dev/pytest#10811): Fixed issue when using `--import-mode=importlib` together with `--doctest-modules` that caused modules to be imported more than once, causing problems with modules that have import side effects. </details> --- ### Configuration 📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Enabled. ♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNi44LjExIiwidXBkYXRlZEluVmVyIjoiMzYuMTA3LjIiLCJ0YXJnZXRCcmFuY2giOiJtYXN0ZXIifQ==--> Reviewed-on: https://git.apud.pl/jacek/adventofcode/pulls/32 Co-authored-by: Renovate <renovate@apud.pl> Co-committed-by: Renovate <renovate@apud.pl>
pip list
from the virtual environment you are usingBug description and minimal example
https://github.com/flying-sheep/pytest-doctest-import-twice
Versions
OS: Arch Linux
The text was updated successfully, but these errors were encountered: