python / python/mypy

ctypes nested array constructor doesn't work with sequences

Open
#6,212 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Using Python 3.7 and mypy 0.660 on the following code:

import ctypes

Quaternion = ctypes.c_int * 4
Pair = Quaternion * 2

quat = Quaternion(1, 2, 3, 4)
p = Pair((1, 2, 3, 4), (2, 3, 4, 5))

returns

$ mypy ctypes_nested.py 
ctypes_nested.py:7: error: Array constructor argument 1 of type "Tuple[builtins.int, builtins.int, builtins.int, builtins.int]" is not convertible to the array element type "ctypes.Array[ctypes.c_int]"
ctypes_nested.py:7: error: Array constructor argument 2 of type "Tuple[builtins.int, builtins.int, builtins.int, builtins.int]" is not convertible to the array element type "ctypes.Array[ctypes.c_int]"

even though at runtime this constructor works just fine. Of course, changing the constructor to Pair(Quaternion(1, 2, 3, 4), Quaternion(2, 3, 4, 5)) works just fine.

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 issue with the Python and ctypes snippet in the report, then trace mypy's handling of ctypes array constructor arguments and nested array elements. Done means Pair((1, 2, 3, 4), (2, 3, 4, 5)) type-checks without errors while the existing Quaternion and explicit nested-array forms remain valid.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.