Skip to content

Mypy not treating bytes as typing.ByteString #7714

Closed

Description

@willmcgugan

According to the docs, an argument typed as bytes should also accept bytearray and memoryview, but this doesn't seem to be the case.

The following example demonstrates this:

def process(b: bytes) -> None:
    pass

process(memoryview(b"foo"))

Mypy produces the following error:

error: Argument 1 to "process" has incompatible type "memoryview"; expected "bytes"

I found #4871 which is essentially the same issue. If there hasn't been any relevant changes since April 2008, perhaps its a docs issue only?

Activity

  1. srittau commented on Oct 15, 2019

    @srittau
    Contributor

    In typeshed I have directed people to just use bytes instead of a union with memoryview, because of the documentation. In case we just change the documentation, we'd need to change the stubs as well.

  2. ilevkivskyi commented on Oct 15, 2019

    @ilevkivskyi
    Member

    I think this should be just fixed in mypy. It should be not hard, one just needs to update TYPE_PROMOTIONS_PYTHON2 and TYPE_PROMOTIONS_PYTHON3 in mypy/semanal_classprop.py and add a test for this. Anyone wants to make a PR?

  3. added a commit that references this issue on Oct 15, 2019
    e4623b9
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

    false-positivegood-first-issuepriority-1-normal

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions