Namespace packages have inconsistent __file__ and __spec__.origin #76486
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
assignee = 'https://github.com/warsaw' closed_at = <Date 2018-02-03.04:21:56.274> created_at = <Date 2017-12-13.15:48:41.485> labels = ['3.7', '3.8'] title = 'Namespace packages have inconsistent __file__ and __spec__.origin' updated_at = <Date 2018-11-23.00:00:21.338> user = 'https://github.com/warsaw'
activity = <Date 2018-11-23.00:00:21.338> actor = 'maggyero' assignee = 'barry' closed = True closed_date = <Date 2018-02-03.04:21:56.274> closer = 'barry' components =  creation = <Date 2017-12-13.15:48:41.485> creator = 'barry' dependencies =  files =  hgrepos =  issue_num = 32305 keywords = ['patch'] message_count = 8.0 messages = ['308206', '311463', '312946', '313242', '313274', '313277', '313278', '330288'] nosy_count = 6.0 nosy_names = ['barry', 'eric.smith', 'nedbat', 'ned.deily', 'eric.snow', 'maggyero'] pr_nums = ['5481', '5994'] priority = 'normal' resolution = 'fixed' stage = 'resolved' status = 'closed' superseder = None type = None url = 'https://bugs.python.org/issue32305' versions = ['Python 3.7', 'Python 3.8']
The text was updated successfully, but these errors were encountered:
Along the lines of bpo-32303 there's another inconsistency in namespace package metadata. Let's say I have a namespace package:
>>> importlib_resources.tests.data03.namespace <module 'importlib_resources.tests.data03.namespace' (namespace)>
The package has no __file__ attribute, and it has a misleading __spec__.origin
>>> importlib_resources.tests.data03.namespace.__spec__.origin 'namespace' >>> importlib_resources.tests.data03.namespace.__file__ Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: module 'importlib_resources.tests.data03.namespace' has no attribute '__file__'
This is especially bad because the documentation for __spec__.origin implies a correlation to __file__, and says:
"Name of the place from which the module is loaded, e.g. “builtin” for built-in modules and the filename for modules loaded from source. Normally “origin” should be set, but it may be None (the default) which indicates it is unspecified."
I don't particularly like that its origin is "namespace". That's an odd special case that's unhelpful to test against (what if you import a non-namespace package from the directory "namespace"?)
What would break if __spec__.origin were (missing? or) None?
On Mar 5, 2018, at 10:33, Ned Batchelder <firstname.lastname@example.org> wrote:
Sounds good; I’ll work up a PR
@barry You gave 2 reasons for changing __spec__.origin and __file__ for namespace packages.
Your 1st reason:
As far as I know, a non-namespace package always has an __init__.py file, so if it is imported from a directory named "namespace" it has a __spec__.origin and __file__ attributes equal to "path/to/package/namespace/init.py". So I don’t see the problem here with having a "namespace" origin for namespace package specs.
In addition, PEP-420 that introduced implicit namespace packages in Python 3.3 clearly stated that having no __file__ attribute was intended for namespace packages, and more generally was left a the discretion of the module’s loader and no more limited to built-in modules (https://www.python.org/dev/peps/pep-0420/#module-reprs):
Your 2nd reason:
I agree here, so why not updating the documentation instead of changing the implementation which followed PEP-420?