-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
bytearray
treated as a subclass of bytes
#8505
Comments
I see some PRs about |
Mypy internally promotes bytearray to bytes for convenience, similar to how int is promoted to float. I thought this was specified in PEP 484, but as it turns out, it isn't. The only mention of this behavior that I can find the mypy docs is in https://mypy.readthedocs.io/en/stable/python2.html?highlight=promotion#type-checking-python-2-code, which mentions the int-float promotion (but not the bytearray-bytes promotion) as an aside. It's definitely intentional behavior, but it should be documented better. |
It is mentioned in the typing documentation. One of the problems is that the documentation is currently split between several PEPs, the typing, and the mypy documentation. Edit: The typing documentation implies that this promotion is valid for argument types only. |
Thanks for the explanations. I'm not sure I agree with convenience over correctness in a type system, but I can put that aside for now and be pragmatic. I wonder if we can tighten up the implementation so that Btw, my concrete use case where |
Just thinking out loud here, but could we introduce a new option + CLI flag to make this more strict? I found the dict with the type promotions in |
This is a frustrating one. We could consider a flag, though I'd want to investigate the impact first. One issue is that it would be hard to make it work in a per-module way with the existing implementation. If you want to submit a PR that adds a flag or changes the behavior, take a look at My intuition, though, is that the damage will be moderate and manageable but that maybe the scope of this problem is still not big enough to be worth doing that fixing. |
The main issue I see with changing this (even with an opt-in flag) is that existing stubs assume that you don't need to write |
I don't expect this to be a trivial change that will happen overnight, but perhaps we can move in that direction. If there are issues with stubs, they can be updated over time to accept to the union. Because these changes are backwards compatible for users running without the opt-in flag, we don't need to resolve everything all at once and can update them as we go. The 8k references to It's important to be able to trust a type checker, and imho the long term roadmap should value correctness over convenience. |
I just encountered a related issue: In a project I want to optimize the usage of some def decode_to_str(data: bytes) -> str:
return data.decode() # will fail at runtime: AttributeError: 'memoryview' object has no attribute 'decode'
# data = b"a string as bytes" # slow slicing, refactor to memoryview
data = memoryview(b"a string as bytes")
print(decode_to_str(data[:8]))
Taking up the example of a project with 8k usages of |
Based on #12661, the impact doesn't seem terrible, but some projects will be quite heavily impacted. Some of the errors will probably go away once stubs have been updated. A few additional things to consider:
|
Closing as a duplicate of #15313 |
No mypy error (and the expected
AssertionError
at runtime)Mypy error about assigning wrong type (and the expected
AssertionError
at runtime)mypy 0.761 on CPython 3.7.4
--strict
Typeshed appears to be fine, with
bytes
andbytearray
both inheriting fromByteString
, but no inheritance ofbytearray
frombytes
.The text was updated successfully, but these errors were encountered: