Thanks to visit codestin.com
Credit goes to github.com

Skip to content

bytes broader than bytearray? #8970

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

Closed
gsakkis opened this issue Oct 23, 2022 · 3 comments
Closed

bytes broader than bytearray? #8970

gsakkis opened this issue Oct 23, 2022 · 3 comments

Comments

@gsakkis
Copy link

gsakkis commented Oct 23, 2022

Mypy errors with Overloaded function signature 2 will never be matched: signature 1's parameter type(s) are the same or broader [misc] for the following class:

from typing import Optional, Union, overload


class Transform:
    @overload
    def apply(self, b: bytes) -> bytes:
        """Return the transformed bytestring of the input"""

    @overload
    def apply(self, b: bytearray) -> None:
        """Transform the input bytearray in place"""

    def apply(self, b: Union[bytes, bytearray]) -> Optional[bytes]:
        pass

Changing the order of the two @overload doesn't give the error.

mypy --version
mypy 0.981 (compiled: yes)
@srittau
Copy link
Collaborator

srittau commented Oct 23, 2022

Yes, bytes includes bytearray in argument types: https://docs.python.org/3/library/typing.html?highlight=typing#typing.ByteString.

@srittau srittau closed this as not planned Won't fix, can't repro, duplicate, stale Oct 23, 2022
@gsakkis
Copy link
Author

gsakkis commented Oct 23, 2022

So how is someone supposed to differentiate between the (actual) bytes vs bytearray types?

def f(b: Union[bytes, bytearray]) -> None:
    if isinstance(b, bytes):
        print("bytes")
    else:
        print("bytearray")

f(bytearray())

Mypy claims Statement is unreachable for the else clause but it is clearly reachable.

@AlexWaygood
Copy link
Member

You may be interested in PEP 688 (written by @JelleZijlstra), which attempts to address this problem. The PEP is discussed here: https://discuss.python.org/t/pep-688-take-2-making-the-buffer-protocol-accessible-in-python/19756/1.

This problem was also recently discussed here: python/mypy#12643.

Regardless, this isn't really a typeshed issue; there's nothing we can do about it in this repo :)

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

No branches or pull requests

3 participants