python / python/mypy

sys.platform checks assigned to variables are not handled correctly

Open
#14,604 2 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

When doing a sys.platform check, mypy seems to only recognise this when the check is done directly in the if statement, but not when the check is assigned to a variable.

e.g. https://github.com/kuwv/invoke/blob/typing/invoke/terminals.py#L33
This produces:

invoke/terminals.py:33:5: error: Module "ctypes" has no attribute "windll"  [attr-defined]
        from ctypes import (

But, if you change if WINDOWS to if sys.platform == "win32", then the error disappears.

I'm pretty sure I've seen mypy work correctly when sys.version_info checks are assigned to a variable, so not sure why sys.platform checks would be any different.

Tested with 0.991.

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 example in invoke/terminals.py around line 33, comparing the assigned-variable check with the direct sys.platform check. Trace mypy's handling of sys.platform conditions and add coverage for the assigned form; done means the ctypes.windll error is no longer reported for that case.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.