-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Fix usage of byte2int with bytes #9152
Conversation
This comment has been minimized.
This comment has been minimized.
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 from typing import Any, Protocol, TypeVar, Union
_T = TypeVar("_T")
_KT_contra = TypeVar("_KT_contra", contravariant=True)
_VT_co = TypeVar("_VT_co", covariant=True)
class SupportsGetItem(Protocol[_KT_contra, _VT_co]):
def __contains__(self, __x: Any) -> bool:
...
def __getitem__(self, __key: _KT_contra) -> _VT_co:
...
###
def foo(obj: SupportsGetItem[int, _T]) -> _T:
return obj[0]
bar: int = foo(b"1")
###
import six
six.byte2int(b"1")
###
def byte2int(c: Union[bytes, int]) -> int:
if isinstance(c, bytes):
return six.byte2int(c)
return c
bar = byte2int(b"1") mypy 0.990 (compiled: yes) |
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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. 🤖🎉 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @Avasam!
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#14032indexbytes
seems unnafected