From 605b212dea172e2adf86f53ffa1ef068d7c1cd8f Mon Sep 17 00:00:00 2001 From: Avasam Date: Thu, 10 Nov 2022 11:54:39 -0500 Subject: [PATCH 1/4] Fix usage of byte2int with bytes --- stubs/six/six/__init__.pyi | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/stubs/six/six/__init__.pyi b/stubs/six/six/__init__.pyi index d2f6e4b15033..0ee5f00f6d83 100644 --- a/stubs/six/six/__init__.pyi +++ b/stubs/six/six/__init__.pyi @@ -65,7 +65,12 @@ unichr = chr def int2byte(i: int) -> bytes: ... -# Should be `byte2int: operator.itemgetter[int]`. But a bug in mypy prevents using TypeVar in itemgetter__call__ +# This overload works around a mypy bug that thinks `SupportsGetItem[int, ]` is expected when passed bytes +@overload +def byte2int(obj: bytes) -> int: ... + +# Should be `byte2int: operator.itemgetter[int]`. But a bug in mypy prevents using TypeVar in itemgetter.__call__ +@overload def byte2int(obj: SupportsGetItem[int, _T]) -> _T: ... indexbytes = operator.getitem From 385a363f75cafcf766a4b71a2075039986a0f34a Mon Sep 17 00:00:00 2001 From: Avasam Date: Thu, 10 Nov 2022 13:46:36 -0500 Subject: [PATCH 2/4] Update comment --- stubs/six/six/__init__.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stubs/six/six/__init__.pyi b/stubs/six/six/__init__.pyi index 0ee5f00f6d83..881e6ed9722e 100644 --- a/stubs/six/six/__init__.pyi +++ b/stubs/six/six/__init__.pyi @@ -65,7 +65,7 @@ unichr = chr def int2byte(i: int) -> bytes: ... -# This overload works around a mypy bug that thinks `SupportsGetItem[int, ]` is expected when passed bytes +# This overload is temporary until typecheckers update their shipped version of typeshed @overload def byte2int(obj: bytes) -> int: ... From 9b4261bfb46488ee2e91b500d6a416efefe26a13 Mon Sep 17 00:00:00 2001 From: Avasam Date: Fri, 11 Nov 2022 23:00:37 -0500 Subject: [PATCH 3/4] better solution --- stubs/six/six/__init__.pyi | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/stubs/six/six/__init__.pyi b/stubs/six/six/__init__.pyi index 881e6ed9722e..207ac09298bd 100644 --- a/stubs/six/six/__init__.pyi +++ b/stubs/six/six/__init__.pyi @@ -2,18 +2,24 @@ import builtins import operator import types import unittest -from _typeshed import IdentityFunction, SupportsGetItem +from _typeshed import IdentityFunction, _KT_contra, _VT_co from builtins import next as next from collections.abc import Callable, ItemsView, Iterable, Iterator as _Iterator, KeysView, Mapping, ValuesView from functools import wraps as wraps from importlib.util import spec_from_loader as spec_from_loader from io import BytesIO as BytesIO, StringIO as StringIO from re import Pattern -from typing import Any, AnyStr, NoReturn, TypeVar, overload +from typing import Any, AnyStr, NoReturn, Protocol, TypeVar, overload from typing_extensions import Literal from six import moves as moves +# TODO: We should switch to the _typeshed version of SupportsGetItem +# once mypy updates its vendored copy of typeshed and makes a new release +class SupportsGetItem(Protocol[_KT_contra, _VT_co]): + def __contains__(self, __x: Any) -> bool: ... + def __getitem__(self, __key: _KT_contra) -> _VT_co: ... + _T = TypeVar("_T") _K = TypeVar("_K") _V = TypeVar("_V") @@ -65,12 +71,7 @@ unichr = chr def int2byte(i: int) -> bytes: ... -# This overload is temporary until typecheckers update their shipped version of typeshed -@overload -def byte2int(obj: bytes) -> int: ... - # Should be `byte2int: operator.itemgetter[int]`. But a bug in mypy prevents using TypeVar in itemgetter.__call__ -@overload def byte2int(obj: SupportsGetItem[int, _T]) -> _T: ... indexbytes = operator.getitem From e0c0a9a229f1a9031d4689e715e50f9e93f7f453 Mon Sep 17 00:00:00 2001 From: Avasam Date: Fri, 11 Nov 2022 23:02:46 -0500 Subject: [PATCH 4/4] private _SupportsGetItem --- stubs/six/six/__init__.pyi | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stubs/six/six/__init__.pyi b/stubs/six/six/__init__.pyi index 207ac09298bd..bb87f71f2751 100644 --- a/stubs/six/six/__init__.pyi +++ b/stubs/six/six/__init__.pyi @@ -16,7 +16,7 @@ from six import moves as moves # TODO: We should switch to the _typeshed version of SupportsGetItem # once mypy updates its vendored copy of typeshed and makes a new release -class SupportsGetItem(Protocol[_KT_contra, _VT_co]): +class _SupportsGetItem(Protocol[_KT_contra, _VT_co]): def __contains__(self, __x: Any) -> bool: ... def __getitem__(self, __key: _KT_contra) -> _VT_co: ... @@ -72,7 +72,7 @@ unichr = chr def int2byte(i: int) -> bytes: ... # Should be `byte2int: operator.itemgetter[int]`. But a bug in mypy prevents using TypeVar in itemgetter.__call__ -def byte2int(obj: SupportsGetItem[int, _T]) -> _T: ... +def byte2int(obj: _SupportsGetItem[int, _T]) -> _T: ... indexbytes = operator.getitem iterbytes = iter