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
Accessing attributes in modules imported by import_module #5059
Are you reporting a bug, or opening a feature request?
I believe I found a bug in mypy (as far as I understand what's going on, this is not a typeshed issue).
$ mypy
my_file.py:8: error: Module has no attribute "LOGGING"
Expected behavior
No mypy error. Especially, when the attribute is explicitly asserted to be there (analogically as with narrowing types using isinstance()).
This issue is not about hasattr checks, though. (It was just a suggestion how it could be dealt with.) It's about modules imported using import_module. Shouldn't mypy approach differently to them? Adding # type: ignore seems the only option I have, which I'd love to avoid.
Importing something based on an environment variable is too dynamic, mypy cannot statically know what module you are reffering to.
importlib.import_module returns type types.ModuleType. We don't know anything else about the attributes of that module, beyond the attributes of that type. Therefore, the error message boils down to #1424.
Perhaps we should warn when someone calls __import__, importlib.import_module or the like, because it almost always requires a type ignore.
Right, I get it, thanks for the explanation. Sometimes it's unavoidable to use import_module (as in my example), warning won't help in my opinion, since the user is probably aware of the situation. I will wait for resolving #1424. Thanks again.
Activity
emmatyping commentedon May 16, 2018
This is a duplicate of #1424, we don't currently support
hasattrchecks.pawelswiecki commentedon May 16, 2018
Thank you for the reply.
This issue is not about
hasattrchecks, though. (It was just a suggestion how it could be dealt with.) It's about modules imported usingimport_module. Shouldn't mypy approach differently to them? Adding# type: ignoreseems the only option I have, which I'd love to avoid.emmatyping commentedon May 16, 2018
Importing something based on an environment variable is too dynamic, mypy cannot statically know what module you are reffering to.
importlib.import_modulereturns typetypes.ModuleType. We don't know anything else about the attributes of that module, beyond the attributes of that type. Therefore, the error message boils down to #1424.Perhaps we should warn when someone calls
__import__,importlib.import_moduleor the like, because it almost always requires a type ignore.pawelswiecki commentedon May 16, 2018
Right, I get it, thanks for the explanation. Sometimes it's unavoidable to use
import_module(as in my example), warning won't help in my opinion, since the user is probably aware of the situation. I will wait for resolving #1424. Thanks again.JukkaL commentedon May 16, 2018
I think that the return type of
importlib.import_moduleshould beAny, similar to how the type ofgetattrisAny.JelleZijlstra commentedon May 16, 2018
Or perhaps types.ModuleType should grow a
def __getattr__(self, attr) -> Any, because modules can have arbitrary attributes.patrick91 commentedon Jul 20, 2020
I'm having problem with this too:
I get this error:
Which can only be fixed by using
# type: ignorekunit: tool: fix typecheck errors about loading qemu configs
mypy: fix tox failures
mypy: fix tox failures
mypy: fix tox failures
Fix CI: mypy
Fix CI: mypy
Fix CI: mypy
Fix CI: mypy
Fix CI: mypy
Fix CI: mypy
Ignore type of shlex.join