classification
Title: Raise an ImportWarning when __spec__.parent/__package__ isn't defined for a relative import
Type: enhancement Stage: resolved
Components: Interpreter Core Versions: Python 3.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: brett.cannon Nosy List: brett.cannon, eric.snow, ncoghlan, python-dev, superluser
Priority: low Keywords: patch

Created on 2015-12-03 20:48 by brett.cannon, last changed 2016-01-22 23:27 by brett.cannon. This issue is now closed.

Files
File name Uploaded Description Edit
issue25791.patch superluser, 2016-01-13 21:32 review
issue25791_2.patch superluser, 2016-01-13 23:00 review
Messages (10)
msg255838 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2015-12-03 20:48
When you do a relative import, __package__ is used to help resolve it, but if it isn't defined we fall back on using __name__ and __path__. We should probably consider raising an ImportWarning if __package__ isn't defined so that some day we can consider cleaning up __import__ and its call signature to be a bit more sane and drop the `globals` argument. It would also help people catch errors where they went overboard deleting attributes off a module.

We should probably even extend it to start using __spec__.parent and then falling back to __package__, and only after both are found missing do we raise the ImportWarning about needing to use __name__ and __path__. That way __import__ can simply start taking in the spec of the calling module to do all of its work instead of having to pass all of globals().
msg258171 - (view) Author: Rose Ames (superluser) * Date: 2016-01-13 21:34
Patch with tests.  Not sure if importlib.h should be included?
msg258172 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2016-01-13 21:36
Including importlib.h doesn't hurt.
msg258173 - (view) Author: Rose Ames (superluser) * Date: 2016-01-13 21:39
that's what I figured.
msg258178 - (view) Author: Rose Ames (superluser) * Date: 2016-01-13 23:00
Thanks for the quick review, new patch uploaded.
msg258329 - (view) Author: Roundup Robot (python-dev) Date: 2016-01-15 21:33
New changeset 6908b2c9a404 by Brett Cannon in branch 'default':
Issue #25791: Raise an ImportWarning when __spec__ or __package__ are
https://hg.python.org/cpython/rev/6908b2c9a404
msg258330 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2016-01-15 21:33
Thanks for the patch, Rose! I did notice your review comments about the missing goto and the stacklevel and I simply added them myself.
msg258354 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2016-01-16 04:43
Favouring __spec__.parent over __package__ will break the documented workaround in PEP 366 for enabling explicit relative imports from __main__ even when a module is run directly instead of via -m:

    if __name__ == "__main__" and __package__ is None:
        __package__ = "expected.package.name"

It may be that workaround is something we *want* to break (as per http://bugs.python.org/issue21762#msg258332 ), but if so, it's at least worthy of a porting note in the What's New document.
msg258400 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2016-01-16 18:18
As I commented on another issue, I think I'm going to tweak the change to continue to prefer __package__, but raise ImportWarning when it doesn't match __spec__.parent. Once we do that for all attributes we can wait until Python 2.7 is done and then swap priority to __spec__ and raise a DeprecationWarning if __spec__ is missing. That way post-Python 2.7 we can move entirely to a spec-based import system.
msg258842 - (view) Author: Roundup Robot (python-dev) Date: 2016-01-22 23:27
New changeset 219c44fe8968 by Brett Cannon in branch 'default':
Issue #25791: Warn when __package__ != __spec__.parent.
https://hg.python.org/cpython/rev/219c44fe8968
History
Date User Action Args
2016-01-22 23:28:43brett.cannonlinkissue21762 dependencies
2016-01-22 23:27:30brett.cannonsetstatus: open -> closed
2016-01-22 23:27:05python-devsetmessages: + msg258842
2016-01-16 18:18:45brett.cannonsetstatus: closed -> open

messages: + msg258400
2016-01-16 04:43:41ncoghlansetmessages: + msg258354
2016-01-15 21:33:44brett.cannonsetstatus: open -> closed
resolution: fixed
messages: + msg258330

stage: test needed -> resolved
2016-01-15 21:33:11python-devsetnosy: + python-dev
messages: + msg258329
2016-01-14 00:20:29brett.cannonsetassignee: brett.cannon
2016-01-13 23:00:29superlusersetfiles: + issue25791_2.patch

messages: + msg258178
2016-01-13 21:39:28superlusersetmessages: + msg258173
2016-01-13 21:36:08brett.cannonsetmessages: + msg258172
2016-01-13 21:34:49superlusersetmessages: + msg258171
2016-01-13 21:32:36superlusersetfiles: + issue25791.patch
keywords: + patch
2015-12-05 22:27:56superlusersetnosy: + superluser
2015-12-03 20:48:12brett.cannoncreate