Skip to content

Support conditional Python version and platform checks #698

Closed
@JukkaL

Description

@JukkaL

Mypy should support some common kinds of Python version checks and platform checks (e.g. Windows vs. Posix). We should ignore code paths that won't be run on the targeted Python version or platform. This way mypy can more effectively type check code that supports Python 2 and 3 and multiplatform code.

We first need to decide which checks to support. These examples are from PEP 484 and should be supported:

import sys

if sys.version_info[0] >= 3:
    # Python 3 specific definitions
    ...
else:
    # Python 2 specific definitions
    ...

if sys.platform == 'win32':
    # Windows specific definitions
    ...
else:
    # Posix specific definitions
    ...

When type checking code as above, always only if or the else block would be analyzed, never both, since on any given program run only one them can be evaluated (we assume that nobody does anything crazy like modifying sys.platform at runtime). We'd detect the check expressions during semantic analysis and wouldn't semantically analyze (or type check) the skipped blocks, similar to how mypy currently deals with PY2/PY3 conditions in if statements.

We already have Python 2 and Python 3 modes, and we should also implement Windows and non-Windows (Posix) modes. Initially, we can just use the platform on which the type checker is being run, but more generally this should be configurable (e.g., mypy --platform win32 ...).

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions