Title: Deprecate __loader__, __package__, __file__, and __cached__ on modules
Type: Stage: test needed
Components: Library (Lib) Versions: Python 3.8
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: barry, brett.cannon, eric.snow, ncoghlan, rhettinger
Priority: low Keywords:

Created on 2018-04-13 21:48 by brett.cannon, last changed 2018-04-20 20:20 by rhettinger.

Messages (4)
msg315269 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2018-04-13 21:48
If you look at you will notice there are a lot of attributes on modules. But since the introduction of module specs (, all of those attributes became redundant.

Now some attributes can never be taken away based on common idioms in the Python community. __name__ can't go due to `if __name__ == '__main__'`. __path__ can't go due to people manipulating it with e.g. pkg_resources or pkgutil. The rest, though, don't have such strong idioms tied to them (and in the case of __package__, has actually been made officially redundant by the import system since Python 3.6). That means setting them is unnecessary and any time someone wants to read or potentially manipulate them they have to choose between those attributes or the spec (or update both). Either way it can lead to bugs (I know I have nearly forgotten to set both the spec and the attribute before).

I don't know if we can easily deprecate these attributes using __getattr__ on modules as added in Python 3.7, or if this is more of a documentation thing and need a long period of clearly messaging that these redundant attributes will no longer be set at some point.
msg315276 - (view) Author: Barry A. Warsaw (barry) * (Python committer) Date: 2018-04-14 00:21
I agree.  It should also be pointed out that we've had inconsistencies between the module attributes and the spec attributes, and even fixing those has lead to problems.  There should be a single source of truth, and the module spec should be that.

+1, and I agree about not being able to __name__ and __path__.
msg315521 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2018-04-20 16:28
-1 from me for __package__, as setting that's the preferred way for directly executed scripts to fix their explicit relative imports (rather than trying to retroactively fix their spec). ( also doesn't say anything about __package__ being deprecated)

I'd be OK with seeing __file__/__cached__/__loader__ get deprecated, but before they went away, I'd like to see "importlib.util.get_filename()", "importlib.util.get_cache_filename()", and "importlib.util.get_loader()"  helpers added.
msg315534 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2018-04-20 20:20
+1 from me.  The visual clutter in dir() has proven to be somewhat distracting in introduction-to-python courses.
Date User Action Args
2018-04-20 20:20:53rhettingersetnosy: + rhettinger
messages: + msg315534
2018-04-20 16:28:14ncoghlansetnosy: + ncoghlan
messages: + msg315521
2018-04-14 00:21:05barrysetnosy: + barry
messages: + msg315276
2018-04-13 21:48:26brett.cannonsetpriority: normal -> low
2018-04-13 21:48:22brett.cannoncreate