Skip to content
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

Added support for platform.system() #8461

Closed
wants to merge 5 commits into from

Conversation

joybh98
Copy link
Contributor

@joybh98 joybh98 commented Feb 29, 2020

What is this PR about ?
This PR is related to #8166 .
@brettcannon asked to include support for platform.system() with the existing sys.platform

Things I've Changed

  1. Renamed platform to sys_platform to avoid confusion
  2. I've added the platform = platform.system() in options.py
  3. Added param platform in consider_sys_platform function

Tests
I've checked the changes with pep8(pycodestyle)

I think changing the platform to sys_platform may cause some errors if it is being used somewhere else.

@joybh98 joybh98 changed the title Added supprot for platform.system() Added support for platform.system() Feb 29, 2020
Copy link
Collaborator

@ethanhs ethanhs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here are my initial comments, I think you have the right idea, but there are some things that need to be fixed.

@@ -71,6 +72,7 @@ def __init__(self) -> None:
# then mypy does not search for PEP 561 packages.
self.python_executable = sys.executable # type: Optional[str]
self.platform = sys.platform
Copy link
Collaborator

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.

Suggested change
self.platform = sys.platform
self.sys_platform = sys.platform

Copy link
Collaborator

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 use options.sys_platform, but here you are assigning to options.platform those two need to be consistently named.

@@ -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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You seem not to be passing options.platform_system here, but you added it as an argument below.

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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update the docstring to add that this also considers platform.system (see below).

# 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':
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be if isinstance(expr.expr, CallExpr) and expr.expr.name == 'platform'?

elif platform:
if not is_sys_attr(expr.operands[0], 'platform'):
return TRUTH_VALUE_UNKNOWN
right = expr.operands[1]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems over-indented, it gets used below.

@@ -1,5 +1,5 @@
"""Utilities related to determining the reachability of code (in semantic analysis)."""

import platform
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this import is needed?

@@ -302,6 +302,8 @@ else:
import sys
if sys.platform == 'fictional':
def foo() -> int: return 0
if platform.system():
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if platform.system():
if platform.system() == 'fake':

I think this test should test comparison.

@joybh98
Copy link
Contributor Author

joybh98 commented Mar 8, 2020

@ethanhs thanks for reviewing this commit, will work on it ASAP

@joybh98
Copy link
Contributor Author

joybh98 commented Mar 11, 2020

@ethanhs I've added the requested changes, please let me know what else needs to be done, and then I'll start to fix the checks!

@joybh98
Copy link
Contributor Author

joybh98 commented Mar 13, 2020

@ethanhs I've renamed the variable, but I still think there is some logic inconsistencies

@ethanhs
Copy link
Collaborator

ethanhs commented Mar 13, 2020

@joybhallaa I will take a look at this later.

return TRUTH_VALUE_UNKNOWN
right = expr.operands[1]
# check if either platform or platform_system is being used
if platform_system:
Copy link
Collaborator

@ethanhs ethanhs Mar 16, 2020

Choose a reason for hiding this comment

The 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 platform attribute, but you don't want to do that. Both platform and platform_system should be truthy, since they will both be set in options.

@ethanhs
Copy link
Collaborator

ethanhs commented Mar 16, 2020

Also, user configuration/command line input should override this. I'm not certain what exactly to do here, but I think mapping sys.platform values to platform.system() values would be best. (@JukkaL thoughts?)

Looking at the sys.platform and platform.system() docs, the following mapping makes sense:

{
     'win32': 'Windows',
     'linux': 'Linux',
     'darwin': 'Darwin',
}

There is some awkwardness with cygwin https://stackoverflow.com/a/32612402 and I would be surprised if anyone uses mypy on AIX, so I wouldn't worry about those.

Copy link
Collaborator

@JukkaL JukkaL left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think mapping sys.platform values to platform.system() values would be best.

I agree that this seems like the best option. Something like if platform.system() == 'Linux' should always mean exactly the same as if sys.platform == 'linux'. Thus we don't need to (and shouldn't) import platform at runtime.

@@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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 --platform affects a platform.system() check.

@hauntsaninja
Copy link
Collaborator

PR is pretty stale, feel free to re-open if you want to pick this back up and address feedback / broken tests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants