Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
jdufresne opened this issue Jun 26, 2022 · 6 comments
Closed

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

jdufresne opened this issue Jun 26, 2022 · 6 comments
Labels
status: deferred Issue or PR deferred until some precondition is fixed stubs: false positive Type checkers report false errors

Comments

@jdufresne
Copy link
Contributor

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

@srittau
Copy link
Collaborator

srittau commented Jun 26, 2022

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)

@srittau srittau added the stubs: false positive Type checkers report false errors label Jun 26, 2022
@AlexWaygood
Copy link
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.

@hauntsaninja
Copy link
Collaborator

hauntsaninja commented Jul 8, 2022

@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)

@AlexWaygood AlexWaygood added the status: deferred Issue or PR deferred until some precondition is fixed label Nov 1, 2023
@AlexWaygood
Copy link
Member

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

@srittau
Copy link
Collaborator

srittau commented Feb 14, 2024

See #11422 for the type var generics feature tracker.

@max-muoto
Copy link
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
Labels
status: deferred Issue or PR deferred until some precondition is fixed stubs: false positive Type checkers report false errors
6 participants