Recognize platform.system() as well as sys.platform
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.
On one hand this is pretty niche, but on other hand should be not hard to implement.
Hi @ilevkivskyi I am interested in working on this issue, please guide me on where I can start.
Look at consider_sys_platform() in mypy/reachability.py.
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()
@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?
Yes.
@ilevkivskyi I was going through the code, and I think I should:
- Change function name from
consider_sys_platformtoconsider_platform - Change
platformtosys.platformand addplatformsinoptions.py(I've already done that in the PR, but I have to do a lot of work as it failed all the tests) - Update tests to accommodate reflecting changes
Please let me know if this approach is right or not.
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.
@all please take a look at the PR and let me know what I am doing wrong, would really like your input.
#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!
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
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.