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

Consider promoting platform.system over sys.platform and os.name #5622

Closed
NeilGirdhar opened this issue Jul 8, 2023 · 8 comments
Closed
Labels
needs-decision Awaiting a decision from a maintainer rule Implementing or modifying a lint rule

Comments

@NeilGirdhar
Copy link

See this explanation.

@NeilGirdhar NeilGirdhar changed the title Consider urging prefer platform.system over sys.platform and os.name Consider promoting platform.system over sys.platform and os.name Jul 8, 2023
@charliermarsh charliermarsh added needs-decision Awaiting a decision from a maintainer rule Implementing or modifying a lint rule labels Jul 10, 2023
@real-or-random
Copy link

I disagree. Different methods have different purposes. As stated in the linked post, if you only care whether you're on posix or not, os.name == "posix" is the right thing to do.

@NeilGirdhar
Copy link
Author

I disagree. Different methods have different purposes. As stated in the linked post, if you only care whether you're on posix or not, os.name == "posix" is the right thing to do.

Why not do platform.system() in {'Linux', 'Darwin'}? It's easier to understand, it's easier to alter if what you're checking changes, and it doesn't depend on what future Python versions do with "OS specific modules".

@real-or-random
Copy link

Why not do platform.system() in {'Linux', 'Darwin'}?

This misses Android, iOS, and potentially FreeBSD.

It's just a different check. If I want to know how to interpret the os.popen return code, I may want to know whether I'm on a POSIX system or not, and the straightforward way to check this is os.name == "posix".

@NeilGirdhar
Copy link
Author

Got it, thanks. Does sys.platform have common uses too? It seems like most people will just check if it startswith something they know?

@MichaReiser
Copy link
Member

sys.platforms is used and understood by type checkers whereas os.platform isn't (but some type checkers do support it, e.g. Pyright).

A use case where you want to use sys.platform is when checking for specific APIs, e.g. https://docs.python.org/3/library/sys.html#sys.activate_stack_trampoline which is only available on linux and not all posix systems.

@MichaReiser MichaReiser closed this as not planned Won't fix, can't repro, duplicate, stale Mar 26, 2025
@NeilGirdhar
Copy link
Author

sys.platforms is used and understood by type checkers whereas os.platform isn't (but some type checkers do support it, e.g. Pyright).

A use case where you want to use sys.platform is when checking for specific APIs, e.g. https://docs.python.org/3/library/sys.html#sys.activate_stack_trampoline which is only available on linux and not all posix systems.

How would you check that? Wouldn't platform.system() == 'Linux' be what you want? As opposed to sys.platform.startswith('linux'), which seems much worse.

@MichaReiser
Copy link
Member

I'd use sys.platform == "linux"? startswith shouldn't be necessary anymore because of

Changed in version 3.3: On Linux, sys.platform doesn’t contain the major version anymore. It is always 'linux', instead of 'linux2' or 'linux3'.

@NeilGirdhar
Copy link
Author

Okay. I'm not sure if it's possible, but I think it would be good to come up with canonical ways of accomplishing various tasks and have Ruff nudge users towards these ways. If sys.platform == 'linux' is better than platform.system() == 'Linux', (or vice versa), it would be good to nudge everyone to write the same idea in the same way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-decision Awaiting a decision from a maintainer rule Implementing or modifying a lint rule
Projects
None yet
Development

No branches or pull requests

4 participants