KEMBAR78
"if PY{2,3} and X" doesn't ignore the X when run in other python version · Issue #3156 · python/mypy · GitHub
Skip to content

"if PY{2,3} and X" doesn't ignore the X when run in other python version #3156

@euresti

Description

@euresti

Simple example:

from six import PY2

if PY2 and any({'foo': 'bar'}.iterkeys()):  # error: Dict[str, str] has no attribute "iterkeys" when checking py3
    pass

if PY2:
    if any({'foo': 'bar'}.iterkeys()):  # No error
        pass

Note: The same thing applies to PY3 when run with --py2

Sometimes it's cumbersome to split it when you have a series of if, elif, else cases, like this:

if isinstance(node, ast.Name):
    ...
elif isinstance(node, ast.Num):
    ...
elif PY3 and isinstance(node, ast.Starred):   
    ...
else:
    // unhandled node type!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions