Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Namespace packages have inconsistent __file__ and __spec__.origin #76486

Closed
warsaw opened this issue Dec 13, 2017 · 8 comments
Closed

Namespace packages have inconsistent __file__ and __spec__.origin #76486

warsaw opened this issue Dec 13, 2017 · 8 comments
Assignees

Comments

@warsaw
Copy link
Member

@warsaw warsaw commented Dec 13, 2017

BPO 32305
Nosy @warsaw, @ericvsmith, @nedbat, @ned-deily, @ericsnowcurrently, @maggyero
PRs
  • #5481
  • #5994
  • 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

    GitHub fields:

    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'

    bugs.python.org fields:

    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']

    @warsaw
    Copy link
    Member Author

    @warsaw warsaw commented Dec 13, 2017

    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?

    @warsaw warsaw added the 3.7 label Dec 13, 2017
    @warsaw warsaw added the 3.8 label Feb 1, 2018
    @warsaw warsaw self-assigned this Feb 1, 2018
    @warsaw
    Copy link
    Member Author

    @warsaw warsaw commented Feb 1, 2018

    3.5 is in security fix only mode, and this is not a security issue.

    @warsaw warsaw closed this as completed Feb 3, 2018
    @ned-deily
    Copy link
    Member

    @ned-deily ned-deily commented Feb 26, 2018

    Note that this change was originally also backported to 3.6 in PR 5504 but, due to third-party package regressions discovered in pre-release testing, the 3.6 change was reverted in PR 5591 prior to release of 3.6.5rc1.

    @nedbat
    Copy link
    Member

    @nedbat nedbat commented Mar 5, 2018

    Should this get an entry in the What's New?

    @warsaw
    Copy link
    Member Author

    @warsaw warsaw commented Mar 5, 2018

    I guess it depends on whether you think this is a new feature or a bug fix. Or, OTOH, since we had to revert for 3.6, maybe it makes sense either way since some code will be affected.

    @nedbat
    Copy link
    Member

    @nedbat nedbat commented Mar 5, 2018

    As is usual for me, I am here because some coverage.py code broke due to this change. A diff between b1 and b2 found me the code change (thanks for the comment, btw!), but a What's New doesn't seem out of place.

    @warsaw
    Copy link
    Member Author

    @warsaw warsaw commented Mar 5, 2018

    On Mar 5, 2018, at 10:33, Ned Batchelder <report@bugs.python.org> wrote:

    As is usual for me, I am here because some coverage.py code broke due to this change. A diff between b1 and b2 found me the code change (thanks for the comment, btw!), but a What's New doesn't seem out of place.

    Sounds good; I’ll work up a PR

    @maggyero
    Copy link
    Mannequin

    @maggyero maggyero mannequin commented Nov 23, 2018

    @barry You gave 2 reasons for changing __spec__.origin and __file__ for namespace packages.

    Your 1st reason:

    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"?)

    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):

    Previously, module reprs were hard coded based on assumptions about a module's __file__ attribute. If this attribute existed and was a string, it was assumed to be a file system path, and the module object's repr would include this in its value. The only exception was that PEP-302 reserved missing __file__ attributes to built-in modules, and in CPython, this assumption was baked into the module object's implementation. Because of this restriction, some modules contained contrived __file__ values that did not reflect file system paths, and which could cause unexpected problems later (e.g. os.path.join() on a non-path __file__ would return gibberish).
    This PEP relaxes this constraint, and leaves the setting of __file__ to the purview of the loader producing the module. Loaders may opt to leave __file__ unset if no file system path is appropriate. Loaders may also set additional reserved attributes on the module if useful. This means that the definitive way to determine the origin of a module is to check its __loader__ attribute.
    For example, namespace packages as described in this PEP will have no __file__ attribute because no corresponding file exists.

    Your 2nd reason:

    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 agree here, so why not updating the documentation instead of changing the implementation which followed PEP-420?

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants