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

Have importlib.reload() raise ModuleNotFoundError when a spec can't be found #74037

Closed
richardcooper mannequin opened this issue Mar 19, 2017 · 5 comments
Closed

Have importlib.reload() raise ModuleNotFoundError when a spec can't be found #74037

richardcooper mannequin opened this issue Mar 19, 2017 · 5 comments
Labels
3.7 easy stdlib type-feature

Comments

@richardcooper
Copy link
Mannequin

@richardcooper richardcooper mannequin commented Mar 19, 2017

BPO 29851
Nosy @brettcannon, @ncoghlan, @ericsnowcurrently, @serhiy-storchaka
PRs
  • #972
  • Files
  • bug.py: Repo testcase
  • 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 = None
    closed_at = <Date 2017-05-24.22:20:40.688>
    created_at = <Date 2017-03-19.00:09:56.344>
    labels = ['3.7', 'easy', 'type-feature', 'library']
    title = "Have importlib.reload() raise ModuleNotFoundError when a spec can't be found"
    updated_at = <Date 2017-05-24.22:20:40.688>
    user = 'https://bugs.python.org/RichardCooper'

    bugs.python.org fields:

    activity = <Date 2017-05-24.22:20:40.688>
    actor = 'brett.cannon'
    assignee = 'none'
    closed = True
    closed_date = <Date 2017-05-24.22:20:40.688>
    closer = 'brett.cannon'
    components = ['Library (Lib)']
    creation = <Date 2017-03-19.00:09:56.344>
    creator = 'Richard Cooper'
    dependencies = []
    files = ['46737']
    hgrepos = []
    issue_num = 29851
    keywords = ['easy']
    message_count = 5.0
    messages = ['289834', '289903', '291081', '291125', '294405']
    nosy_count = 5.0
    nosy_names = ['brett.cannon', 'ncoghlan', 'eric.snow', 'serhiy.storchaka', 'Richard Cooper']
    pr_nums = ['972']
    priority = 'low'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue29851'
    versions = ['Python 3.7']

    @richardcooper
    Copy link
    Mannequin Author

    @richardcooper richardcooper mannequin commented Mar 19, 2017

    importlib.reload doesn't work; gives an error about NoneType having no name attribute.

    See attached a simple repo testcase

    When run it yields the following [disappointing] result. I'm running Python3.0.6.1 (installed from brew) on OSX 10.12.3

    iMac:python_package_loader cooper$ python3 bug.py 
    module loaded
    Traceback (most recent call last):
      File "bug.py", line 14, in <module>
        importlib.reload(sys.modules[moduleName])
      File "/usr/local/Cellar/python3/3.6.0_1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/importlib/__init__.py", line 166, in reload
        _bootstrap._exec(spec, module)
      File "<frozen importlib._bootstrap>", line 589, in _exec
    AttributeError: 'NoneType' object has no attribute 'name'
    

    @richardcooper richardcooper mannequin added type-crash stdlib labels Mar 19, 2017
    @brettcannon
    Copy link
    Member

    @brettcannon brettcannon commented Mar 20, 2017

    First, I don't know what version you're testing against because 3.0.6.1 isn't an actual release of Python and 3.6.1 isn't released yet (unless you know something I don't know :) ).

    Second, the issue is that you're trying to import a module under a name which doesn't match the file specified. That's causing reload() to not be able to find the original source file to reload against, leading to the None being returned by importlib._bootstrap._find_spec() which is leading to the error you're seeing. (Remember, reload() basically runs like an import statement for the module you're reloading but recycles the module object.)

    Third, while an exception is reasonable in this case, it is misleading and reload() should be updated to raise an ImportError if _bootstrap._find_spec() returns None.

    I'm marking this issue as an easy fix since you just need to add an is None check on a return value and then raise ImportError if necessary in case someone wants to propose a PR to improve the error. It will require a doc update to document the change in the exception raised.

    @brettcannon brettcannon changed the title importlib.reload references None object Have importlib.reload() raise ImportError when a spec can't be found Mar 20, 2017
    @brettcannon brettcannon added type-feature and removed type-crash labels Mar 20, 2017
    @serhiy-storchaka
    Copy link
    Member

    @serhiy-storchaka serhiy-storchaka commented Apr 3, 2017

    Wouldn't ModuleNotFoundError be more appropriate?

    @brettcannon
    Copy link
    Member

    @brettcannon brettcannon commented Apr 4, 2017

    That's a good point, Serhiy, since that's what the exception is signaling. So yes, the exception should be ModuleNotFounderror.

    @brettcannon brettcannon changed the title Have importlib.reload() raise ImportError when a spec can't be found Have importlib.reload() raise ModuleNotFoundError when a spec can't be found Apr 4, 2017
    @brettcannon
    Copy link
    Member

    @brettcannon brettcannon commented May 24, 2017

    New changeset 9498782 by Brett Cannon (Garvit Khatri) in branch 'master':
    bpo-29851: Have importlib.reload() raise ImportError if the module's spec is not found (GH-972)
    9498782

    @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
    Labels
    3.7 easy stdlib type-feature
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants