Ask questionsIssues with conditional imports
Even though #649 was closed, there still remains several issues related to conditional imports. This issue is a top-level issue for these.
Related, more specific issues:
Answer questions rodrigc
@JukkaL While introducing mypy to the CI for the Twisted codebase, I ran into this problem which is similar to the examples mentioned in this issue, similar to the one brought up by @ethanhs:
What is the way to deal with this:
try: from foo import A except ImportError: A = None
Is there something that needs to be fixed in mypy? For now, I just put:
A = None # type:ignore
to turn off mypy errors on that line. As much as possible, I want to avoid turning off mypy like that.
The other option is to follow the example mentioned by @cjerdonek here: https://github.com/python/mypy/issues/1297#issuecomment-508593494
try: from foo import A as _A except ImportError: A = None else: A = _A