From f99a87a825faec801c7a37606f85e2352832bb0d Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Thu, 20 Mar 2025 13:33:40 +0100 Subject: [PATCH 1/4] Constrain memoryview type var to allowed types --- stdlib/builtins.pyi | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index 4e2484509c1d..e466d2827529 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -76,7 +76,7 @@ if sys.version_info >= (3, 9): from types import GenericAlias _T = TypeVar("_T") -_I = TypeVar("_I", default=int) +_I = TypeVar("_I", int, bytes, float, bool, default=int) # possibly memoryview types _T_co = TypeVar("_T_co", covariant=True) _T_contra = TypeVar("_T_contra", contravariant=True) _R_co = TypeVar("_R_co", covariant=True) @@ -882,7 +882,7 @@ class memoryview(Sequence[_I]): @overload def cast(self, format: Literal["?"], shape: list[int] | tuple[int, ...] = ...) -> memoryview[bool]: ... @overload - def cast(self, format: _IntegerFormats, shape: list[int] | tuple[int, ...] = ...) -> memoryview: ... + def cast(self, format: _IntegerFormats, shape: list[int] | tuple[int, ...] = ...) -> memoryview[int]: ... @overload def __getitem__(self, key: SupportsIndex | tuple[SupportsIndex, ...], /) -> _I: ... @overload From 6a7f55ace2aaaf794ab83e1a51b20f1f5e95b7e1 Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Wed, 5 Nov 2025 10:50:53 +0100 Subject: [PATCH 2/4] Change memoryview generic default to Any --- stdlib/builtins.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index 0126b17b49d9..5c95d6fe0799 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -79,7 +79,7 @@ if sys.version_info >= (3, 14): from _typeshed import AnnotateFunc _T = TypeVar("_T") -_I = TypeVar("_I", int, bytes, float, bool, default=int) # possibly memoryview types +_I = TypeVar("_I", int, bytes, float, bool, default=Any) # possibly memoryview types _T_co = TypeVar("_T_co", covariant=True) _T_contra = TypeVar("_T_contra", contravariant=True) _R_co = TypeVar("_R_co", covariant=True) From 010ca90badd68acb113116faa1cf85aab2c6c3f1 Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Wed, 5 Nov 2025 10:56:38 +0100 Subject: [PATCH 3/4] Silence mypy --- stdlib/builtins.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index 5c95d6fe0799..c0d0c28dd145 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -79,7 +79,7 @@ if sys.version_info >= (3, 14): from _typeshed import AnnotateFunc _T = TypeVar("_T") -_I = TypeVar("_I", int, bytes, float, bool, default=Any) # possibly memoryview types +_I = TypeVar("_I", int, bytes, float, bool, default=Any) # possibly memoryview types # type: ignore[misc] _T_co = TypeVar("_T_co", covariant=True) _T_contra = TypeVar("_T_contra", contravariant=True) _R_co = TypeVar("_R_co", covariant=True) From ffb1e981e1968384ace345f54a10bc7b7241d0fc Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Wed, 5 Nov 2025 11:00:22 +0100 Subject: [PATCH 4/4] Fix, reference mypy issue --- stdlib/builtins.pyi | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index c0d0c28dd145..4b3d5ff7a70f 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -79,7 +79,8 @@ if sys.version_info >= (3, 14): from _typeshed import AnnotateFunc _T = TypeVar("_T") -_I = TypeVar("_I", int, bytes, float, bool, default=Any) # possibly memoryview types # type: ignore[misc] +# Type ignore needed because of https://github.com/python/mypy/issues/20184. +_I = TypeVar("_I", int, bytes, float, bool, default=Any) # type: ignore[misc] # possibly memoryview types _T_co = TypeVar("_T_co", covariant=True) _T_contra = TypeVar("_T_contra", contravariant=True) _R_co = TypeVar("_R_co", covariant=True)