Skip to content

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

Closed as duplicate

Description

@Jacobfaib
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).

Activity

  1. brianschubert commented on Apr 2, 2025

    @brianschubert
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions