mypy icon indicating copy to clipboard operation
mypy copied to clipboard

Recognize platform.system() as well as sys.platform

Open brettcannon opened this issue 6 years ago • 12 comments

Note: if you are reporting a wrong signature of a function or a class in the standard library, then the typeshed tracker is better suited for this report: https://github.com/python/typeshed/issues

Please provide more information to help us understand the issue:

  • Are you reporting a bug, or opening a feature request? feature request
  • Please insert below the code you are checking with mypy, or a mock-up repro if the source is private. We would appreciate if you try to simplify your case to a minimal repro.
    if platform.system() != "Windows":  # sys.platform != "win32":
        try:
            # os.confstr("CS_GNU_LIBC_VERSION") returns a string like "glibc 2.17".
            version_string = os.confstr("CS_GNU_LIBC_VERSION")
            assert version_string is not None
            _, version = version_string.split()
        except (AssertionError, OSError, ValueError):
            # os.confstr() or CS_GNU_LIBC_VERSION not available (or a bad value)...
            return None
        return version
    return None
  • What is the actual behavior/output? "error: Module has no attribute "confstr""
  • What is the behavior/output you expect? No error when run under Windows
  • What are the versions of mypy and Python you are using? mypy 0.750, Python 3.7.4

Basically it would be nice to get to use platform.system() instead of having to use sys.platform to make mypy happy with code that is conditional on the platform.

brettcannon avatar Dec 17 '19 23:12 brettcannon

On one hand this is pretty niche, but on other hand should be not hard to implement.

ilevkivskyi avatar Dec 18 '19 14:12 ilevkivskyi

Hi @ilevkivskyi I am interested in working on this issue, please guide me on where I can start.

neesara avatar Dec 20 '19 09:12 neesara

Look at consider_sys_platform() in mypy/reachability.py.

ilevkivskyi avatar Dec 20 '19 13:12 ilevkivskyi

Which option would you recommend? - Write a new function similar to 'consider_sys_platform()' for handling platform.system() or modify 'consider_sys_platform()' to accomodate platform.system()

neesara avatar Jan 01 '20 08:01 neesara

@ilevkivskyi , I was reading the function consider_sys_platform, and from what I could understand, we want to check Comaprision Expression, and call expressions using the platform.system() module. In addition to the existing sys.platform(). Am I on the right track?

joybh98 avatar Feb 28 '20 01:02 joybh98

Yes.

ilevkivskyi avatar Feb 28 '20 08:02 ilevkivskyi

@ilevkivskyi I was going through the code, and I think I should:

  1. Change function name from consider_sys_platform to consider_platform
  2. Change platform to sys.platform and add platforms in options.py (I've already done that in the PR, but I have to do a lot of work as it failed all the tests)
  3. Update tests to accommodate reflecting changes

Please let me know if this approach is right or not.

joybh98 avatar Mar 02 '20 02:03 joybh98

No, you need to change the "parsing" logic to recognize the platform.system() as well.

Btw, no need to ask me about this. There are other team members who can review your PR.

ilevkivskyi avatar Mar 02 '20 10:03 ilevkivskyi

@all please take a look at the PR and let me know what I am doing wrong, would really like your input.

joybh98 avatar Mar 07 '20 11:03 joybh98

#10686 has most of the implementation, but tests are failing and the PR is a bit stale. Any help with finishing up the PR would be appreciated!

JukkaL avatar Apr 23 '23 19:04 JukkaL

I'd also like to note that the platform.system() for is used and recommended in https://peps.python.org/pep-0508/ and https://peps.python.org/pep-0722/. And is what setuptools currently uses https://github.com/pypa/setuptools/pull/3979#discussion_r1367962801

Avasam avatar Oct 22 '23 21:10 Avasam

sysconfig.get_platform() offers another API for doing platform queries, so supporting each way of implementing them feels like it could turn into a neverending grind.

#9025 (respecting `sys.platform() assertions for specific code branches) would be a complementary approach that generalises to any mechanism for determining which platform code is running on.

ncoghlan avatar Aug 27 '24 06:08 ncoghlan