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

Add a command line switch to disable bytes aliasing #12657

Closed
dvarrazzo opened this issue Apr 22, 2022 · 3 comments
Closed

Add a command line switch to disable bytes aliasing #12657

dvarrazzo opened this issue Apr 22, 2022 · 3 comments
Labels

Comments

@dvarrazzo
Copy link

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.

JelleZijlstra added a commit to JelleZijlstra/mypy that referenced this issue Apr 23, 2022
@AlexWaygood
Copy link
Member

Implemented in #13952!

@hauntsaninja
Copy link
Collaborator

hauntsaninja commented Oct 27, 2022

Let's keep this one open. #13952 does implement this, but currently only for testing purposes, not as a documented feature of mypy. I don't recommend users rely on its existence or behaviour for now.

@hauntsaninja hauntsaninja reopened this Oct 27, 2022
@hauntsaninja
Copy link
Collaborator

Documented feature can be tracked at #15313

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants