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 more slicing tests. Fixed a bug one of these found.
  • Loading branch information
Guido van Rossum committed Jul 27, 2016
commit 4e8487466b545964f6a5a0188de0dedecbbcfbac
28 changes: 13 additions & 15 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -2832,16 +2832,16 @@ def consider_sys_version_info(expr: Node, pyversion: Tuple[int, ...]) -> int:
else:
return TRUTH_VALUE_UNKNOWN
elif isinstance(index, tuple) and isinstance(thing, tuple):
lo, hi = cast(Tuple[Optional[int], Optional[int]], index)
# Why doesn't mypy see that index can't be None here?
lo, hi = cast(tuple, index)
if lo is None:
lo = 0
if hi is None:
hi = 2
if 0 <= lo <= 2:
if 0 <= hi <= 2:
val = pyversion[lo:hi]
if len(val) == len(thing) or op not in ('==', '!='):
return fixed_comparison(val, op, thing)
if 0 <= lo < hi <= 2:
val = pyversion[lo:hi]
if len(val) == len(thing) or op not in ('==', '!='):
return fixed_comparison(val, op, thing)
return TRUTH_VALUE_UNKNOWN


Expand All @@ -2864,19 +2864,17 @@ def fixed_comparison(left: Targ, op: str, right: Targ) -> int:
return TRUTH_VALUE_UNKNOWN


def contains_int_or_tuple_of_ints(expr: Node) -> Union[None, int, Tuple[int], Tuple[int, int]]:
def contains_int_or_tuple_of_ints(expr: Node) -> Union[None, int, Tuple[int], Tuple[int, ...]]:
if isinstance(expr, IntExpr):
return expr.value
if isinstance(expr, TupleExpr):
if expr.literal == LITERAL_YES:
if len(expr.items) == 1:
e = expr.items[0]
if isinstance(e, IntExpr):
return (e.value,)
elif len(expr.items) == 2:
e0, e1 = expr.items[0], expr.items[1]
if isinstance(e0, IntExpr) and isinstance(e1, IntExpr):
return (e0.value, e1.value)
thing = []
for x in expr.items:
if not isinstance(x, IntExpr):
return None
thing.append(x.value)
return tuple(thing)
return None


Expand Down
28 changes: 28 additions & 0 deletions test-data/unit/check-unreachable-code.test
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,31 @@ else:
foo() + 0
[builtins fixtures/ops.py]
[out]

[case testSysVersionInfoSliced7]
import sys
if sys.version_info >= (3, 5):
def foo() -> int: return 0
else:
def foo() -> str: return ''
foo() + 0
[builtins fixtures/ops.py]
[out]

[case testSysVersionInfoSliced8]
import sys
if sys.version_info >= (3, 5, 0):
def foo() -> int: return 0
else:
def foo() -> str: return ''
[builtins fixtures/ops.py]
[out]

[case testSysVersionInfoSliced9]
import sys
if sys.version_info[1:] >= (5, 0):
def foo() -> int: return 0
else:
def foo() -> str: return ''
[builtins fixtures/ops.py]
[out]
0