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

Document exceptions raised by importlib.import #85076

Open
j13r mannequin opened this issue Jun 7, 2020 · 10 comments · May be fixed by #94662
Open

Document exceptions raised by importlib.import #85076

j13r mannequin opened this issue Jun 7, 2020 · 10 comments · May be fixed by #94662
Labels
3.9 only security fixes 3.10 only security fixes 3.11 only security fixes docs Documentation in the Doc dir easy topic-importlib type-feature A feature request or enhancement

Comments

@j13r
Copy link
Mannequin

j13r mannequin commented Jun 7, 2020

BPO 40899
Nosy @ronaldoussoren, @FFY00, @iritkatriel, @meowmeowmeowcat
PRs
  • bpo-40899: Document exception raised when module cannot be imported #27709
  • 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 = None
    created_at = <Date 2020-06-07.11:08:14.764>
    labels = ['easy', '3.9', '3.10', '3.11', 'type-feature', 'docs']
    title = 'Document exceptions raised by importlib.import'
    updated_at = <Date 2021-10-23.17:05:11.853>
    user = 'https://bugs.python.org/j13r'

    bugs.python.org fields:

    activity = <Date 2021-10-23.17:05:11.853>
    actor = 'FFY00'
    assignee = 'docs@python'
    closed = False
    closed_date = None
    closer = None
    components = ['Documentation']
    creation = <Date 2020-06-07.11:08:14.764>
    creator = 'j13r'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 40899
    keywords = ['patch', 'easy']
    message_count = 9.0
    messages = ['370887', '399205', '399319', '399320', '399328', '399334', '399369', '399370', '399592']
    nosy_count = 6.0
    nosy_names = ['ronaldoussoren', 'docs@python', 'j13r', 'FFY00', 'iritkatriel', 'meowmeowmeowcat']
    pr_nums = ['27709']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue40899'
    versions = ['Python 3.9', 'Python 3.10', 'Python 3.11']

    Linked PRs

    @j13r
    Copy link
    Mannequin Author

    j13r mannequin commented Jun 7, 2020

    https://docs.python.org/3/library/functions.html#__import__
    and
    https://docs.python.org/3/library/importlib.html#importlib.import_module
    do not list which Exceptions are raised in case the module cannot be imported.

    The two exceptions are listed here
    https://docs.python.org/3/library/exceptions.html#ImportError
    ModuleNotFoundError

    Could you add a half-sentence "and raises an ModuleNotFoundError if import was unsuccessful." to the function docs?

    @j13r j13r mannequin added 3.7 (EOL) end of life 3.8 only security fixes 3.10 only security fixes 3.9 only security fixes labels Jun 7, 2020
    @j13r j13r mannequin assigned docspython Jun 7, 2020
    @j13r j13r mannequin added docs Documentation in the Doc dir 3.7 (EOL) end of life 3.8 only security fixes 3.10 only security fixes 3.9 only security fixes labels Jun 7, 2020
    @j13r j13r mannequin assigned docspython Jun 7, 2020
    @j13r j13r mannequin added the docs Documentation in the Doc dir label Jun 7, 2020
    @serhiy-storchaka serhiy-storchaka changed the title Ddcument exceptions raised by importlib.import Document exceptions raised by importlib.import Jun 7, 2020
    @serhiy-storchaka serhiy-storchaka changed the title Ddcument exceptions raised by importlib.import Document exceptions raised by importlib.import Jun 7, 2020
    @iritkatriel
    Copy link
    Member

    ModuleNotFoundError is a subclass of ImportError, so it's probably enough to just document the functions as raising ImportError on failure, as in the case of https://docs.python.org/3/library/importlib.html#importlib.abc.Loader.load_module.

    @iritkatriel iritkatriel added easy 3.11 only security fixes and removed 3.7 (EOL) end of life 3.8 only security fixes labels Aug 7, 2021
    @meowmeowmeowcat
    Copy link
    Mannequin

    meowmeowmeowcat mannequin commented Aug 10, 2021

    Thanks! I've opened a PR for this issue.

    @ronaldoussoren
    Copy link
    Contributor

    Note that importlib.import_module, and the import statement itself, can raise an arbitrary exception when that exception is raised while executing the module body.

    This is easily observed by creating a module that just raises an error in its body:

    # t.py
    1/0
    # EOF

    importlib.import_module("t") will raise ZeroDivisionError.

    @meowmeowmeowcat
    Copy link
    Mannequin

    meowmeowmeowcat mannequin commented Aug 10, 2021

    Oh, I didn't realize that. Then maybe we don't need to document ImportError in https://docs.python.org/3/library/importlib.html#importlib.import_module?

    @ronaldoussoren
    Copy link
    Contributor

    Good question. Hopefully one of the documentation or importlib experts can chime in about that. I may also have uncovered a new issue in importlib ;-)

    Personally I'd say that documenting raising ImportError is still useful because that's the common exception, and an exception that's not mentioned in the rest of the documentation for importlib. That does require a careful formulation to avoid implying that this the only exception that can be raised.

    @meowmeowmeowcat
    Copy link
    Mannequin

    meowmeowmeowcat mannequin commented Aug 11, 2021

    Maybe we can add something like:

    If the module cannot be imported,:exc:`ImportError` is usually raised.
    
    .. note::
       
       Sometimes other errors are raised if the module cannot be imported successfully. See https://docs.python.org/3/library/exceptions.html for more information.
    
    

    @meowmeowmeowcat
    Copy link
    Mannequin

    meowmeowmeowcat mannequin commented Aug 11, 2021

    importlib.import_module("t") will raise ZeroDivisionError.

    I've tested __import__ with the same code, and it will raise ZeroDivisionError too.

    @meowmeowmeowcat meowmeowmeowcat mannequin added type-feature A feature request or enhancement labels Aug 14, 2021
    @meowmeowmeowcat
    Copy link
    Mannequin

    meowmeowmeowcat mannequin commented Aug 14, 2021

    So.. maybe changing the module cannot be imported to the module is not found?

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    kj7rrv added a commit to kj7rrv/cpython that referenced this issue Jul 7, 2022
    @kj7rrv
    Copy link
    Contributor

    kj7rrv commented Jul 7, 2022

    Added in gh-94662

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.9 only security fixes 3.10 only security fixes 3.11 only security fixes docs Documentation in the Doc dir easy topic-importlib type-feature A feature request or enhancement
    Projects
    None yet
    Development

    Successfully merging a pull request may close this issue.

    4 participants