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

Pass importmode to import_path in DoctestModule #10088

Merged
merged 2 commits into from
Jun 29, 2022

Conversation

alicederyn
Copy link
Contributor

This allows doctest to be used with namespace modules in tox, as illustrated in the new unit test. See also #3396.

Copy link
Member

@asottile asottile left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@asottile
Copy link
Member

and just to confirm the failure mode of your test before the fix:

$ pytest testing/test_doctest.py
============================= test session starts ==============================
platform linux -- Python 3.8.10, pytest-7.2.0.dev179+g85000f037.d20220629, pluggy-1.0.0
rootdir: /tmp/pytest, configfile: pyproject.toml
plugins: hypothesis-6.48.1
collected 134 items                                                            

testing/test_doctest.py ....................F........................... [ 35%]
...........................x............................................ [ 89%]
..............                                                           [100%]

=================================== FAILURES ===================================
_________________________ TestDoctests.test_importmode _________________________

self = <test_doctest.TestDoctests object at 0x7f8fe2b40040>
pytester = <Pytester PosixPath('/tmp/pytest-of-asottile/pytest-10/test_importmode0')>

    def test_importmode(self, pytester: Pytester):
        p = pytester.makepyfile(
            **{
                "namespacepkg/innerpkg/__init__.py": "",
                "namespacepkg/innerpkg/a.py": """
                  def some_func():
                    return 42
                """,
                "namespacepkg/innerpkg/b.py": """
                  from namespacepkg.innerpkg.a import some_func
                  def my_func():
                    '''
                    >>> my_func()
                    42
                    '''
                    return some_func()
                """,
            }
        )
        reprec = pytester.inline_run(p, "--doctest-modules", "--import-mode=importlib")
>       reprec.assertoutcome(passed=1)
E       AssertionError: ([], [], [<CollectReport 'namespacepkg/innerpkg/b.py' lenresult=0 outcome='failed'>])
E       assert {'failed': 1,... 'skipped': 0} == {'failed': 0,... 'skipped': 0}
E         Omitting 1 identical items, use -vv to show
E         Differing items:
E         {'passed': 0} != {'passed': 1}
E         {'failed': 1} != {'failed': 0}
E         Use -v to get more diff

/tmp/pytest/testing/test_doctest.py:136: AssertionError
----------------------------- Captured stdout call -----------------------------
============================= test session starts ==============================
platform linux -- Python 3.8.10, pytest-7.2.0.dev179+g85000f037.d20220629, pluggy-1.0.0
rootdir: /tmp/pytest-of-asottile/pytest-10/test_importmode0
collected 0 items / 1 error

==================================== ERRORS ====================================
_________________ ERROR collecting namespacepkg/innerpkg/b.py __________________
namespacepkg/innerpkg/b.py:1: in <module>
    from namespacepkg.innerpkg.a import some_func
E   ModuleNotFoundError: No module named 'namespacepkg'
=========================== short test summary info ============================
ERROR namespacepkg/innerpkg/b.py - ModuleNotFoundError: No module named 'name...
!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
=============================== 1 error in 0.09s ===============================
=========================== short test summary info ============================
FAILED testing/test_doctest.py::TestDoctests::test_importmode - AssertionError: ([], [], [<CollectReport 'namespacepkg/innerpkg/b.py' lenre...
=================== 1 failed, 132 passed, 1 xfailed in 6.41s ===================

@asottile asottile merged commit 7dcabc1 into pytest-dev:main Jun 29, 2022
@asottile asottile added needs backport applied to PRs, indicates that it should be ported to the current bug-fix branch backport 7.1.x and removed backport 7.1.x labels Jun 29, 2022
@alicederyn alicederyn deleted the doctest.importmode branch June 30, 2022 08:20
nicoddemus added a commit to nicoddemus/pytest that referenced this pull request Jun 29, 2023
The initial implementation (in pytest-dev#7246) introduced the `importlib` mode, which
never added the imported module to `sys.modules`, so it included a test
to ensure calling `import_path` twice would yield different modules.

That proved problematic, so we started adding the imported module to `sys.modules`
in pytest-dev#7870, but failed to realize that given we are now changing `sys.modules`, we might
as well avoid importing it more than once.

Then pytest-dev#10088 came along, passing `importlib` also when importing application modules
(as opposed to only test modules before), which caused problems due to imports
having side-effects and the expectation being that they are imported only once.

With this PR, `import_path` returns the module immediately if already in
`sys.modules`.

Fix pytest-dev#10811, pytest-dev#10341
nicoddemus added a commit to nicoddemus/pytest that referenced this pull request Jun 29, 2023
The initial implementation (in pytest-dev#7246) introduced the `importlib` mode, which
never added the imported module to `sys.modules`, so it included a test
to ensure calling `import_path` twice would yield different modules.

Not adding modules to `sys.modules` proved problematic, so we began to add the imported module to `sys.modules`
in pytest-dev#7870, but failed to realize that given we are now changing `sys.modules`, we might
as well avoid importing it more than once.

Then pytest-dev#10088 came along, passing `importlib` also when importing application modules
(as opposed to only test modules before), which caused problems due to imports
having side-effects and the expectation being that they are imported only once.

With this PR, `import_path` returns the module immediately if already in
`sys.modules`.

Fix pytest-dev#10811, pytest-dev#10341
nicoddemus added a commit to nicoddemus/pytest that referenced this pull request Jul 1, 2023
The initial implementation (in pytest-dev#7246) introduced the `importlib` mode, which
never added the imported module to `sys.modules`, so it included a test
to ensure calling `import_path` twice would yield different modules.

Not adding modules to `sys.modules` proved problematic, so we began to add the imported module to `sys.modules`
in pytest-dev#7870, but failed to realize that given we are now changing `sys.modules`, we might
as well avoid importing it more than once.

Then pytest-dev#10088 came along, passing `importlib` also when importing application modules
(as opposed to only test modules before), which caused problems due to imports
having side-effects and the expectation being that they are imported only once.

With this PR, `import_path` returns the module immediately if already in
`sys.modules`.

Fix pytest-dev#10811, pytest-dev#10341
nicoddemus added a commit to nicoddemus/pytest that referenced this pull request Jul 1, 2023
The initial implementation (in pytest-dev#7246) introduced the `importlib` mode, which
never added the imported module to `sys.modules`, so it included a test
to ensure calling `import_path` twice would yield different modules.

Not adding modules to `sys.modules` proved problematic, so we began to add the imported module to `sys.modules`
in pytest-dev#7870, but failed to realize that given we are now changing `sys.modules`, we might
as well avoid importing it more than once.

Then pytest-dev#10088 came along, passing `importlib` also when importing application modules
(as opposed to only test modules before), which caused problems due to imports
having side-effects and the expectation being that they are imported only once.

With this PR, `import_path` returns the module immediately if already in
`sys.modules`.

Fix pytest-dev#10811, pytest-dev#10341
nicoddemus added a commit that referenced this pull request Jul 1, 2023
The initial implementation (in #7246) introduced the `importlib` mode, which
never added the imported module to `sys.modules`, so it included a test
to ensure calling `import_path` twice would yield different modules.

Not adding modules to `sys.modules` proved problematic, so we began to add the imported module to `sys.modules`
in #7870, but failed to realize that given we are now changing `sys.modules`, we might
as well avoid importing it more than once.

Then #10088 came along, passing `importlib` also when importing application modules
(as opposed to only test modules before), which caused problems due to imports
having side-effects and the expectation being that they are imported only once.

With this PR, `import_path` returns the module immediately if already in
`sys.modules`.

Fix #10811
Fix #10341
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs backport applied to PRs, indicates that it should be ported to the current bug-fix branch
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants