python / python/mypy

mypy fails to recognize functions as generators when `sys.platform` is used in conditions

Open
#5,678 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug priority-1-normal topic-runtime-semantics
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

mypy seems to be too thorough when interpreting if sys.platform == ... conditions. Consider the following file:

import sys
from typing import Generator

def f() -> Generator[int, None, None]:
    if sys.platform == "darwin":
        yield 1

I'm trying to run mypy for this file on Linux using mypy gen.py

Expected behavior:
mypy test passes for this file on Linux

Actual behavior:
I get this on Linux:

$ mypy gen.py 
gen.py:4: error: Missing return statement

At the same time, mypy on macOS does not complain. I think that this is related to #698

Platform information:

$ uname -sr; mypy --version; python --version
Linux 4.15.0-34-generic
mypy 0.630
Python 2.7.15rc1

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the report using the gen.py example and run mypy on Linux, then compare the result with macOS behavior. Trace how the checker handles sys.platform conditions and generator functions; done means the example no longer reports a missing return statement on Linux without regressing the macOS result.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.