bpo-32303: Consistency fixes for namespace loaders#5481
Merged
warsaw merged 5 commits intopython:masterfrom Feb 2, 2018
Merged
bpo-32303: Consistency fixes for namespace loaders#5481warsaw merged 5 commits intopython:masterfrom
warsaw merged 5 commits intopython:masterfrom
Conversation
Also, repair a couple of tests.
brettcannon
approved these changes
Feb 2, 2018
Lib/test/test_importlib/util.py
Outdated
| is_package=is_package) | ||
| module.__spec__ = spec | ||
| module.__loader__ = loader | ||
| module.__loader__ = module.__loader__ = loader |
Member
There was a problem hiding this comment.
Any specific reason to double-assign the same attribute?
Member
Author
There was a problem hiding this comment.
Nope, good catch! I thought I needed that in a previous commit, but I don't, so removed!
warsaw
commented
Feb 2, 2018
Doc/library/importlib.rst
Outdated
| built-in modules and the filename for modules loaded from source. | ||
| Normally "origin" should be set, but it may be ``None`` (the default) | ||
| which indicates it is unspecified. | ||
| which indicates it is unspecified (e.g. for namespace pacakges). |
|
@warsaw: Please replace |
Contributor
|
Thanks @warsaw for the PR 🌮🎉.. I'm working now to backport this PR to: 3.6, 3.7. |
|
GH-5498 is a backport of this pull request to the 3.7 branch. |
miss-islington
pushed a commit
to miss-islington/cpython
that referenced
this pull request
Feb 2, 2018
* Make sure ``__spec__.loader`` matches ``__loader__`` for namespace packages. * Make sure ``__spec__.origin` matches ``__file__`` for namespace packages. https://bugs.python.org/issue32303 https://bugs.python.org/issue32305 (cherry picked from commit bbbcf86) Co-authored-by: Barry Warsaw <[email protected]>
Contributor
|
Sorry, @warsaw, I could not cleanly backport this to |
warsaw
added a commit
that referenced
this pull request
Feb 3, 2018
* Make sure ``__spec__.loader`` matches ``__loader__`` for namespace packages. * Make sure ``__spec__.origin` matches ``__file__`` for namespace packages. https://bugs.python.org/issue32303 https://bugs.python.org/issue32305 (cherry picked from commit bbbcf86) Co-authored-by: Barry Warsaw <[email protected]>
warsaw
added a commit
that referenced
this pull request
Feb 3, 2018
* Make sure ``__spec__.loader`` matches ``__loader__`` for namespace packages. * Make sure ``__spec__.origin` matches ``__file__`` for namespace packages. https://bugs.python.org/issue32303 https://bugs.python.org/issue32305. (cherry picked from commit bbbcf86) Co-authored-by: Barry Warsaw <[email protected]>
|
GH-5504 is a backport of this pull request to the 3.6 branch. |
warsaw
added a commit
that referenced
this pull request
Feb 3, 2018
…5504) * Make sure ``__spec__.loader`` matches ``__loader__`` for namespace packages. * Make sure ``__spec__.origin` matches ``__file__`` for namespace packages. https://bugs.python.org/issue32303 https://bugs.python.org/issue32305. (cherry picked from commit bbbcf86) Co-authored-by: Barry Warsaw <[email protected]>
warsaw
added a commit
to warsaw/cpython
that referenced
this pull request
Feb 26, 2018
…thonGH-5481) (python#5504)" This reverts commit a71397f.
PCManticore
added a commit
to pylint-dev/astroid
that referenced
this pull request
Jun 4, 2018
…pace package ModuleSpec.origin was changed in python/cpython#5481 to be None for namespace packages.
karlicoss
added a commit
to karlicoss/pytest
that referenced
this pull request
May 16, 2025
Previously, when running `--pyargs pkg`, if you didn't have `pkg/__init__.py`, pytest would fail with `ERROR: module or package not found: pkg (missing __init__.py?)`. Now it's discovering the package and tests inside it correctly. If used in conjunction with =consider_namespace_packages= in config, the test modules get correct `__package__` and `__name__` attributes. Fixes: pytest-dev#478 Relevant: pytest-dev#10569 In addition, remove `"namespace"` origin handling -- this value isn't used since python 3.8. See python/cpython#5481 and https://docs.python.org/3/library/importlib.html#importlib.machinery.ModuleSpec.submodule_search_locations .
karlicoss
added a commit
to karlicoss/pytest
that referenced
this pull request
May 16, 2025
Previously, when running `--pyargs pkg`, if you didn't have `pkg/__init__.py`, pytest would fail with `ERROR: module or package not found: pkg (missing __init__.py?)`. Now it's discovering the package and tests inside it correctly. If used in conjunction with `consider_namespace_packages` in config, test modules get correct `__package__` and `__name__` attributes as well. Fixes: pytest-dev#478 Relevant: - pytest-dev#2371 - pytest-dev#10569 In addition, remove `"namespace"` origin handling -- this value isn't used since python 3.8. See python/cpython#5481 and https://docs.python.org/3/library/importlib.html#importlib.machinery.ModuleSpec.submodule_search_locations .
karlicoss
added a commit
to karlicoss/pytest
that referenced
this pull request
May 16, 2025
Previously, when running `--pyargs pkg`, if you didn't have `pkg/__init__.py`, pytest would fail with `ERROR: module or package not found: pkg (missing __init__.py?)`. Now it's discovering the package and tests inside it correctly. If used in conjunction with `consider_namespace_packages` in config, test modules get correct `__package__` and `__name__` attributes as well. Fixes: pytest-dev#478 Other relevant issues: - pytest-dev#2371 - pytest-dev#10569 In addition, remove `"namespace"` origin handling -- this value isn't used since python 3.8. See: - python/cpython#5481 - https://docs.python.org/3/library/importlib.html#importlib.machinery.ModuleSpec.submodule_search_locations .
karlicoss
added a commit
to karlicoss/pytest
that referenced
this pull request
May 16, 2025
Previously, when running `--pyargs pkg`, if you didn't have `pkg/__init__.py`, pytest would fail with `ERROR: module or package not found: pkg (missing __init__.py?)`. Now it's discovering the package and tests inside it correctly. If used in conjunction with `consider_namespace_packages` in config, test modules get correct `__package__` and `__name__` attributes as well. Fixes: pytest-dev#478 Other relevant issues: - pytest-dev#2371 - pytest-dev#10569 In addition, remove `"namespace"` origin handling -- this value isn't used since python 3.8. See: - python/cpython#5481 - https://docs.python.org/3/library/importlib.html#importlib.machinery.ModuleSpec.submodule_search_locations
karlicoss
added a commit
to karlicoss/pytest
that referenced
this pull request
Jul 9, 2025
Previously, when running `--pyargs pkg`, if you didn't have `pkg/__init__.py`, pytest would fail with `ERROR: module or package not found: pkg (missing __init__.py?)`. Now it's discovering the package and tests inside it correctly. If used in conjunction with `consider_namespace_packages` in config, test modules get correct `__package__` and `__name__` attributes as well. Fixes: pytest-dev#478 Other relevant issues: - pytest-dev#2371 - pytest-dev#10569 In addition, remove `"namespace"` origin handling -- this value isn't used since python 3.8. See: - python/cpython#5481 - https://docs.python.org/3/library/importlib.html#importlib.machinery.ModuleSpec.submodule_search_locations
nicoddemus
pushed a commit
to pytest-dev/pytest
that referenced
this pull request
Jul 12, 2025
…13426) Previously, when running `--pyargs pkg`, if you didn't have `pkg/__init__.py`, pytest would fail with `ERROR: module or package not found: pkg (missing __init__.py?)`. If used in conjunction with `consider_namespace_packages` in config, pytest discovers the package and tests inside it correctly. If used in conjunction with `consider_namespace_packages` in config, test modules get correct `__package__` and `__name__` attributes as well. In addition, remove `"namespace"` origin handling -- this value isn't used since python 3.8. See: - python/cpython#5481 - https://docs.python.org/3/library/importlib.html#importlib.machinery.ModuleSpec.submodule_search_locations Fixes #478 Fixes #2371 Fixes #10569
pull bot
pushed a commit
to mergify-ci-insights/pytest
that referenced
this pull request
Jul 16, 2025
…ytest-dev#13426) Previously, when running `--pyargs pkg`, if you didn't have `pkg/__init__.py`, pytest would fail with `ERROR: module or package not found: pkg (missing __init__.py?)`. If used in conjunction with `consider_namespace_packages` in config, pytest discovers the package and tests inside it correctly. If used in conjunction with `consider_namespace_packages` in config, test modules get correct `__package__` and `__name__` attributes as well. In addition, remove `"namespace"` origin handling -- this value isn't used since python 3.8. See: - python/cpython#5481 - https://docs.python.org/3/library/importlib.html#importlib.machinery.ModuleSpec.submodule_search_locations Fixes pytest-dev#478 Fixes pytest-dev#2371 Fixes pytest-dev#10569
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This branch actually fixes two inconsistencies for namespace package loaders.
__loader__and__spec__.loader. This inconsistency leads to namespace packages having a valid__loader__butNonefor__spec__.loader. This branch fixes it by ensuring that the two attributes start out the same. (As always, a user could change one or the other, and no attempt is made by Python to keep them in sync.)__file__and__spec__.origin. Here, namespace packages have a__spec__.originthat is the literal string"namespace", and they do not have a__file__attribute. Not only is this inconsistent, I believe it violates the documentation. The fix here is to set both__spec__.originand__file__toNonefor namespace packages.I suppose it's possible that this will break existing code, but I'd argue that because current behavior runs counter to the documentation and makes no sense given the inconsistencies, it is better to fix them. I propose this change be applied to 3.7 and 3.6, although if you, my friendly reviewer, disagrees about 3.6, we can talk about it!
This isn't a security issue to back porting to 3.5 is off the table.
https://bugs.python.org/issue32303