python / python/mypy

Respect sys.platform platform check assertion inside functions

Open
#9,025 5 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature priority-1-normal
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

This is a feature request

From the docs
https://mypy.readthedocs.io/en/stable/common_issues.html#python-version-and-system-platform-checks

More specifically, mypy will understand the use of sys.version_info and sys.platform checks
within if/elif/else statements.

and

As a special case, you can also use one of these checks in a top-level (unindented) assert; this makes mypy skip the rest of the file.

It would be nice if this kind of code would also typecheck on mac/linux.
Currently the option is to refactor such that you can define the entire function within an if sys.platform == "win32"

class A:
   def func_on_win_only() -> str:
      assert sys.platform == "win32"
      return sys.getwindowsversion()

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

Start by reproducing the example on a non-Windows platform and read the linked mypy documentation on sys.platform checks. Trace the existing handling for top-level assertions, then add regression coverage for an assertion inside a function and verify that the Windows-only call type-checks on macOS or Linux.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.