From 8eb7b7d945285a405735385b1ee7748f450482e2 Mon Sep 17 00:00:00 2001 From: Joy Bhalla Date: Sat, 29 Feb 2020 16:27:18 +0530 Subject: [PATCH 1/5] added supprt for platform.system() --- mypy/options.py | 4 +++- mypy/reachability.py | 10 +++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/mypy/options.py b/mypy/options.py index 38072b821d15..cfe64ddc4ff9 100644 --- a/mypy/options.py +++ b/mypy/options.py @@ -2,6 +2,7 @@ import re import pprint import sys +import platform from typing_extensions import Final from typing import Dict, List, Mapping, Optional, Pattern, Set, Tuple, Callable, Any @@ -70,7 +71,8 @@ def __init__(self) -> None: # The executable used to search for PEP 561 packages. If this is None, # then mypy does not search for PEP 561 packages. self.python_executable = sys.executable # type: Optional[str] - self.platform = sys.platform + self.sys_platform = sys.platform + self.platform = platform.system() self.custom_typing_module = None # type: Optional[str] self.custom_typeshed_dir = None # type: Optional[str] self.mypy_path = [] # type: List[str] diff --git a/mypy/reachability.py b/mypy/reachability.py index 5ee813dc982c..e426396f80cf 100644 --- a/mypy/reachability.py +++ b/mypy/reachability.py @@ -1,5 +1,5 @@ """Utilities related to determining the reachability of code (in semantic analysis).""" - +import platform from typing import Tuple, TypeVar, Union, Optional from typing_extensions import Final @@ -92,7 +92,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.sys_platform) if result == TRUTH_VALUE_UNKNOWN: if name == 'PY2': result = ALWAYS_TRUE if pyversion[0] == 2 else ALWAYS_FALSE @@ -150,7 +150,7 @@ def consider_sys_version_info(expr: Expression, pyversion: Tuple[int, ...]) -> i return TRUTH_VALUE_UNKNOWN -def consider_sys_platform(expr: Expression, platform: str) -> int: +def consider_sys_platform(expr: Expression, sys_platform: str, platform: str) -> int: """Consider whether expr is a comparison involving sys.platform. Return ALWAYS_TRUE, ALWAYS_FALSE, or TRUTH_VALUE_UNKNOWN. @@ -166,7 +166,7 @@ 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'): + if not is_sys_attr(expr.operands[0], 'sys_platform', 'platform'): return TRUTH_VALUE_UNKNOWN right = expr.operands[1] if not isinstance(right, (StrExpr, UnicodeExpr)): @@ -177,7 +177,7 @@ def consider_sys_platform(expr: Expression, platform: str) -> int: return TRUTH_VALUE_UNKNOWN if len(expr.args) != 1 or not isinstance(expr.args[0], (StrExpr, UnicodeExpr)): return TRUTH_VALUE_UNKNOWN - if not is_sys_attr(expr.callee.expr, 'platform'): + if not is_sys_attr(expr.callee.expr, 'sys_platform', 'platform'): return TRUTH_VALUE_UNKNOWN if expr.callee.name != 'startswith': return TRUTH_VALUE_UNKNOWN From dfad35ae5085518fe3ca8418dcf65134efe307db Mon Sep 17 00:00:00 2001 From: Joy Bhalla Date: Wed, 4 Mar 2020 07:50:22 +0530 Subject: [PATCH 2/5] added support for compar expr for platform.system() --- mypy/options.py | 4 ++-- mypy/reachability.py | 27 +++++++++++++++++++++------ 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/mypy/options.py b/mypy/options.py index cfe64ddc4ff9..479979bdaa60 100644 --- a/mypy/options.py +++ b/mypy/options.py @@ -71,8 +71,8 @@ def __init__(self) -> None: # The executable used to search for PEP 561 packages. If this is None, # then mypy does not search for PEP 561 packages. self.python_executable = sys.executable # type: Optional[str] - self.sys_platform = sys.platform - self.platform = platform.system() + self.platform = sys.platform + self.platform_system = platform.system() self.custom_typing_module = None # type: Optional[str] self.custom_typeshed_dir = None # type: Optional[str] self.mypy_path = [] # type: List[str] diff --git a/mypy/reachability.py b/mypy/reachability.py index e426396f80cf..6f812d1f98d6 100644 --- a/mypy/reachability.py +++ b/mypy/reachability.py @@ -150,8 +150,8 @@ def consider_sys_version_info(expr: Expression, pyversion: Tuple[int, ...]) -> i return TRUTH_VALUE_UNKNOWN -def consider_sys_platform(expr: Expression, sys_platform: str, 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 Return ALWAYS_TRUE, ALWAYS_FALSE, or TRUTH_VALUE_UNKNOWN. """ @@ -166,9 +166,14 @@ def consider_sys_platform(expr: Expression, sys_platform: str, platform: str) -> op = expr.operators[0] if op not in ('==', '!='): return TRUTH_VALUE_UNKNOWN - if not is_sys_attr(expr.operands[0], 'sys_platform', 'platform'): - return TRUTH_VALUE_UNKNOWN - right = expr.operands[1] + #check if either platform or platform_system is being used + if platform_system: + if not is_platform_attr(expr.operands[0], 'plaform_system'): + return TRUTH_VALUE_UNKNOWN + elif platform: + if not is_sys_attr(expr.operands[0], 'platform'): + return TRUTH_VALUE_UNKNOWN + right = expr.operands[1] if not isinstance(right, (StrExpr, UnicodeExpr)): return TRUTH_VALUE_UNKNOWN return fixed_comparison(platform, op, right.value) @@ -177,7 +182,7 @@ def consider_sys_platform(expr: Expression, sys_platform: str, platform: str) -> return TRUTH_VALUE_UNKNOWN if len(expr.args) != 1 or not isinstance(expr.args[0], (StrExpr, UnicodeExpr)): return TRUTH_VALUE_UNKNOWN - if not is_sys_attr(expr.callee.expr, 'sys_platform', 'platform'): + if not is_sys_attr(expr.callee.expr, 'platform'): return TRUTH_VALUE_UNKNOWN if expr.callee.name != 'startswith': return TRUTH_VALUE_UNKNOWN @@ -261,6 +266,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 == 'system': + return True + return False + + def mark_block_unreachable(block: Block) -> None: block.is_unreachable = True block.accept(MarkImportsUnreachableVisitor()) From 0794d77dcf6dbdefd32f874b5b4b2d2b20edaad3 Mon Sep 17 00:00:00 2001 From: Joy Bhalla Date: Thu, 5 Mar 2020 20:40:21 +0530 Subject: [PATCH 3/5] added test --- test-data/unit/check-unreachable-code.test | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test-data/unit/check-unreachable-code.test b/test-data/unit/check-unreachable-code.test index 262ac86e49ad..1a00783beec1 100644 --- a/test-data/unit/check-unreachable-code.test +++ b/test-data/unit/check-unreachable-code.test @@ -302,6 +302,8 @@ else: import sys if sys.platform == 'fictional': def foo() -> int: return 0 +if platform.system(): + def foo() -> int: return 0 else: def foo() -> str: return '' foo() + '' From 2df738ececda109f8448381cbdcfdb65d2e6c049 Mon Sep 17 00:00:00 2001 From: Joy Bhalla Date: Tue, 10 Mar 2020 10:30:59 +0530 Subject: [PATCH 4/5] added requested changes --- mypy/reachability.py | 11 +++++------ test-data/unit/check-unreachable-code.test | 2 +- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/mypy/reachability.py b/mypy/reachability.py index 6f812d1f98d6..1132fef87424 100644 --- a/mypy/reachability.py +++ b/mypy/reachability.py @@ -1,5 +1,4 @@ """Utilities related to determining the reachability of code (in semantic analysis).""" -import platform 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.sys_platform) + result = consider_sys_platform(expr, options.sys_platform,options.platform_system) if result == TRUTH_VALUE_UNKNOWN: if name == 'PY2': result = ALWAYS_TRUE if pyversion[0] == 2 else ALWAYS_FALSE @@ -151,7 +150,7 @@ def consider_sys_version_info(expr: Expression, pyversion: Tuple[int, ...]) -> i def consider_sys_platform(expr: Expression, platform: str, platform_system: str) -> int: - """Consider whether expr is a comparison involving sys.platform + """Consider whether expr is a comparison involving sys.platform and platform.system() Return ALWAYS_TRUE, ALWAYS_FALSE, or TRUTH_VALUE_UNKNOWN. """ @@ -166,9 +165,9 @@ def consider_sys_platform(expr: Expression, platform: str, platform_system: str) op = expr.operators[0] if op not in ('==', '!='): return TRUTH_VALUE_UNKNOWN - #check if either platform or platform_system is being used + # check if either platform or platform_system is being used if platform_system: - if not is_platform_attr(expr.operands[0], 'plaform_system'): + 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'): @@ -271,7 +270,7 @@ def is_platform_attr(expr: Expression, name: str) -> bool: # 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 == 'system': + if isinstance(expr.expr, CallExpr) and expr.expr.name == 'platform': return True return False diff --git a/test-data/unit/check-unreachable-code.test b/test-data/unit/check-unreachable-code.test index 1a00783beec1..c18fb12527df 100644 --- a/test-data/unit/check-unreachable-code.test +++ b/test-data/unit/check-unreachable-code.test @@ -302,7 +302,7 @@ else: import sys if sys.platform == 'fictional': def foo() -> int: return 0 -if platform.system(): +if platform.system() == 'fake': def foo() -> int: return 0 else: def foo() -> str: return '' From 92a6aa21ae61f33081550272ae446a9e9f66cd51 Mon Sep 17 00:00:00 2001 From: Joy Bhalla Date: Fri, 13 Mar 2020 07:14:25 +0530 Subject: [PATCH 5/5] fixed called variable to platform from sys_platform --- mypy/reachability.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mypy/reachability.py b/mypy/reachability.py index 1132fef87424..52011c5be498 100644 --- a/mypy/reachability.py +++ b/mypy/reachability.py @@ -91,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.sys_platform,options.platform_system) + 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