Skip to content

issues with memoryview #2390

@jonmac

Description

@jonmac

Running into a couple of issues with memoryview annotations.

Consider the following python3 program:

def fn(packet: memoryview) -> bytes:
    if chr(packet[0]) == "\xff":
        return packet.tobytes()
    else:
        return b"fail"

print(fn(memoryview(b"\xffabc")))
print(fn(memoryview(b"\xfeabc")))

Which when run has the following output:

b'\xffabc'
b'fail'

Presently mypy gives the following errors for this program:

$ mypy tmp/test.py
tmp/test.py: note: In function "fn":
tmp/test.py:2: error: Value of type "memoryview" is not indexable
tmp/test.py:3: error: "memoryview" has no attribute "tobytes"
tmp/test.py: note: At top level:
tmp/test.py:7: error: Argument 1 to "memoryview" has incompatible type "bytes"; expected "bytearray"
tmp/test.py:8: error: Argument 1 to "memoryview" has incompatible type "bytes"; expected "bytearray"

Each of the errors claimed here is incorrect.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions