pytype icon indicating copy to clipboard operation
pytype copied to clipboard

memoryview should accept mmap, array, and _CData instances (false positive)

Open cebtenzzre opened this issue 2 years ago • 1 comments

Software versions

Python 3.9.7 pytype 2021.11.18

Testcases

repr1.py:

import mmap
x = mmap.mmap(-1, 4096)
y = memoryview(x)

repr2.py:

import array
x = array.array('B')
y = memoryview(x)

repr3.py:

import ctypes
x = ctypes.c_char()
y = memoryview(x)

Description

$ pytype-single repr1.py
File "repr1.py", line 3, in <module>: Function memoryview.__init__ was called with the wrong arguments [wrong-arg-types]
         Expected: (self, object: bytes)
  Actually passed: (self, object: mmap.mmap)

For more details, see https://google.github.io/pytype/errors.html#wrong-arg-types
$ pytype-single repr2.py
File "repr2.py", line 3, in <module>: Function memoryview.__init__ was called with the wrong arguments [wrong-arg-types]
         Expected: (self, object: bytes)
  Actually passed: (self, object: array.array[int])

For more details, see https://google.github.io/pytype/errors.html#wrong-arg-types
$ pytype-single repr3.py
File "repr3.py", line 3, in <module>: Function memoryview.__init__ was called with the wrong arguments [wrong-arg-types]
         Expected: (self, object: bytes)
  Actually passed: (self, object: ctypes.c_char)

For more details, see https://google.github.io/pytype/errors.html#wrong-arg-types

At runtime, all of these examples are valid. In typeshed, memoryview accepts any instance of _typeshed.ReadableBuffer.

cebtenzzre avatar Dec 09 '21 20:12 cebtenzzre

This is unfortunately a bit of a pain to fix because pytype's stubs are organized in such a way that builtins and typing are expected to be self-contained and not depend on the rest of the standard library. I skimmed through https://docs.python.org/3/c-api/buffer.html#buffer-protocol to see if buffers have a common interface that could be described with a typing.Protocol, but it doesn't seem like they do :/

rchen152 avatar Dec 11 '21 01:12 rchen152