Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

gh-65961: Raise DeprecationWarning 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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 5, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Make a strong recommendation to use __spec__
  • Loading branch information
brettcannon committed Oct 5, 2022
commit cb558bd03447e68929c82a1a05f21d3e6bdd97af
17 changes: 13 additions & 4 deletions 17 Doc/reference/import.rst
Original file line number Diff line number Diff line change
Expand Up @@ -538,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
Expand All @@ -551,6 +555,9 @@ 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__`` attribute may be set. Its value must
brettcannon marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -561,16 +568,15 @@ the module.
submodules, to the parent package's name. See :pep:`366` for further
details.

This attribute is used instead of ``__name__`` to calculate explicit
relative imports for main modules, as defined in :pep:`366`. It is
expected to have the same value as ``__spec__.parent``.
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 must fall back to
:exc:`ImportWarning` is raised if import falls back to
``__package__`` instead of
:attr:`~importlib.machinery.ModuleSpec.parent`.

Expand Down Expand Up @@ -632,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__
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.