Skip to content

Add a command line switch to disable bytes aliasing #12657

Closed
@dvarrazzo

Description

@dvarrazzo

Feature

Please add a command line and configuration parameter --disable-bytes-alias to avoid Mypy to consider bytes an alias for bytearray and memoryview.

Pitch

In the documentation of typing.ByteString, a little footnote probably, entered by the author's cat walking on the keyboard and entering random keypresses, says that bytes can be used as a shorthand to represent unrelated types such as memoryview, which have a different interface. See #12643.

As a result, it is not possible to obtain reliable code analysis on code using different ByteString types, such as:

def f(data: ByteString) -> str:
    if not isinstance(data, bytes):
        data = bytes(data)

    return data.decode()

Mypy will report that the if branch will never be executed, then it will proceed to complain that memoryview doesn't have a decode() method.

It would be nice to have this behaviour deprecated and eventually removed and forgotten (no harm intended to the cat). However, as this is a long process, it would be nice to give developers the possibility to disable this ill-thought rule sooner.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions