Skip to content

memoryview.cast("c") false error #8182

@jdufresne

Description

@jdufresne

Given the following code snippet:

buf = b"abcdefg"
view = memoryview(buf).cast("c")
for c in view:
    print(type(c))
    if c == b"c":
        print("Found c")

Inside the loop, the variable c is always type bytes. However, mypy believes it to be int and reports an error:

$ mypy --version
mypy 0.961 (compiled: yes)
$ mypy --strict ~/test.py
/home/jon/test.py:5: error: Non-overlapping equality check (left operand type: "int", right operand type: "Literal[b'c']")
Found 1 error in 1 file (checked 1 source file)

Running the script confirms the type is bytes:

$ python ~/test.py
<class 'bytes'>
<class 'bytes'>
<class 'bytes'>
Found c
<class 'bytes'>
<class 'bytes'>
<class 'bytes'>
<class 'bytes'>

Docs on this method:
https://docs.python.org/3/library/stdtypes.html#memoryview.cast

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: deferredIssue or PR deferred until some precondition is fixedstubs: false positiveType checkers report false errors

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions