mirror of
https://github.com/python/cpython.git
synced 2025-12-23 09:19:18 +00:00
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:
parent
2016bc54a2
commit
c206e53bb7
9 changed files with 45 additions and 102 deletions
|
|
@ -1228,7 +1228,7 @@ def _calc___package__(globals):
|
|||
if spec is not None and package != spec.parent:
|
||||
_warnings.warn("__package__ != __spec__.parent "
|
||||
f"({package!r} != {spec.parent!r})",
|
||||
ImportWarning, stacklevel=3)
|
||||
DeprecationWarning, stacklevel=3)
|
||||
return package
|
||||
elif spec is not None:
|
||||
return spec.parent
|
||||
|
|
|
|||
|
|
@ -141,26 +141,6 @@ def _module_to_load(name):
|
|||
module.__initializing__ = False
|
||||
|
||||
|
||||
def set_package(fxn):
|
||||
"""Set __package__ on the returned module.
|
||||
|
||||
This function is deprecated.
|
||||
|
||||
"""
|
||||
@functools.wraps(fxn)
|
||||
def set_package_wrapper(*args, **kwargs):
|
||||
warnings.warn('The import system now takes care of this automatically; '
|
||||
'this decorator is slated for removal in Python 3.12',
|
||||
DeprecationWarning, stacklevel=2)
|
||||
module = fxn(*args, **kwargs)
|
||||
if getattr(module, '__package__', None) is None:
|
||||
module.__package__ = module.__name__
|
||||
if not hasattr(module, '__path__'):
|
||||
module.__package__ = module.__package__.rpartition('.')[0]
|
||||
return module
|
||||
return set_package_wrapper
|
||||
|
||||
|
||||
def set_loader(fxn):
|
||||
"""Set __loader__ on the returned module.
|
||||
|
||||
|
|
|
|||
|
|
@ -74,8 +74,8 @@ class Using__package__:
|
|||
self.assertEqual(module.__name__, 'pkg')
|
||||
|
||||
def test_warn_when_package_and_spec_disagree(self):
|
||||
# Raise an ImportWarning if __package__ != __spec__.parent.
|
||||
with self.assertWarns(ImportWarning):
|
||||
# Raise a DeprecationWarning if __package__ != __spec__.parent.
|
||||
with self.assertWarns(DeprecationWarning):
|
||||
self.import_module({'__package__': 'pkg.fake',
|
||||
'__spec__': FakeSpec('pkg.fakefake')})
|
||||
|
||||
|
|
|
|||
|
|
@ -252,69 +252,6 @@ class ModuleForLoaderTests:
|
|||
) = util.test_both(ModuleForLoaderTests, util=importlib_util)
|
||||
|
||||
|
||||
class SetPackageTests:
|
||||
|
||||
"""Tests for importlib.util.set_package."""
|
||||
|
||||
def verify(self, module, expect):
|
||||
"""Verify the module has the expected value for __package__ after
|
||||
passing through set_package."""
|
||||
fxn = lambda: module
|
||||
wrapped = self.util.set_package(fxn)
|
||||
with warnings.catch_warnings():
|
||||
warnings.simplefilter('ignore', DeprecationWarning)
|
||||
wrapped()
|
||||
self.assertTrue(hasattr(module, '__package__'))
|
||||
self.assertEqual(expect, module.__package__)
|
||||
|
||||
def test_top_level(self):
|
||||
# __package__ should be set to the empty string if a top-level module.
|
||||
# Implicitly tests when package is set to None.
|
||||
module = types.ModuleType('module')
|
||||
module.__package__ = None
|
||||
self.verify(module, '')
|
||||
|
||||
def test_package(self):
|
||||
# Test setting __package__ for a package.
|
||||
module = types.ModuleType('pkg')
|
||||
module.__path__ = ['<path>']
|
||||
module.__package__ = None
|
||||
self.verify(module, 'pkg')
|
||||
|
||||
def test_submodule(self):
|
||||
# Test __package__ for a module in a package.
|
||||
module = types.ModuleType('pkg.mod')
|
||||
module.__package__ = None
|
||||
self.verify(module, 'pkg')
|
||||
|
||||
def test_setting_if_missing(self):
|
||||
# __package__ should be set if it is missing.
|
||||
module = types.ModuleType('mod')
|
||||
if hasattr(module, '__package__'):
|
||||
delattr(module, '__package__')
|
||||
self.verify(module, '')
|
||||
|
||||
def test_leaving_alone(self):
|
||||
# If __package__ is set and not None then leave it alone.
|
||||
for value in (True, False):
|
||||
module = types.ModuleType('mod')
|
||||
module.__package__ = value
|
||||
self.verify(module, value)
|
||||
|
||||
def test_decorator_attrs(self):
|
||||
def fxn(module): pass
|
||||
with warnings.catch_warnings():
|
||||
warnings.simplefilter('ignore', DeprecationWarning)
|
||||
wrapped = self.util.set_package(fxn)
|
||||
self.assertEqual(wrapped.__name__, fxn.__name__)
|
||||
self.assertEqual(wrapped.__qualname__, fxn.__qualname__)
|
||||
|
||||
|
||||
(Frozen_SetPackageTests,
|
||||
Source_SetPackageTests
|
||||
) = util.test_both(SetPackageTests, util=importlib_util)
|
||||
|
||||
|
||||
class SetLoaderTests:
|
||||
|
||||
"""Tests importlib.util.set_loader()."""
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue