Skip to content

pydoc.safeimport: Use importlib.import_module instead of __import__ #103118

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

Merged
merged 1 commit into from
May 3, 2023

Conversation

ppwwyyxx
Copy link
Contributor

@ppwwyyxx ppwwyyxx commented Mar 29, 2023

Some libraries behave a bit differently:

$ import torch.optim.sgd   # success
$ torch.optim.sgd
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [2], in <cell line: 1>()
----> 1 torch.optim.sgd

AttributeError: module 'torch.optim' has no attribute 'sgd'

For such modules, safeimport fails to import the module because it tries to access the attribute "sgd". On the other hand, importlib.import_module can do it correctly.
Using import_module also simplifies code.

@arhadthedev arhadthedev added skip issue skip news stdlib Python modules in the Lib dir labels Mar 31, 2023
@Security2965 Security2965 linked an issue Apr 7, 2023 that may be closed by this pull request
@arhadthedev arhadthedev removed a link to an issue Apr 7, 2023
@ppwwyyxx
Copy link
Contributor Author

Can I get a reviewer assigned?

@arhadthedev
Copy link
Member

@iritkatriel (as a more-than-once committer into Lib/pydoc.py last year still active)

@iritkatriel
Copy link
Member

Can I get a reviewer assigned?

That’s not how it works.

@arhadthedev - having committed to a script is not a good indication of ability to review any change in it.

I’m less concerned about being spammed with inappropriate review requests, than about creating false expectations among contributors who do not understand how this goes.

In this case it would be better to first reset the OP’s expectations about our process. If you are looking for a reviewer, I would start by looking for a pydoc maintainer, and if I can’t find one then an import expert.

@ppwwyyxx
Copy link
Contributor Author

Thanks for the explanation!
From https://devguide.python.org/core-developers/experts/ it seems there is no maintainer for pydoc - not sure if this is the correct place to find such info. But I can find importlib experts from this link.

@ppwwyyxx
Copy link
Contributor Author

@brettcannon You're listed as an import expert. Could you review this?

@brettcannon
Copy link
Member

@ppwwyyxx thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
skip issue skip news stdlib Python modules in the Lib dir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants