Skip to content

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

Merged
brettcannon merged 1 commit intopython:mainfrom
ppwwyyxx:patch-2
May 3, 2023
Merged

pydoc.safeimport: Use importlib.import_module instead of __import__#103118
brettcannon merged 1 commit intopython:mainfrom
ppwwyyxx:patch-2

Conversation

@ppwwyyxx
Copy link
Copy Markdown
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 Standard Library Python modules in the Lib/ directory labels Mar 31, 2023
@arhadthedev arhadthedev removed a link to an issue Apr 7, 2023
@ppwwyyxx
Copy link
Copy Markdown
Contributor Author

Can I get a reviewer assigned?

@arhadthedev
Copy link
Copy Markdown
Member

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

@iritkatriel
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Contributor Author

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

@brettcannon
Copy link
Copy Markdown
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 Standard Library Python modules in the Lib/ directory

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants