Skip to content
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

Conditional import of izip is problematic #1026

Closed
gvanrossum opened this issue Nov 30, 2015 · 2 comments
Closed

Conditional import of izip is problematic #1026

gvanrossum opened this issue Nov 30, 2015 · 2 comments
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal

Comments

@gvanrossum
Copy link
Member

Test code:

try:
    from itertools import izip
except ImportError:
    izip = zip

In Python 3 mode, this complains that itertools doesn't define izip. This is correct but maybe inside try/except ImportError this should not be an error.

In Python 2 mode, this gives the dreaded "Incompatible types" errror:

/Users/guido/mypy_tests/mypy_izip.py:4: error: Incompatible types in assignment (expression has type overloaded function, variable has type overloaded function)

I guess this is another case of #649 that isn't handled yet.

@JukkaL JukkaL added the bug mypy got something wrong label Dec 1, 2015
@JukkaL
Copy link
Collaborator

JukkaL commented Dec 1, 2015

Yeah, we could consider this is special case of #649, but we can keep ImportError related problems where mypy should ignore a block separate in this issue. #649 can cover cases where all conditional definitions should take effect.

Another related case that is pretty common in Python 2 std library code is shown below. In this case both imports work, but mypy can't deal with a multiply defined class. Maybe we should just ignore one of the class definitions here for now, and maybe give an optional warning in the future:

try:
    from cStringIO import StringIO
except ImportError:
    from StringIO import StringIO

@AlexWaygood
Copy link
Member

AlexWaygood commented Mar 31, 2022

Closing as a duplicate of #1153, which (although it's a newer issue) has more detailed discussion and is less Python 2-specific (refs #12237)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal
Projects
None yet
Development

No branches or pull requests

3 participants