From f646eb86e2da7c2b00a9b4a16a15cb698264324d Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Wed, 24 Apr 2024 16:19:12 +0200 Subject: [PATCH] Introduce SupportsContainsAndGetItem (#11827) --- stdlib/_typeshed/__init__.pyi | 13 +++++++++++-- stdlib/cgi.pyi | 4 ++-- .../influxdb_client/client/write/point.pyi | 2 +- stubs/six/six/__init__.pyi | 12 +++--------- 4 files changed, 17 insertions(+), 14 deletions(-) diff --git a/stdlib/_typeshed/__init__.pyi b/stdlib/_typeshed/__init__.pyi index db143e425..d7f54282b 100644 --- a/stdlib/_typeshed/__init__.pyi +++ b/stdlib/_typeshed/__init__.pyi @@ -151,13 +151,22 @@ class SupportsKeysAndGetItem(Protocol[_KT, _VT_co]): def keys(self) -> Iterable[_KT]: ... def __getitem__(self, key: _KT, /) -> _VT_co: ... -# stable +# This protocol is currently under discussion. Use SupportsGetItemAndContains +# instead, if you require the __contains__ method. +# See https://github.com/python/typeshed/issues/11822. class SupportsGetItem(Protocol[_KT_contra, _VT_co]): def __contains__(self, x: Any, /) -> bool: ... def __getitem__(self, key: _KT_contra, /) -> _VT_co: ... # stable -class SupportsItemAccess(SupportsGetItem[_KT_contra, _VT], Protocol[_KT_contra, _VT]): +class SupportsContainsAndGetItem(Protocol[_KT_contra, _VT_co]): + def __contains__(self, x: Any, /) -> bool: ... + def __getitem__(self, key: _KT_contra, /) -> _VT_co: ... + +# stable +class SupportsItemAccess(Protocol[_KT_contra, _VT]): + def __contains__(self, x: Any, /) -> bool: ... + def __getitem__(self, key: _KT_contra, /) -> _VT: ... def __setitem__(self, key: _KT_contra, value: _VT, /) -> None: ... def __delitem__(self, key: _KT_contra, /) -> None: ... diff --git a/stdlib/cgi.pyi b/stdlib/cgi.pyi index d20be33e3..3a2e2a91b 100644 --- a/stdlib/cgi.pyi +++ b/stdlib/cgi.pyi @@ -1,4 +1,4 @@ -from _typeshed import SupportsGetItem, SupportsItemAccess, Unused +from _typeshed import SupportsContainsAndGetItem, SupportsGetItem, SupportsItemAccess, Unused from builtins import list as _list, type as _type from collections.abc import Iterable, Iterator, Mapping from email.message import Message @@ -85,7 +85,7 @@ class FieldStorage: fp: IO[Any] | None = None, headers: Mapping[str, str] | Message | None = None, outerboundary: bytes = b"", - environ: SupportsGetItem[str, str] = ..., + environ: SupportsContainsAndGetItem[str, str] = ..., keep_blank_values: int = 0, strict_parsing: int = 0, limit: int | None = None, diff --git a/stubs/influxdb-client/influxdb_client/client/write/point.pyi b/stubs/influxdb-client/influxdb_client/client/write/point.pyi index 5f6e79615..53a9e1475 100644 --- a/stubs/influxdb-client/influxdb_client/client/write/point.pyi +++ b/stubs/influxdb-client/influxdb_client/client/write/point.pyi @@ -18,7 +18,7 @@ class Point: def measurement(measurement: str) -> Point: ... @staticmethod def from_dict( - dictionary: SupportsGetItem[str, Any], + dictionary: SupportsGetItem[str, Any], # TODO: Use SupportsContainsAndGetItem write_precision: _WritePrecision = "ns", *, record_measurement_name: str | None = ..., diff --git a/stubs/six/six/__init__.pyi b/stubs/six/six/__init__.pyi index d8d3150fc..7f317169b 100644 --- a/stubs/six/six/__init__.pyi +++ b/stubs/six/six/__init__.pyi @@ -2,23 +2,17 @@ import builtins import operator import types import unittest -from _typeshed import IdentityFunction, Unused, _KT_contra, _VT_co +from _typeshed import IdentityFunction, SupportsGetItem, Unused 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, Literal, NoReturn, Protocol, TypeVar, overload +from typing import Any, AnyStr, Literal, NoReturn, TypeVar, overload 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") @@ -68,7 +62,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