python / python/mypy

ctypes.Structure and Iterable

Open
#10,272 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Bug Report

For some reason ctypes.Structure instances pass mypy's isinstance checks although mypy also claims that they are not iterable.
For example, consider the following code:

from ctypes import Structure

from typing import Iterable


class S(Structure):
    pass


s = S()

if isinstance(s, Iterable):
    for _ in s:
        pass

mypy gives the following error:

test.py:13: error: "S" has no attribute "__iter__" (not iterable)
Found 1 error in 1 file (checked 1 source file)

replacing it with iter(s) make mypy happy again.

Your Environment

  • Mypy version used: mypy 0.790
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: Python 3.8.6
  • Operating system and version: Linux ubuntu 5.10.0 #17 SMP Mon Dec 14 10:04:17 IST 2020 x86_64 x86_64 x86_64 GNU/Linux

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 running the reported Python reproducer with mypy and compare the isinstance(s, Iterable) branch with the iter(s) call. Trace the relevant type-checking behavior for ctypes.Structure and Iterable, then add a regression test showing the expected consistency.

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
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.