-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Remove automatic type promotion of bytearray
and memoryview
to bytes
#5697
Comments
…. PEP 688 eliminates the need for this hack, and it indicates that type checkers should remove support for this older behavior. This addresses #5697.
I don't think it's too soon. When I wrote that section I was thinking of a deprecation mechanism like this:
I don't know pyright's codebase well, but I imagine it may be hard to implement this warning in the right place, because presumably type compatibility is often checked in places where it's hard to show a warning to the user in the right place. So maybe it's fine to just make the change at once, as you suggest. python/mypy#12661 is a sample of the possible impact; it does affect some projects but it's relatively rare. |
Yeah, reporting errors at the point where pyright checks for type compatibility isn't possible. There are many cases where type incompatibility isn't even an error — e.g. protocol matching or overload matching. The mypy_primer run shows that this change will have more impact than I feared. I think the options are:
I'll need to think about this a bit more. All of these are unappealing at some level. |
I'm going to punt on this problem for now. We can revisit in 6 to 12 months. |
This is now getting in the way of additional improvements and bug fixes, so I've decided to "rip the bandaid off". Developers who were relying on promotion of |
I've added a new configuration option |
This is addressed in pyright 1.1.329, which I just published. It will also be included in a future release of pylance. |
Note that without this setting, there can be some confusing errors due to the mismatch between the runtime behaviour vs typing assumptions. Eg. I ended up here trying to figure out why:
Is giving:
Which seems to be because its assuming the supposed "bytes" argument might really be a bytearray/memoryview, so narrowing includes those types because the runtime isinstance check won't eliminate them. It may make more sense for this setting to default to on even on standard mode. |
PEP 688 aims to remove a poorly-documented "hack" in the type system, namely that
bytearray
andmemoryview
should be treated as implicit subtypes ofbytes
even though they have no runtime subclass relationship.PEP 688 recommends that type checkers deprecate and remove this support. I don't see a good way to do this gradually, so I'm tempted to "rip the bandaid off" and remove the old support.
I'll do a mypy_primer run to inform the decision.
@JelleZijlstra, what are your thoughts? Too soon?
The text was updated successfully, but these errors were encountered: