Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork32.4k
gh-65961: RaiseDeprecationWarning
when__package__
differs from__spec__.parent
#97879
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
6 commits Select commitHold shift + click to select a range
fa3ac86
gh-65961: Raise `DeprecationWarning` when `__package__` differs from …
brettcannona5bae06
Merge branch 'main' into deprecate-__package__
brettcannon380a20b
List the removal of `__package__` in What's New
brettcannon820e677
Merge branch 'deprecate-__package__' of https://github.com/brettcanno…
brettcannone4e2f26
Update Misc/NEWS.d/next/Core and Builtins/2022-10-05-00-37-27.gh-issu…
brettcannoncb558bd
Make a strong recommendation to use `__spec__`
brettcannonFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -358,7 +358,6 @@ of what happens during the loading portion of import:: | ||
sys.modules[spec.name] = module | ||
elif not hasattr(spec.loader, 'exec_module'): | ||
module = spec.loader.load_module(spec.name) | ||
else: | ||
sys.modules[spec.name] = module | ||
try: | ||
@@ -539,6 +538,10 @@ The import machinery fills in these attributes on each module object | ||
during loading, based on the module's spec, before the loader executes | ||
the module. | ||
It is **strongly** recommended that you rely on :attr:`__spec__` and | ||
its attributes instead of any of the other individual attributes | ||
listed below. | ||
.. attribute:: __name__ | ||
The ``__name__`` attribute must be set to the fully qualified name of | ||
@@ -552,24 +555,36 @@ the module. | ||
for introspection, but can be used for additional loader-specific | ||
functionality, for example getting data associated with a loader. | ||
It is **strongly** recommended that you rely on :attr:`__spec__` | ||
instead instead of this attribute. | ||
.. attribute:: __package__ | ||
The module's ``__package__`` attributemay be set. Its value must | ||
brettcannon marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
be a string, but it can be the same value as its ``__name__``. When | ||
the module is a package, its ``__package__`` value should be set to | ||
its ``__name__``. When the module is not a package, ``__package__`` | ||
should be set to the empty string for top-level modules, or for | ||
submodules, to the parent package's name. See :pep:`366` for further | ||
details. | ||
It is **strongly** recommended that you rely on :attr:`__spec__` | ||
instead instead of this attribute. | ||
.. versionchanged:: 3.6 | ||
The value of ``__package__`` is expected to be the same as | ||
``__spec__.parent``. | ||
.. versionchanged:: 3.10 | ||
:exc:`ImportWarning` is raised if import falls back to | ||
``__package__`` instead of | ||
:attr:`~importlib.machinery.ModuleSpec.parent`. | ||
.. versionchanged:: 3.12 | ||
Raise :exc:`DeprecationWarning` instead of :exc:`ImportWarning` | ||
when falling back to ``__package__``. | ||
.. attribute:: __spec__ | ||
The ``__spec__`` attribute must be set to the module spec that was | ||
@@ -578,7 +593,7 @@ the module. | ||
interpreter startup <programs>`. The one exception is ``__main__``, | ||
where ``__spec__`` is :ref:`set to None in some cases <main_spec>`. | ||
When ``__spec__.parent`` is notset, ``__package__`` is used as | ||
a fallback. | ||
.. versionadded:: 3.4 | ||
@@ -623,6 +638,9 @@ the module. | ||
if a loader can load from a cached module but otherwise does not load | ||
from a file, that atypical scenario may be appropriate. | ||
It is **strongly** recommended that you rely on :attr:`__spec__` | ||
instead instead of ``__cached__``. | ||
.. _package-path-rules: | ||
module.__path__ | ||
12 changes: 12 additions & 0 deletionsDoc/whatsnew/3.12.rst
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
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
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
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
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
5 changes: 5 additions & 0 deletionsMisc/NEWS.d/next/Core and Builtins/2022-10-05-00-37-27.gh-issue-65961.z0Ys0y.rst
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
When ``__package__`` is different than ``__spec__.parent``, raise a | ||
``DeprecationWarning`` instead of ``ImportWarning``. | ||
Also remove ``importlib.util.set_package()`` which was scheduled for | ||
removal. |
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
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.