[New-bugs-announce] [issue25791] Raise an ImportWarning when __spec__.parent/__package__ isn't defined for a relative import

Brett Cannon report at bugs.python.org
Thu Dec 3 15:48:12 EST 2015


New submission from Brett Cannon:

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().

----------
components: Interpreter Core
messages: 255838
nosy: brett.cannon, eric.snow, ncoghlan
priority: low
severity: normal
stage: test needed
status: open
title: Raise an ImportWarning when __spec__.parent/__package__ isn't defined for a relative import
type: enhancement
versions: Python 3.6

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue25791>
_______________________________________


More information about the New-bugs-announce mailing list