8000 Implement most common sys.version_info and sys.platform checks by gvanrossum · Pull Request #1942 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Implement most common sys.version_info and sys.platform checks #1942

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

Merged
merged 9 commits into from
Jul 27, 2016
Prev Previous commit
Next Next commit
Add tests for negated sys.version checks, and fix the code.
  • Loading branch information
Guido van Rossum committed Jul 27, 2016
commit cea303b0925e9bf1e885e7462f5d2e22fd4c0134
19 changes: 9 additions & 10 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -2781,21 +2781,20 @@ def infer_if_condition_value(expr: Node, pyversion: Tuple[int, int]) -> int:
if alias.op == 'not':
expr = alias.expr
negated = True
result = TRUTH_VALUE_UNKNOWN
if isinstance(expr, NameExpr):
name = expr.name
elif isinstance(expr, MemberExpr):
name = expr.name
else:
r = consider_sys_version_info(expr, pyversion)
if r != TRUTH_VALUE_UNKNOWN:
return r
result = TRUTH_VALUE_UNKNOWN
if name == 'PY2':
result = ALWAYS_TRUE if pyversion[0] == 2 else ALWAYS_FALSE
elif name == 'PY3':
result = ALWAYS_TRUE if pyversion[0] == 3 else ALWAYS_FALSE
elif name == 'MYPY':
result = ALWAYS_TRUE
result = consider_sys_version_info(expr, pyversion)
if result == TRUTH_VALUE_UNKNOWN:
if name == 'PY2':
result = ALWAYS_TRUE if pyversion[0] == 2 else ALWAYS_FALSE
elif name == 'PY3':
result = ALWAYS_TRUE if pyversion[0] == 3 else ALWAYS_FALSE
elif name == 'MYPY':
result = ALWAYS_TRUE
if negated:
if result == ALWAYS_TRUE:
result = ALWAYS_FALSE
Expand Down
20 changes: 20 additions & 0 deletions test-data/unit/check-unreachable-code.test
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,23 @@ else:
reveal_type(foo()) # E: Revealed type is 'builtins.int'
[builtins fixtures/ops.py]
[out]

[case testSysVersionInfoNegated_python2]
import sys
if not (sys.version_info[0] < 3):
def foo() -> int: return 0
else:
def foo() -> str: return ''
reveal_type(foo()) # E: Revealed type is 'builtins.str'
[builtins_py2 fixtures/ops.py]
[out]

[case testSysVersionInfoNegated]
import sys
if not (sys.version_info[0] < 3):
def foo() -> int: return 0
else:
def foo() -> str: return ''
reveal_type(foo()) # E: Revealed type is 'builtins.int'
[builtins fixtures/ops.py]
[out]
0