python / python/mypy

unexpected attr-defined error in imports in dead code

Open
#20,408 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-reachability
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

mypy still gives an attr-defined error in dead code when importing platform-specific attributes from modules.

To Reproduce

import sys

if sys.platform != "win32":
    assert False

from ctypes import windll

Check it on any OS other than Windows.

Expected Behavior

No errors. This is valid code since it has the platform-specific guard.

Actual Behavior

$ uvx mypy example.py 
example.py:6: error: Module "ctypes" has no attribute "windll"  [attr-defined]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: mypy 1.19.0 (compiled: yes)
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.12

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.py snippet and uvx mypy example.py on a non-Windows system. Trace mypy's handling of platform-specific imports in dead code, then add focused coverage for this case if the relevant test location is found. Done means the guarded from ctypes import windll no longer produces an attr-defined error.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
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.