Issue #25791: Warn when __package__ != __spec__.parent.

In a previous change, __spec__.parent was prioritized over
__package__. That is a backwards-compatibility break, but we do
eventually want __spec__ to be the ground truth for module details. So
this change reverts the change in semantics and instead raises an
ImportWarning when __package__ != __spec__.parent to give people time
to adjust to using spec objects.
This commit is contained in:
Brett Cannon 2016-01-22 15:25:50 -08:00
parent 52c854a838
commit 849113af6b
8 changed files with 184 additions and 121 deletions

View file

@ -1032,11 +1032,17 @@ def _calc___package__(globals):
to represent that its proper value is unknown.
"""
spec = globals.get('__spec__')
if spec is not None:
return spec.parent
package = globals.get('__package__')
if package is None:
spec = globals.get('__spec__')
if package is not None:
if spec is not None and package != spec.parent:
_warnings.warn("__package__ != __spec__.parent "
f"({package!r} != {spec.parent!r})",
ImportWarning, stacklevel=3)
return package
elif spec is not None:
return spec.parent
else:
_warnings.warn("can't resolve package from __spec__ or __package__, "
"falling back on __name__ and __path__",
ImportWarning, stacklevel=3)