Uh oh!
There was an error while loading. Please reload this page.
Fix usage of byte2int with bytes - #9152
Conversation
This comment has been minimized.
This comment has been minimized.
JelleZijlstra
commented
Nov 10, 2022
Could you add a test case for this? Also worth reporting as a separate mypy bug I think. |
I was going to but I can't replicate it anymore O_o fromtypingimportAny, Protocol, TypeVar, Union_T=TypeVar("_T")
_KT_contra=TypeVar("_KT_contra", contravariant=True)
_VT_co=TypeVar("_VT_co", covariant=True)
classSupportsGetItem(Protocol[_KT_contra, _VT_co]):
def__contains__(self, __x: Any) ->bool:
...
def__getitem__(self, __key: _KT_contra) ->_VT_co:
...
###deffoo(obj: SupportsGetItem[int, _T]) ->_T:
returnobj[0]
bar: int=foo(b"1")
###importsixsix.byte2int(b"1")
###defbyte2int(c: Union[bytes, int]) ->int:
ifisinstance(c, bytes):
returnsix.byte2int(c)
returncbar=byte2int(b"1")mypy 0.990 (compiled: yes) |
JelleZijlstra
commented
Nov 10, 2022
Interesting, different mypy version? |
Actually it differs whether I point to my local typeshed repo or not... Even if I revert the change to The other culprit could be The change from object to any was actually important as part of #9117 Edit: Changing Then would this PR make sense as a stop-gap until type-checkers update their shipped typeshed stubs? |
This comment has been minimized.
This comment has been minimized.
For the record, having to use |
There was a problem hiding this comment.
A better solution might be to just copy the new version of SupportsGetItem into the stubs for six, and then use that directly instead of importing it from _typeshed (with a TODO comment saying we should switch to the _typeshed version once mypy updates its vendored copy of typeshed and makes a new release). Then we wouldn't have to make the function an overloaded function.
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
Fixes#9145 . Restores functionality specifically with bytes.
Works around a mypy bug that thinks
SupportsGetItem[int, <nothing>]is expected when passed bytes.May be python/mypy#14032indexbytesseems unnafected