| 1 | { |
| 2 | "task_id": "pytest-dev__pytest-11148", |
| 3 | "setup_info": { |
| 4 | "repo_path": "/media/media0/yuntong/SWE-bench/testbed/pytest-dev__pytest/setup_pytest-dev__pytest__8.0", |
| 5 | "env_name": "setup_pytest-dev__pytest__8.0", |
| 6 | "pre_install": [], |
| 7 | "install": "python -m pip install -e .", |
| 8 | "test_cmd": "pytest -rA testing/acceptance_test.py testing/test_pathlib.py" |
| 9 | }, |
| 10 | "task_info": { |
| 11 | "instance_id": "pytest-dev__pytest-11148", |
| 12 | "base_commit": "2f7415cfbc4b6ca62f9013f1abd27136f46b9653", |
| 13 | "hints_text": "In pmxbot/pmxbot@3adc54c, I've managed to pare down the project to a bare minimum reproducer. The issue only happens when `import-mode=importlib` and `doctest-modules` and one of the modules imports another module.\r\n\r\nThis issue may be related to (or same as) #10341.\r\n\r\nI think you'll agree this is pretty basic behavior that should be supported.\r\n\r\nI'm not even aware of a good workaround.\nHey @jaraco, thanks for the reproducer! \r\n\r\nI found the problem, will open a PR shortly.", |
| 14 | "created_at": "2023-06-29T00:04:33Z", |
| 15 | "test_patch": "diff --git a/testing/acceptance_test.py b/testing/acceptance_test.py\n--- a/testing/acceptance_test.py\n+++ b/testing/acceptance_test.py\n@@ -1315,3 +1315,38 @@ def test_stuff():\n )\n res = pytester.runpytest()\n res.stdout.fnmatch_lines([\"*Did you mean to use `assert` instead of `return`?*\"])\n+\n+\n+def test_doctest_and_normal_imports_with_importlib(pytester: Pytester) -> None:\n+ \"\"\"\n+ Regression test for #10811: previously import_path with ImportMode.importlib would\n+ not return a module if already in sys.modules, resulting in modules being imported\n+ multiple times, which causes problems with modules that have import side effects.\n+ \"\"\"\n+ # Uses the exact reproducer form #10811, given it is very minimal\n+ # and illustrates the problem well.\n+ pytester.makepyfile(\n+ **{\n+ \"pmxbot/commands.py\": \"from . import logging\",\n+ \"pmxbot/logging.py\": \"\",\n+ \"tests/__init__.py\": \"\",\n+ \"tests/test_commands.py\": \"\"\"\n+ import importlib\n+ from pmxbot import logging\n+\n+ class TestCommands:\n+ def test_boo(self):\n+ assert importlib.import_module('pmxbot.logging') is logging\n+ \"\"\",\n+ }\n+ )\n+ pytester.makeini(\n+ \"\"\"\n+ [pytest]\n+ addopts=\n+ --doctest-modules\n+ --import-mode importlib\n+ \"\"\"\n+ )\n+ result = pytester.runpytest_subprocess()\n+ result.stdout.fnmatch_lines(\"*1 passed*\")\ndiff --git a/testing/test_pathlib.py b/testing/test_pathlib.py\n--- a/testing/test_pathlib.py\n+++ b/testing/test_pathlib.py\n@@ -7,6 +7,7 @@\n from types import ModuleType\n from typing import Any\n from typing import Generator\n+from typing import Iterator\n \n import pytest\n from _pytest.monkeypatch import MonkeyPatch\n@@ -282,29 +283,36 @@ def test_invalid_path(self, tmp_path: Path) -> None:\n import_path(tmp_path / \"invalid.py\", root=tmp_path)\n \n @pytest.fixture\n- def simple_module(self, tmp_path: Path) -> Path:\n- fn = tmp_path / \"_src/tests/mymod.py\"\n+ def simple_module(\n+ self, tmp_path: Path, request: pytest.FixtureRequest\n+ ) -> Iterator[Path]:\n+ name = f\"mymod_{request.node.name}\"\n+ fn = tmp_path / f\"_src/tests/{name}.py\"\n fn.parent.mkdir(parents=True)\n fn.write_text(\"def foo(x): return 40 + x\", encoding=\"utf-8\")\n- return fn\n+ module_name = module_name_from_path(fn, root=tmp_path)\n+ yield fn\n+ sys.modules.pop(module_name, None)\n \n- def test_importmode_importlib(self, simple_module: Path, tmp_path: Path) -> None:\n+ def test_importmode_importlib(\n+ self, simple_module: Path, tmp_path: Path, request: pytest.FixtureRequest\n+ ) -> None:\n \"\"\"`importlib` mode does not change sys.path.\"\"\"\n module = import_path(simple_module, mode=\"importlib\", root=tmp_path)\n assert module.foo(2) == 42 # type: ignore[attr-defined]\n assert str(simple_module.parent) not in sys.path\n assert module.__name__ in sys.modules\n- assert module.__name__ == \"_src.tests.mymod\"\n+ assert module.__name__ == f\"_src.tests.mymod_{request.node.name}\"\n assert \"_src\" in sys.modules\n assert \"_src.tests\" in sys.modules\n \n- def test_importmode_twice_is_different_module(\n+ def test_remembers_previous_imports(\n self, simple_module: Path, tmp_path: Path\n ) -> None:\n- \"\"\"`importlib` mode always returns a new module.\"\"\"\n+ \"\"\"`importlib` mode called remembers previous module (#10341, #10811).\"\"\"\n module1 = import_path(simple_module, mode=\"importlib\", root=tmp_path)\n module2 = import_path(simple_module, mode=\"importlib\", root=tmp_path)\n- assert module1 is not module2\n+ assert module1 is module2\n \n def test_no_meta_path_found(\n self, simple_module: Path, monkeypatch: MonkeyPatch, tmp_path: Path\n@@ -317,6 +325,9 @@ def test_no_meta_path_found(\n # mode='importlib' fails if no spec is found to load the module\n import importlib.util\n \n+ # Force module to be re-imported.\n+ del sys.modules[module.__name__]\n+\n monkeypatch.setattr(\n importlib.util, \"spec_from_file_location\", lambda *args: None\n )\n", |
| 16 | "repo": "pytest-dev/pytest", |
| 17 | "problem_statement": "Module imported twice under import-mode=importlib\nIn pmxbot/pmxbot@7f189ad, I'm attempting to switch pmxbot off of pkg_resources style namespace packaging to PEP 420 namespace packages. To do so, I've needed to switch to `importlib` for the `import-mode` and re-organize the tests to avoid import errors on the tests.\r\n\r\nYet even after working around these issues, the tests are failing when the effect of `core.initialize()` doesn't seem to have had any effect.\r\n\r\nInvestigating deeper, I see that initializer is executed and performs its actions (setting a class variable `pmxbot.logging.Logger.store`), but when that happens, there are two different versions of `pmxbot.logging` present, one in `sys.modules` and another found in `tests.unit.test_commands.logging`:\r\n\r\n```\r\n=========================================================================== test session starts ===========================================================================\r\nplatform darwin -- Python 3.11.1, pytest-7.2.0, pluggy-1.0.0\r\ncachedir: .tox/python/.pytest_cache\r\nrootdir: /Users/jaraco/code/pmxbot/pmxbot, configfile: pytest.ini\r\nplugins: black-0.3.12, mypy-0.10.3, jaraco.test-5.3.0, checkdocs-2.9.0, flake8-1.1.1, enabler-2.0.0, jaraco.mongodb-11.2.1, pmxbot-1122.14.3.dev13+g7f189ad\r\ncollected 421 items / 180 deselected / 241 selected \r\nrun-last-failure: rerun previous 240 failures (skipped 14 files)\r\n\r\ntests/unit/test_commands.py E\r\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> traceback >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\r\n\r\ncls = <class 'tests.unit.test_commands.TestCommands'>\r\n\r\n @classmethod\r\n def setup_class(cls):\r\n path = os.path.dirname(os.path.abspath(__file__))\r\n configfile = os.path.join(path, 'testconf.yaml')\r\n config = pmxbot.dictlib.ConfigDict.from_yaml(configfile)\r\n cls.bot = core.initialize(config)\r\n> logging.Logger.store.message(\"logged\", \"testrunner\", \"some text\")\r\nE AttributeError: type object 'Logger' has no attribute 'store'\r\n\r\ntests/unit/test_commands.py:37: AttributeError\r\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> entering PDB >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\r\n\r\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> PDB post_mortem (IO-capturing turned off) >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\r\n> /Users/jaraco/code/pmxbot/pmxbot/tests/unit/test_commands.py(37)setup_class()\r\n-> logging.Logger.store.message(\"logged\", \"testrunner\", \"some text\")\r\n(Pdb) logging.Logger\r\n<class 'pmxbot.logging.Logger'>\r\n(Pdb) logging\r\n<module 'pmxbot.logging' from '/Users/jaraco/code/pmxbot/pmxbot/pmxbot/logging.py'>\r\n(Pdb) import sys\r\n(Pdb) sys.modules['pmxbot.logging']\r\n<module 'pmxbot.logging' from '/Users/jaraco/code/pmxbot/pmxbot/pmxbot/logging.py'>\r\n(Pdb) sys.modules['pmxbot.logging'] is logging\r\nFalse\r\n```\r\n\r\nI haven't yet made a minimal reproducer, but I wanted to first capture this condition.\r\n\n", |
| 18 | "version": "8.0", |
| 19 | "FAIL_TO_PASS": [ |
| 20 | "testing/acceptance_test.py::test_doctest_and_normal_imports_with_importlib", |
| 21 | "testing/test_pathlib.py::TestImportPath::test_remembers_previous_imports" |
| 22 | ], |
| 23 | "PASS_TO_PASS": [ |
| 24 | "testing/acceptance_test.py::TestDurations::test_calls", |
| 25 | "testing/acceptance_test.py::TestDurations::test_calls_show_2", |
| 26 | "testing/acceptance_test.py::TestDurations::test_calls_showall", |
| 27 | "testing/acceptance_test.py::TestDurations::test_calls_showall_verbose", |
| 28 | "testing/acceptance_test.py::TestDurations::test_with_deselected", |
| 29 | "testing/acceptance_test.py::TestDurations::test_with_failing_collection", |
| 30 | "testing/acceptance_test.py::TestDurations::test_with_not", |
| 31 | "testing/acceptance_test.py::TestDurationsWithFixture::test_setup_function", |
| 32 | "testing/acceptance_test.py::TestGeneralUsage::test_assertion_rewrite[append]", |
| 33 | "testing/acceptance_test.py::TestGeneralUsage::test_assertion_rewrite[importlib]", |
| 34 | "testing/acceptance_test.py::TestGeneralUsage::test_assertion_rewrite[prepend]", |
| 35 | "testing/acceptance_test.py::TestGeneralUsage::test_better_reporting_on_conftest_load_failure", |
| 36 | "testing/acceptance_test.py::TestGeneralUsage::test_config_error", |
| 37 | "testing/acceptance_test.py::TestGeneralUsage::test_config_preparse_plugin_option", |
| 38 | "testing/acceptance_test.py::TestGeneralUsage::test_conftest_printing_shows_if_error", |
| 39 | "testing/acceptance_test.py::TestGeneralUsage::test_direct_addressing_notfound", |
| 40 | "testing/acceptance_test.py::TestGeneralUsage::test_direct_addressing_selects", |
| 41 | "testing/acceptance_test.py::TestGeneralUsage::test_directory_skipped", |
| 42 | "testing/acceptance_test.py::TestGeneralUsage::test_docstring_on_hookspec", |
| 43 | "testing/acceptance_test.py::TestGeneralUsage::test_early_hook_configure_error_issue38", |
| 44 | "testing/acceptance_test.py::TestGeneralUsage::test_early_hook_error_issue38_1", |
| 45 | "testing/acceptance_test.py::TestGeneralUsage::test_early_load_setuptools_name[False]", |
| 46 | "testing/acceptance_test.py::TestGeneralUsage::test_early_load_setuptools_name[True]", |
| 47 | "testing/acceptance_test.py::TestGeneralUsage::test_early_skip", |
| 48 | "testing/acceptance_test.py::TestGeneralUsage::test_file_not_found", |
| 49 | "testing/acceptance_test.py::TestGeneralUsage::test_file_not_found_unconfigure_issue143", |
| 50 | "testing/acceptance_test.py::TestGeneralUsage::test_getsourcelines_error_issue553", |
| 51 | "testing/acceptance_test.py::TestGeneralUsage::test_initialization_error_issue49", |
| 52 | "testing/acceptance_test.py::TestGeneralUsage::test_issue109_sibling_conftests_not_loaded", |
| 53 | "testing/acceptance_test.py::TestGeneralUsage::test_issue134_report_error_when_collecting_member[test_fun.py::test_a]", |
| 54 | "testing/acceptance_test.py::TestGeneralUsage::test_issue88_initial_file_multinodes", |
| 55 | "testing/acceptance_test.py::TestGeneralUsage::test_issue93_initialnode_importing_capturing", |
| 56 | "testing/acceptance_test.py::TestGeneralUsage::test_multiple_items_per_collector_byid", |
| 57 | "testing/acceptance_test.py::TestGeneralUsage::test_namespace_import_doesnt_confuse_import_hook", |
| 58 | "testing/acceptance_test.py::TestGeneralUsage::test_nested_import_error", |
| 59 | "testing/acceptance_test.py::TestGeneralUsage::test_not_collectable_arguments", |
| 60 | "testing/acceptance_test.py::TestGeneralUsage::test_parametrized_with_bytes_regex", |
| 61 | "testing/acceptance_test.py::TestGeneralUsage::test_parametrized_with_null_bytes", |
| 62 | "testing/acceptance_test.py::TestGeneralUsage::test_plugins_given_as_strings", |
| 63 | "testing/acceptance_test.py::TestGeneralUsage::test_report_all_failed_collections_initargs", |
| 64 | "testing/acceptance_test.py::TestGeneralUsage::test_root_conftest_syntax_error", |
| 65 | "testing/acceptance_test.py::TestGeneralUsage::test_skip_on_generated_funcarg_id", |
| 66 | "testing/acceptance_test.py::TestGeneralUsage::test_unknown_option", |
| 67 | "testing/acceptance_test.py::TestInvocationVariants::test_cmdline_python_namespace_package", |
| 68 | "testing/acceptance_test.py::TestInvocationVariants::test_cmdline_python_package", |
| 69 | "testing/acceptance_test.py::TestInvocationVariants::test_cmdline_python_package_not_exists", |
| 70 | "testing/acceptance_test.py::TestInvocationVariants::test_cmdline_python_package_symlink", |
| 71 | "testing/acceptance_test.py::TestInvocationVariants::test_core_backward_compatibility", |
| 72 | "testing/acceptance_test.py::TestInvocationVariants::test_doctest_id", |
| 73 | "testing/acceptance_test.py::TestInvocationVariants::test_double_pytestcmdline", |
| 74 | "testing/acceptance_test.py::TestInvocationVariants::test_earlyinit", |
| 75 | "testing/acceptance_test.py::TestInvocationVariants::test_has_plugin", |
| 76 | "testing/acceptance_test.py::TestInvocationVariants::test_import_star_pytest", |
| 77 | "testing/acceptance_test.py::TestInvocationVariants::test_invoke_plugin_api", |
| 78 | "testing/acceptance_test.py::TestInvocationVariants::test_invoke_test_and_doctestmodules", |
| 79 | "testing/acceptance_test.py::TestInvocationVariants::test_invoke_with_invalid_type", |
| 80 | "testing/acceptance_test.py::TestInvocationVariants::test_invoke_with_path", |
| 81 | "testing/acceptance_test.py::TestInvocationVariants::test_pyargs_filename_looks_like_module", |
| 82 | "testing/acceptance_test.py::TestInvocationVariants::test_pyargs_importerror", |
| 83 | "testing/acceptance_test.py::TestInvocationVariants::test_pyargs_only_imported_once", |
| 84 | "testing/acceptance_test.py::TestInvocationVariants::test_pydoc", |
| 85 | "testing/acceptance_test.py::TestInvocationVariants::test_python_minus_m_invocation_fail", |
| 86 | "testing/acceptance_test.py::TestInvocationVariants::test_python_minus_m_invocation_ok", |
| 87 | "testing/acceptance_test.py::TestInvocationVariants::test_python_pytest_package", |
| 88 | "testing/acceptance_test.py::test_deferred_hook_checking", |
| 89 | "testing/acceptance_test.py::test_fixture_mock_integration", |
| 90 | "testing/acceptance_test.py::test_fixture_order_respects_scope", |
| 91 | "testing/acceptance_test.py::test_fixture_values_leak", |
| 92 | "testing/acceptance_test.py::test_frame_leak_on_failing_test", |
| 93 | "testing/acceptance_test.py::test_function_return_non_none_warning", |
| 94 | "testing/acceptance_test.py::test_import_plugin_unicode_name", |
| 95 | "testing/acceptance_test.py::test_no_brokenpipeerror_message", |
| 96 | "testing/acceptance_test.py::test_pdb_can_be_rewritten", |
| 97 | "testing/acceptance_test.py::test_pytest_plugins_as_module", |
| 98 | "testing/acceptance_test.py::test_tee_stdio_captures_and_live_prints", |
| 99 | "testing/acceptance_test.py::test_usage_error_code", |
| 100 | "testing/acceptance_test.py::test_warn_on_async_function", |
| 101 | "testing/acceptance_test.py::test_warn_on_async_gen_function", |
| 102 | "testing/acceptance_test.py::test_zipimport_hook", |
| 103 | "testing/test_pathlib.py::TestFNMatcherPort::test_matching[*.py-bar/foo.py]", |
| 104 | "testing/test_pathlib.py::TestFNMatcherPort::test_matching[*.py-foo.py]", |
| 105 | "testing/test_pathlib.py::TestFNMatcherPort::test_matching[/c/*.py-/c/foo.py]", |
| 106 | "testing/test_pathlib.py::TestFNMatcherPort::test_matching[/c/foo/*.py-/c/foo/foo.py]", |
| 107 | "testing/test_pathlib.py::TestFNMatcherPort::test_matching[test_*.py-foo/test_foo.py]", |
| 108 | "testing/test_pathlib.py::TestFNMatcherPort::test_matching[tests/**/doc/**/test*.py-tests/foo/doc/bar/test_foo.py]", |
| 109 | "testing/test_pathlib.py::TestFNMatcherPort::test_matching[tests/**/doc/test*.py-tests/foo/bar/doc/test_foo.py]", |
| 110 | "testing/test_pathlib.py::TestFNMatcherPort::test_matching[tests/**/test*.py-tests/foo/test_foo.py]", |
| 111 | "testing/test_pathlib.py::TestFNMatcherPort::test_matching[tests/*.py-tests/foo.py]", |
| 112 | "testing/test_pathlib.py::TestFNMatcherPort::test_matching_abspath", |
| 113 | "testing/test_pathlib.py::TestFNMatcherPort::test_not_matching[*.py-foo.pyc]", |
| 114 | "testing/test_pathlib.py::TestFNMatcherPort::test_not_matching[*.py-foo/foo.pyc]", |
| 115 | "testing/test_pathlib.py::TestFNMatcherPort::test_not_matching[/c/*.py-/d/foo.py]", |
| 116 | "testing/test_pathlib.py::TestFNMatcherPort::test_not_matching[/c/foo/*.py-/d/foo/foo.py]", |
| 117 | "testing/test_pathlib.py::TestFNMatcherPort::test_not_matching[tests/**/doc/test*.py-tests/foo/bar/doc/foo.py]", |
| 118 | "testing/test_pathlib.py::TestFNMatcherPort::test_not_matching[tests/**/doc/test*.py-tests/foo/bar/test_foo.py]", |
| 119 | "testing/test_pathlib.py::TestFNMatcherPort::test_not_matching[tests/**/test*.py-foo/test_foo.py]", |
| 120 | "testing/test_pathlib.py::TestFNMatcherPort::test_not_matching[tests/**/test*.py-tests/foo.py]", |
| 121 | "testing/test_pathlib.py::TestFNMatcherPort::test_not_matching[tests/*.py-foo/foo.py]", |
| 122 | "testing/test_pathlib.py::TestImportLibMode::test_importmode_importlib_with_dataclass", |
| 123 | "testing/test_pathlib.py::TestImportLibMode::test_importmode_importlib_with_pickle", |
| 124 | "testing/test_pathlib.py::TestImportLibMode::test_importmode_importlib_with_pickle_separate_modules", |
| 125 | "testing/test_pathlib.py::TestImportLibMode::test_insert_missing_modules", |
| 126 | "testing/test_pathlib.py::TestImportLibMode::test_module_name_from_path", |
| 127 | "testing/test_pathlib.py::TestImportLibMode::test_parent_contains_child_module_attribute", |
| 128 | "testing/test_pathlib.py::TestImportPath::test_a", |
| 129 | "testing/test_pathlib.py::TestImportPath::test_b", |
| 130 | "testing/test_pathlib.py::TestImportPath::test_c", |
| 131 | "testing/test_pathlib.py::TestImportPath::test_check_filepath_consistency", |
| 132 | "testing/test_pathlib.py::TestImportPath::test_d", |
| 133 | "testing/test_pathlib.py::TestImportPath::test_dir", |
| 134 | "testing/test_pathlib.py::TestImportPath::test_ensuresyspath_append", |
| 135 | "testing/test_pathlib.py::TestImportPath::test_import_after", |
| 136 | "testing/test_pathlib.py::TestImportPath::test_import_path_missing_file", |
| 137 | "testing/test_pathlib.py::TestImportPath::test_importmode_importlib", |
| 138 | "testing/test_pathlib.py::TestImportPath::test_invalid_path", |
| 139 | "testing/test_pathlib.py::TestImportPath::test_issue131_on__init__", |
| 140 | "testing/test_pathlib.py::TestImportPath::test_messy_name", |
| 141 | "testing/test_pathlib.py::TestImportPath::test_no_meta_path_found", |
| 142 | "testing/test_pathlib.py::TestImportPath::test_renamed_dir_creates_mismatch", |
| 143 | "testing/test_pathlib.py::TestImportPath::test_smoke_test", |
| 144 | "testing/test_pathlib.py::test_access_denied_during_cleanup", |
| 145 | "testing/test_pathlib.py::test_bestrelpath", |
| 146 | "testing/test_pathlib.py::test_commonpath", |
| 147 | "testing/test_pathlib.py::test_get_extended_length_path_str", |
| 148 | "testing/test_pathlib.py::test_long_path_during_cleanup", |
| 149 | "testing/test_pathlib.py::test_package_unimportable", |
| 150 | "testing/test_pathlib.py::test_resolve_package_path", |
| 151 | "testing/test_pathlib.py::test_suppress_error_removing_lock", |
| 152 | "testing/test_pathlib.py::test_visit_ignores_errors" |
| 153 | ], |
| 154 | "environment_setup_commit": "10056865d2a4784934ce043908a0e78d0578f677", |
| 155 | "patch": "diff --git a/src/_pytest/pathlib.py b/src/_pytest/pathlib.py\n--- a/src/_pytest/pathlib.py\n+++ b/src/_pytest/pathlib.py\n@@ -523,6 +523,8 @@ def import_path(\n \n if mode is ImportMode.importlib:\n module_name = module_name_from_path(path, root)\n+ with contextlib.suppress(KeyError):\n+ return sys.modules[module_name]\n \n for meta_importer in sys.meta_path:\n spec = meta_importer.find_spec(module_name, [str(path.parent)])\n", |
| 156 | "pr_link": "https://github.com/pytest-dev/pytest/pull/11148" |
| 157 | } |
| 158 | } |