Skip to content

gh-85076: Document exceptions that can be raised by importlib.import_module #94662

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

kj7rrv
Copy link
Contributor

@kj7rrv kj7rrv commented Jul 7, 2022

Copy link
Member

@FFY00 FFY00 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @kj7rrv this looks good, thank you! Can you add the missing news entry, as the bot requested?

@FFY00 FFY00 requested review from jaraco and ericsnowcurrently July 7, 2022 17:04
@kj7rrv
Copy link
Contributor Author

kj7rrv commented Jul 7, 2022

@FFY00 I didn't because it's just a documentation change.

Comment on lines 127 to 130
If the module cannot be imported, :func:`import_module` will raise an
:exc:`ImportError`. If the module is found and loaded, but the code in the
module raises an exception, :func:`import_module` will pass that exception
to the module that called it.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
If the module cannot be imported, :func:`import_module` will raise an
:exc:`ImportError`. If the module is found and loaded, but the code in the
module raises an exception, :func:`import_module` will pass that exception
to the module that called it.
If the module cannot be found or loaded, :func:`import_module` will raise an
:exc:`ImportError`.

"cannot be imported" is very generic and could conceivably apply to explicitly raised exceptions, like in the second half of this paragraph, so makes sense to be more specific.

I think we could just omit the second half of the paragraph. Talking about "passing exceptions" makes it sound more complicated than it is. The behaviour here is intuitive and already documented by the sentence "This means all semantics of the function are derived from :func:importlib.__import__."

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't "cannot be found or loaded" imply that that's the behavior if the module cannot be loaded because code in the module raised an exception? Maybe "If the module cannot be found or its files cannot be accessed..." instead? That makes it clear that ImportError is raised in cases like the module not being found or being inaccessible due to file permissions, not cases where it is found but not successfully loaded.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest to look further down, where I tried to consolidate things. (sorry for the spam, I hope it's helpful for readers 😅)

Comment on lines 127 to 130
If the module cannot be imported, :func:`import_module` will raise an
:exc:`ImportError`. If the module is found and loaded, but the code in the
module raises an exception, :func:`import_module` will pass that exception
to the module that called it.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trying to incorporate everything discussed at the same time.

@brettcannon's suggestion:

  • is more specific on the type of exception that can be expected.
  • does not incorporate the remark from @hauntsaninja, that criticises the fact that "cannot be imported" is very generic.

In the first thread @kj7rrv suggested a nice way of explaining how a found module can't be imported. Not sure if it's now too specific, but let's continue the discussion:

Suggested change
If the module cannot be imported, :func:`import_module` will raise an
:exc:`ImportError`. If the module is found and loaded, but the code in the
module raises an exception, :func:`import_module` will pass that exception
to the module that called it.
If the module cannot be found or its files cannot be accessed,
:func:`import_module` will raise :exc:`ImportError` or an appropriate
subclass like :exc:`ModuleNotFoundError`.

@zormit
Copy link
Contributor

zormit commented Jul 23, 2023

Hi @kj7rrv, are you still interested in getting this merged? I found this by looking at <easy> issues and I'd be happy to help make the changes go to upstream. I have tried adding to the discussion -- if you personally like it, maybe you can update the branch and PR accordingly, s.t. it gets another round of review?

P.S:. If you're busy / have other priorities, I can also open another PR and we can close this one. But given there is already a 2nd open PR, I didn't want to add a 3rd one 😅

@kj7rrv
Copy link
Contributor Author

kj7rrv commented Jul 23, 2023

@zormit Hi! I applied your suggested change. I am still interested in having it merged; is there anything else that needs to be changed?

@kj7rrv kj7rrv requested review from ncoghlan and warsaw as code owners July 23, 2023 17:09
@zormit
Copy link
Contributor

zormit commented Jul 24, 2023

I'm not sure why it bedevere still says this is awaiting changes, it should now, after you changed something be awaiting review? 🤔 (that's me, trying to understand the PR state machine)

@kj7rrv Also a clarifying question: Did you mean to apply my changes? I think you have accepted the changes by Brett instead of mine. They are a bit less detailed. It would be okay for me, I could approve it, given it's much better than nothing, but I would be curious about your reasoning behind it.

@brettcannon
Copy link
Member

I'm not sure why it bedevere still says this is awaiting changes

It's because @kj7rrv didn't say the triggering phrase as specified in #94662 (comment) to refresh review requests.

@bombs-kim
Copy link
Contributor

@kj7rrv could you add a comment that includes this line?
"I have made the requested changes; please review again"

@kj7rrv
Copy link
Contributor Author

kj7rrv commented Nov 7, 2024 via email

@bedevere-app
Copy link

bedevere-app bot commented Nov 7, 2024

Thanks for making the requested changes!

@brettcannon, @FFY00: please review the changes made to this pull request.

@bedevere-app bedevere-app bot requested review from brettcannon and FFY00 November 7, 2024 06:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Todo
Development

Successfully merging this pull request may close these issues.

Document exceptions raised by importlib.import
6 participants