Skip to content

sys.platform != "foo" defeats platform-specific path #18873

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

Closed
Jacobfaib opened this issue Apr 2, 2025 · 1 comment
Closed

sys.platform != "foo" defeats platform-specific path #18873

Jacobfaib opened this issue Apr 2, 2025 · 1 comment
Labels
bug mypy got something wrong

Comments

@Jacobfaib
Copy link

Jacobfaib commented Apr 2, 2025

import sys


def foo() -> None:
    if sys.platform != "darwin":
        print("not darwin")
        return

    print("darwin")
$ mypy --platform linux plat.py
plat.py: note: In function "foo":
plat.py:9:5: error: Statement is unreachable  [unreachable]
        print("darwin")
        ^~~~~~~~~~~~~~~
Found 1 error in 1 file (checked 1 source file)

mypy claims to understand sys.platform checks, yet is defeated by !=.

I would expect mypy not to warn about unreachable code here:

  1. I am not assigning sys.platform to an intermediate
  2. I am checking against a literal value (which in the documentation mypy also claims to understand).
@Jacobfaib Jacobfaib added the bug mypy got something wrong label Apr 2, 2025
@brianschubert
Copy link
Collaborator

Thanks for the report. Platform checks currently only apply within the if/elif/else statement. If you use an explicit else branch, this will work as expected:

import sys


def foo() -> None:
    if sys.platform != "darwin":
        print("not darwin")
        return
    else:
        print("darwin")  # OK

Closing as duplicate of #10773

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

No branches or pull requests

2 participants