gh-65961: Raise DeprecationWarning when __package__ differs from __spec__.parent (#97879)

Also remove `importlib.util.set_package()` which was already slated for removal.

Co-authored-by: Eric Snow <ericsnowcurrently@gmail.com>
This commit is contained in:
Brett Cannon 2022-10-05 15:00:45 -07:00 committed by GitHub
parent 2016bc54a2
commit c206e53bb7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 45 additions and 102 deletions

View file

@ -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)
# Set __loader__ and __package__ if missing.
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,9 +555,12 @@ 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 must be set. Its value must
The module's ``__package__`` attribute may be set. Its value must
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__``
@ -562,14 +568,23 @@ 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 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 ``__package__`` is not defined, ``__spec__.parent`` is used as
When ``__spec__.parent`` is not set, ``__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__