You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
SIM102 (collapsible-if) has an exception for version and os checks. This currently matches sys.platform.
ifsys.platform=="linux": # no SIM102iffoo:
Analogously, it should also be disabled os.name and platform.system():
ifos.name=="posix": # SIM102iffoo:
See https://stackoverflow.com/a/58071295 to learn about the various methods. (And if you've looked at this post, you're also in a good position to have an opinion on the otherwise unrelated #5622.)
The text was updated successfully, but these errors were encountered:
Can you tell me a bit more why you think SIM102 should be disabled. Is it because some type checkers don't support narrowing if it's an if-else expression?
Can you tell me a bit more why you think SIM102 should be disabled. Is it because some type checkers don't support narrowing if it's an if-else expression?
Okay, TIL, and now that you ask, I'm not so sure anymore.
Until now, my point was merely readability: I think branching on a Python version or on an OS is a different thing than branching on business logic, so believe it's good to have it in a separate if: Then your brain can easily focus on the part that concerns a particular OS, and skip the other parts. But this is my personal view, and I'm not sure how objective it is. My thinking was simply that if it has been decided that checks for sys.platform and sys.version_info should be treated differently for readability, then the same should apply to os.name and platform.system().
But I was not aware that type checkers may be a reason. This goes back to PEP 484, which says "Type checkers are expected to understand simple version and platform checks, e.g.: (...) if sys.version_info[0] >= 3: (...) if sys.platform == 'win32':". The current docs are similar, but use sys.version_info >= (3,12) as an example.
After some research, I found this issue about the set of conditions being underspecified... It's a good summary of what is supported by different typecheckers. For example, pyright supports os.name but mypy doesn't. None of them support platform.system()`.
My feeling is that a linter should be conservative here. If someone uses os.name to make pyright happy, this seems fine. But given that this is still very much under debate, I'm not sure if it's the right time to change SIM102...
In either case, I think the comments here should be updated:
If I'm not mistaken, this file is only about SIM102, so this is not about ternaries. Moreover, is_sys_version_block also checks for sys.platform, so the name is misleading.
Summary
SIM102 (collapsible-if) has an exception for version and os checks. This currently matches
sys.platform
.Analogously, it should also be disabled
os.name
andplatform.system()
:See https://stackoverflow.com/a/58071295 to learn about the various methods. (And if you've looked at this post, you're also in a good position to have an opinion on the otherwise unrelated #5622.)
The text was updated successfully, but these errors were encountered: