You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
"Module has no attribute" error when using "__import__()" #7501
This is expected: mypy doesn't know what module is being imported when __import__ is used.
I wonder if it could not just assume "import X" being used for __import__("X")?
(obviously more tricky with dynamic imports like __import__(modname), but that's not the case here)
I wonder if it could not just assume "import X" being used for import("X")?
I wouldn't be so quick. If someone writes xyzzy = __import__("xyzzy") instead of import xyzzy maybe they have something they want to hide from the type checker?
The code given in the packaging docs seems to be trying to avoid creating a local variable xyzzy -- there are other ways to do that though, and I would personally never have written those recommendations in that style. I guess someone was proud of a clever hack they came up with, but the may effect is that readers are more likely to copy-and-paste magical stuff they don't understand.
I'd be sympathetic if you were to make __import__ return Any in typeshed though.
maybe they have something they want to hide from the type checker?
Wouldn't / shouldn't # type: ignore be used then?
I guess someone was proud of a clever hack they came up with
__import__('pdb').set_trace() is similar: just works around linters complaining about multiple statements on one line (as would be the case with import pdb; pdb.set_trace(), and uses only a single line in the first place).
It's not something important for me (only temporary code in this case), of course.
Would something new like typing.ModuleType would work, which takes a name as an argument, and would result in what "import os" uses?
Where/how does mypy handle the "import" statement?
Activity
msullivan commentedon Sep 12, 2019
This is expected: mypy doesn't know what module is being imported when
__import__is used.You could potentially make an argument that the stub for
__import__in typeshed should returnAny, though.tucked commentedon Sep 26, 2019
FWIW, setuptools namespace packages get hit by this if you use the code suggested in https://packaging.python.org/guides/packaging-namespace-packages/#pkg-resources-style-namespace-packages:
blueyed commentedon Sep 26, 2019
@msullivan
I wonder if it could not just assume "import X" being used for
__import__("X")?(obviously more tricky with dynamic imports like
__import__(modname), but that's not the case here)gvanrossum commentedon Sep 26, 2019
I wouldn't be so quick. If someone writes
xyzzy = __import__("xyzzy")instead ofimport xyzzymaybe they have something they want to hide from the type checker?The code given in the packaging docs seems to be trying to avoid creating a local variable
xyzzy-- there are other ways to do that though, and I would personally never have written those recommendations in that style. I guess someone was proud of a clever hack they came up with, but the may effect is that readers are more likely to copy-and-paste magical stuff they don't understand.I'd be sympathetic if you were to make
__import__returnAnyin typeshed though.blueyed commentedon Sep 26, 2019
Wouldn't / shouldn't
# type: ignorebe used then?__import__('pdb').set_trace()is similar: just works around linters complaining about multiple statements on one line (as would be the case withimport pdb; pdb.set_trace(), and uses only a single line in the first place).It's not something important for me (only temporary code in this case), of course.
blueyed commentedon Oct 29, 2019
Can you hint mypy that a var is of a specific ModuleType currently in any way?
Isn't that a valid feature request, so that this would work:
Would something new like
typing.ModuleTypewould work, which takes a name as an argument, and would result in what "import os" uses?Where/how does mypy handle the "import" statement?
JukkaL commentedon Oct 29, 2019
@blueyed That isn't supported at the moment. If you want to propose this feature, you can create an issue at https://github.com/python/typing or start a discussion at the typing-sig list, since this requires some new type annotation syntax.