-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
mypy bug with try/except conditional imports #1153
Comments
Pretty sure this is a dup, but I can't find the issue. |
Related to #649. |
Note that #649 was closed; a buch of "leftover" bugs were opened in its On Mon, Jan 25, 2016 at 3:19 PM, Ryan Gonzalez notifications@github.com
--Guido van Rossum (python.org/~guido) |
Closed by mistake. |
Another example from #2251 (reported by @RitwikGupta): try:
# Python 3
from urllib.request import urlopen
except ImportError:
# Python 2
from urllib2 import urlopen |
And another from #2253:
|
The current workaround is to add a try:
import cPickle as pickle
except ImportError:
import pickle # type: ignore # <<-- add this |
FYI, this doesn't appear to work with sub-modules: # This will error
try:
import foo.bar
except ImportError:
foo = None # type:ignore This seems to work though: # This will pass
try:
import doesnt.exist # type:ignore
except ImportError:
doesnt = None
# So will this
try:
import collections.abc # type:ignore
except ImportError:
collections = None
assert doesnt is None
assert collections is not None |
This is particularly difficult when the import that's happening is a For example, this works: $ mypy --version
mypy 0.530
$ cat no_try.py
from typing import Dict, Any
JSON = Dict[str, Any]
def accept(obj: JSON) -> Any:
return obj.pop('hello')
$ mypy --ignore-missing-imports no_try.py
This program should typecheck. |
@quodlibetor I don't think your particular case is a bug. mypy has special treatment of from typing import TYPE_CHECKING
if TYPE_CHECKING:
from typing import Dict, Any
else:
try:
from typing import Dict, Any
except ImportError:
from backports.typing import Dict, Any If your version of MYPY = False
if MYPY:
# special imports from typing
else:
# actual runtime implementation mypy will understand this. |
Hmm, okay, I've verified that that works.
I think that I would still be inclined to describe the current behavior as a bug that has a workaround. Would you agree that it is at least as a UX problem? The |
Why do you need |
I did not know about the |
We should contact whoever owns backports and ask to remove the typing
backport.
…On Oct 12, 2017 3:01 PM, "Brandon W Maister" ***@***.***> wrote:
I did not know about the typing package on pypi. The last time I looked
the only option was backports.typing. That that really ought to fix all
my problems, or at least cause interesting *new* problems ;-) Thank you!
—
You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub
<#1153 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/ACwrMu0VaLbDtD_pmgzGlR_Mq7tES4taks5sroxOgaJpZM4HMB58>
.
|
The unit tests require mock but don't specify it, which causes problems for any usages outside tox (e.g. testing that changes to twisted don't break klein). Use PEP508 to specify environment markers and only install mock when required. Tests can then conditionally import unittest.mock and fall back to mock. The conditional imports do raise a mypy error, discussed here: python/mypy#1153 So an annotation is required to silence this specific warning.
Building on an answer from @cjerdonek, would this be an acceptable workaround instead of try:
from py3_pkg import module as _module
except:
from py2_pkg import module
else:
module = _module |
So just igonre the typing on zoneinfo. github com/python/mypy/issues/1153
Is this going to be fixed anytime soon? |
No, it's not, and it's not clear that it should. A majority of cases I've seen here are to work around Python stdlib changes, especially Python 2 to Python 3 stuff.
For the remaining cases, it's often not clear that it's sound, especially given nominal typing. Even if the API is structurally the same, nominal The only thing we should do in this space is #5018 where you can explicitly define exactly the structure you want with a Protocol, and mypy will confirm. For all other cases, if you want to lie to the typechecker, please, go ahead and lie to the type checker (or type ignore):
|
…'s not worth the effort to define a common protocol)
The following error will be reported without if: error: Name "tomllib" already defined (by an import) While this can be silenced by a "# type: ignore", in some case[2] mypy will report the following error: error: Unused "type: ignore" comment [1]: python/mypy#1153 [2]: https://github.com/lilydjwg/nvchecker/actions/runs/4916840821/jobs/8793454970
FYI "type ignore" doesn't work well because in some cases it will cause |
resulst in the error
error: Name 'simplejson' already defined
The text was updated successfully, but these errors were encountered: