-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Added support for platform.system() #8461
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
Changes from all commits
8eb7b7d
dfad35a
0794d77
2df738e
92a6aa2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
"""Utilities related to determining the reachability of code (in semantic analysis).""" | ||
|
||
from typing import Tuple, TypeVar, Union, Optional | ||
from typing_extensions import Final | ||
|
||
|
@@ -92,7 +91,7 @@ def infer_condition_value(expr: Expression, options: Options) -> int: | |
else: | ||
result = consider_sys_version_info(expr, pyversion) | ||
if result == TRUTH_VALUE_UNKNOWN: | ||
result = consider_sys_platform(expr, options.platform) | ||
result = consider_sys_platform(expr, options.platform,options.platform_system) | ||
if result == TRUTH_VALUE_UNKNOWN: | ||
if name == 'PY2': | ||
result = ALWAYS_TRUE if pyversion[0] == 2 else ALWAYS_FALSE | ||
|
@@ -150,8 +149,8 @@ def consider_sys_version_info(expr: Expression, pyversion: Tuple[int, ...]) -> i | |
return TRUTH_VALUE_UNKNOWN | ||
|
||
|
||
def consider_sys_platform(expr: Expression, platform: str) -> int: | ||
"""Consider whether expr is a comparison involving sys.platform. | ||
def consider_sys_platform(expr: Expression, platform: str, platform_system: str) -> int: | ||
"""Consider whether expr is a comparison involving sys.platform and platform.system() | ||
|
||
Return ALWAYS_TRUE, ALWAYS_FALSE, or TRUTH_VALUE_UNKNOWN. | ||
""" | ||
|
@@ -166,9 +165,14 @@ def consider_sys_platform(expr: Expression, platform: str) -> int: | |
op = expr.operators[0] | ||
if op not in ('==', '!='): | ||
return TRUTH_VALUE_UNKNOWN | ||
if not is_sys_attr(expr.operands[0], 'platform'): | ||
return TRUTH_VALUE_UNKNOWN | ||
right = expr.operands[1] | ||
# check if either platform or platform_system is being used | ||
if platform_system: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right now you are saying the truth value is unknown if the expression is not a |
||
if not is_platform_attr(expr.operands[0], 'platform_system'): | ||
return TRUTH_VALUE_UNKNOWN | ||
elif platform: | ||
if not is_sys_attr(expr.operands[0], 'platform'): | ||
return TRUTH_VALUE_UNKNOWN | ||
right = expr.operands[1] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems over-indented, it gets used below. |
||
if not isinstance(right, (StrExpr, UnicodeExpr)): | ||
return TRUTH_VALUE_UNKNOWN | ||
return fixed_comparison(platform, op, right.value) | ||
|
@@ -261,6 +265,16 @@ def is_sys_attr(expr: Expression, name: str) -> bool: | |
return False | ||
|
||
|
||
def is_platform_attr(expr: Expression, name: str) -> bool: | ||
# Platform lib calls methods, it differs from sys lib | ||
# sys.platform is of str class and platform.system is of | ||
# function class | ||
if isinstance(expr, MemberExpr) and expr.name == name: | ||
if isinstance(expr.expr, CallExpr) and expr.expr.name == 'platform': | ||
return True | ||
return False | ||
|
||
|
||
def mark_block_unreachable(block: Block) -> None: | ||
block.is_unreachable = True | ||
block.accept(MarkImportsUnreachableVisitor()) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -302,6 +302,8 @@ else: | |
import sys | ||
if sys.platform == 'fictional': | ||
def foo() -> int: return 0 | ||
if platform.system() == 'fake': | ||
def foo() -> int: return 0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There should also be a test that has a successful platform check. Also test that |
||
else: | ||
def foo() -> str: return '' | ||
foo() + '' | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems you didn't rename this to be consistent with your usage below. You should also change any usage of this elsewhere.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like you missed updating this based on the test failure. In your call to
consider_sys_platform
you useoptions.sys_platform
, but here you are assigning tooptions.platform
those two need to be consistently named.