Skip to content

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

Closed

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

Activity

  1. srittau commented on Jun 26, 2022

    @srittau
    Collaborator

    It looks to me that memoryview should be generic over the format. That said, I'm uncomfortable making a change like this as long as we don't have type var defaults. (python/typing#307)

  2. AlexWaygood commented on Jun 26, 2022

    @AlexWaygood
    Member

    It looks to me that memoryview should be generic over the format.

    Yeah, this might be quite a big breaking change given that it's a builtin.

  3. hauntsaninja commented on Jul 8, 2022

    @hauntsaninja
    Collaborator

    @jdufresne Off-topic, but I skimmed the chardet PR you linked. The one nice thing the typeshed stubs for chardet do is distinguish between cases when ResultDict fields can be None or not. Might be worth seeing if there's something worth stealing from there (make sure to check latest typeshed stubs since there were some recent changes)

  4. AlexWaygood commented on Nov 1, 2023

    @AlexWaygood
    Member

    Marking as deferred unless/until PEP 696 is accepted by the Steering Council and implemented by type checkers.

  5. srittau commented on Feb 14, 2024

    @srittau
    Collaborator

    See #11422 for the type var generics feature tracker.

  6. max-muoto commented on Jul 14, 2024

    @max-muoto
    Contributor

    @srittau This can be closed out now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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