From 658e5b1072fcfba84cbe1aaa2e880ace5051ba4f Mon Sep 17 00:00:00 2001 From: Ilya Konstantinov Date: Fri, 2 Dec 2022 15:42:04 -0500 Subject: [PATCH 01/24] Annotate google.protobuf.struct_pb2 --- .../protobuf/internal/well_known_types.pyi | 45 ++++++++++--------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/stubs/protobuf/google/protobuf/internal/well_known_types.pyi b/stubs/protobuf/google/protobuf/internal/well_known_types.pyi index 3717bb42e090..da8515ee7f95 100644 --- a/stubs/protobuf/google/protobuf/internal/well_known_types.pyi +++ b/stubs/protobuf/google/protobuf/internal/well_known_types.pyi @@ -1,5 +1,10 @@ from datetime import datetime, timedelta, tzinfo -from typing import Any as tAny +from typing import Any as tAny, Mapping, Sequence, Iterable, Iterator + +from google.protobuf import struct_pb2 + +_StructValue: TypeAlias = struct_pb2.Struct | struct_pb2.ListValue | str | float | bool | None +_StructValueArg: TypeAlias = _StructValue | Mapping[_StructValueArg] | Sequence[_StructValueArg] class Any: type_url: tAny = ... @@ -64,28 +69,28 @@ class _FieldMaskTree: def MergeMessage(self, source: tAny, destination: tAny, replace_message: tAny, replace_repeated: tAny) -> None: ... class Struct: - def __getitem__(self, key: tAny): ... - def __contains__(self, item: tAny): ... - def __setitem__(self, key: tAny, value: tAny) -> None: ... - def __delitem__(self, key: tAny) -> None: ... + def __getitem__(self, key: str) -> _StructValue: ... + def __contains__(self, item: _StructValueArg) -> bool: ... + def __setitem__(self, key: str, value: _JsonImmutableValue) -> None: ... + def __delitem__(self, key: str) -> None: ... def __len__(self) -> int: ... - def __iter__(self): ... - def keys(self): ... - def values(self): ... - def items(self): ... - def get_or_create_list(self, key: tAny): ... - def get_or_create_struct(self, key: tAny): ... - def update(self, dictionary: tAny) -> None: ... + def __iter__(self) -> Iterator[str]: ... + def keys(self) -> Iterator[str]: ... + def values(self) -> Iterator[_StructValue]: ... + def items(self) -> Iterator[tuple[str, _StructValue]]: ... + def get_or_create_list(self, key: str) -> struct_pb2.ListValue: ... + def get_or_create_struct(self, key: str) -> struct_pb2.Struct: ... + def update(self, dictionary: Mapping[str, _StructValueArg]) -> None: ... class ListValue: def __len__(self) -> int: ... - def append(self, value: tAny) -> None: ... - def extend(self, elem_seq: tAny) -> None: ... - def __getitem__(self, index: tAny): ... - def __setitem__(self, index: tAny, value: tAny) -> None: ... - def __delitem__(self, key: tAny) -> None: ... - def items(self) -> None: ... - def add_struct(self): ... - def add_list(self): ... + def append(self, value: _StructValue) -> None: ... + def extend(self, elem_seq: Iterable[_StructValue]) -> None: ... + def __getitem__(self, index: int) -> _StructValue: ... + def __setitem__(self, index: int, value: _StructValueArg) -> None: ... + def __delitem__(self, key: int) -> None: ... + def items(self) -> Iterator[_StructValue]: ... + def add_struct(self) -> struct_pb2.Struct: ... + def add_list(self) -> struct_pb2.ListValue: ... WKTBASES: dict[str, type[tAny]] From 96335b03f4d04e65854d18a612bb2f9c132380a1 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 2 Dec 2022 20:43:18 +0000 Subject: [PATCH 02/24] [pre-commit.ci] auto fixes from pre-commit.com hooks --- stubs/protobuf/google/protobuf/internal/well_known_types.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stubs/protobuf/google/protobuf/internal/well_known_types.pyi b/stubs/protobuf/google/protobuf/internal/well_known_types.pyi index da8515ee7f95..6cbaf24d24d8 100644 --- a/stubs/protobuf/google/protobuf/internal/well_known_types.pyi +++ b/stubs/protobuf/google/protobuf/internal/well_known_types.pyi @@ -1,5 +1,5 @@ from datetime import datetime, timedelta, tzinfo -from typing import Any as tAny, Mapping, Sequence, Iterable, Iterator +from typing import Any as tAny, Iterable, Iterator, Mapping, Sequence from google.protobuf import struct_pb2 From 31dcaa1475b878e07b728ea952493d5adc79f882 Mon Sep 17 00:00:00 2001 From: Ilya Konstantinov Date: Fri, 2 Dec 2022 15:48:39 -0500 Subject: [PATCH 03/24] oops --- .../google/protobuf/internal/well_known_types.pyi | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/stubs/protobuf/google/protobuf/internal/well_known_types.pyi b/stubs/protobuf/google/protobuf/internal/well_known_types.pyi index 6cbaf24d24d8..446b107d50f0 100644 --- a/stubs/protobuf/google/protobuf/internal/well_known_types.pyi +++ b/stubs/protobuf/google/protobuf/internal/well_known_types.pyi @@ -1,10 +1,8 @@ from datetime import datetime, timedelta, tzinfo -from typing import Any as tAny, Iterable, Iterator, Mapping, Sequence +from typing import Any as tAny, Iterable, Iterator, Mapping, Sequence, TypeAlias from google.protobuf import struct_pb2 -_StructValue: TypeAlias = struct_pb2.Struct | struct_pb2.ListValue | str | float | bool | None -_StructValueArg: TypeAlias = _StructValue | Mapping[_StructValueArg] | Sequence[_StructValueArg] class Any: type_url: tAny = ... @@ -68,10 +66,15 @@ class _FieldMaskTree: def AddLeafNodes(self, prefix: tAny, node: tAny) -> None: ... def MergeMessage(self, source: tAny, destination: tAny, replace_message: tAny, replace_repeated: tAny) -> None: ... + +_StructValue: TypeAlias = struct_pb2.Struct | struct_pb2.ListValue | str | float | bool | None +_StructValueArg: TypeAlias = _StructValue | Mapping[_StructValueArg] | Sequence[_StructValueArg] + + class Struct: def __getitem__(self, key: str) -> _StructValue: ... def __contains__(self, item: _StructValueArg) -> bool: ... - def __setitem__(self, key: str, value: _JsonImmutableValue) -> None: ... + def __setitem__(self, key: str, value: _StructValueArg) -> None: ... def __delitem__(self, key: str) -> None: ... def __len__(self) -> int: ... def __iter__(self) -> Iterator[str]: ... From 5b60ba0cddf05da0c4ed96b6d4780c607257bab9 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 2 Dec 2022 20:50:01 +0000 Subject: [PATCH 04/24] [pre-commit.ci] auto fixes from pre-commit.com hooks --- stubs/protobuf/google/protobuf/internal/well_known_types.pyi | 3 --- 1 file changed, 3 deletions(-) diff --git a/stubs/protobuf/google/protobuf/internal/well_known_types.pyi b/stubs/protobuf/google/protobuf/internal/well_known_types.pyi index 446b107d50f0..f49c2bc68a5e 100644 --- a/stubs/protobuf/google/protobuf/internal/well_known_types.pyi +++ b/stubs/protobuf/google/protobuf/internal/well_known_types.pyi @@ -3,7 +3,6 @@ from typing import Any as tAny, Iterable, Iterator, Mapping, Sequence, TypeAlias from google.protobuf import struct_pb2 - class Any: type_url: tAny = ... value: tAny = ... @@ -66,11 +65,9 @@ class _FieldMaskTree: def AddLeafNodes(self, prefix: tAny, node: tAny) -> None: ... def MergeMessage(self, source: tAny, destination: tAny, replace_message: tAny, replace_repeated: tAny) -> None: ... - _StructValue: TypeAlias = struct_pb2.Struct | struct_pb2.ListValue | str | float | bool | None _StructValueArg: TypeAlias = _StructValue | Mapping[_StructValueArg] | Sequence[_StructValueArg] - class Struct: def __getitem__(self, key: str) -> _StructValue: ... def __contains__(self, item: _StructValueArg) -> bool: ... From f48ab6d6ef5ee74c28bd07ef684da27138ca61a8 Mon Sep 17 00:00:00 2001 From: Ilya Konstantinov Date: Fri, 2 Dec 2022 15:53:32 -0500 Subject: [PATCH 05/24] add test --- stubs/protobuf/@tests/struct.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 stubs/protobuf/@tests/struct.py diff --git a/stubs/protobuf/@tests/struct.py b/stubs/protobuf/@tests/struct.py new file mode 100644 index 000000000000..323b8fb11853 --- /dev/null +++ b/stubs/protobuf/@tests/struct.py @@ -0,0 +1,15 @@ +from google.protobuf.struct_pb2 import ListValue, Value, Struct + +list_value = ListValue() + +lst = list(list_value) # ensure ListValue's __len__ + __getitem__ make it iterable + +list_value[0] = 42.42 +list_value[0] = '42' +list_value[0] = None +list_value[0] = True +list_value[0] = [42.42, '42', None, True, [42.42, '42', None, True], {"42": 42}] +list_value[0] = ListValue() +list_value[0] = Struct() + +list_element = list_value[0] From dc522b41c4c0ea90848dd8131226db12fd7578d6 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 2 Dec 2022 20:54:58 +0000 Subject: [PATCH 06/24] [pre-commit.ci] auto fixes from pre-commit.com hooks --- stubs/protobuf/@tests/struct.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/stubs/protobuf/@tests/struct.py b/stubs/protobuf/@tests/struct.py index 323b8fb11853..a8f6e940c37c 100644 --- a/stubs/protobuf/@tests/struct.py +++ b/stubs/protobuf/@tests/struct.py @@ -1,14 +1,14 @@ -from google.protobuf.struct_pb2 import ListValue, Value, Struct +from google.protobuf.struct_pb2 import ListValue, Struct list_value = ListValue() lst = list(list_value) # ensure ListValue's __len__ + __getitem__ make it iterable list_value[0] = 42.42 -list_value[0] = '42' +list_value[0] = "42" list_value[0] = None list_value[0] = True -list_value[0] = [42.42, '42', None, True, [42.42, '42', None, True], {"42": 42}] +list_value[0] = [42.42, "42", None, True, [42.42, "42", None, True], {"42": 42}] list_value[0] = ListValue() list_value[0] = Struct() From 4dc78138e89aa2a9f350192eebd078f39d05b4a6 Mon Sep 17 00:00:00 2001 From: Ilya Konstantinov Date: Fri, 2 Dec 2022 15:59:11 -0500 Subject: [PATCH 07/24] oops --- stubs/protobuf/google/protobuf/internal/well_known_types.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stubs/protobuf/google/protobuf/internal/well_known_types.pyi b/stubs/protobuf/google/protobuf/internal/well_known_types.pyi index f49c2bc68a5e..4bfa5aaf1ca9 100644 --- a/stubs/protobuf/google/protobuf/internal/well_known_types.pyi +++ b/stubs/protobuf/google/protobuf/internal/well_known_types.pyi @@ -66,7 +66,7 @@ class _FieldMaskTree: def MergeMessage(self, source: tAny, destination: tAny, replace_message: tAny, replace_repeated: tAny) -> None: ... _StructValue: TypeAlias = struct_pb2.Struct | struct_pb2.ListValue | str | float | bool | None -_StructValueArg: TypeAlias = _StructValue | Mapping[_StructValueArg] | Sequence[_StructValueArg] +_StructValueArg: TypeAlias = _StructValue | Mapping[str, _StructValueArg] | Sequence[_StructValueArg] class Struct: def __getitem__(self, key: str) -> _StructValue: ... From 89fa8debdec48c63bf2a50ec918ca902cf0aecce Mon Sep 17 00:00:00 2001 From: Ilya Konstantinov Date: Fri, 2 Dec 2022 16:10:23 -0500 Subject: [PATCH 08/24] make flake8 happy --- stubs/protobuf/google/protobuf/internal/well_known_types.pyi | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/stubs/protobuf/google/protobuf/internal/well_known_types.pyi b/stubs/protobuf/google/protobuf/internal/well_known_types.pyi index 4bfa5aaf1ca9..d787a3958cad 100644 --- a/stubs/protobuf/google/protobuf/internal/well_known_types.pyi +++ b/stubs/protobuf/google/protobuf/internal/well_known_types.pyi @@ -1,5 +1,6 @@ +from collections.abc import Iterable, Iterator, Mapping, Sequence from datetime import datetime, timedelta, tzinfo -from typing import Any as tAny, Iterable, Iterator, Mapping, Sequence, TypeAlias +from typing import Any as tAny, TypeAlias from google.protobuf import struct_pb2 From 9f5b2525f2b4e1f06607827ea34ce66e1a84fe73 Mon Sep 17 00:00:00 2001 From: Ilya Konstantinov Date: Fri, 2 Dec 2022 16:12:16 -0500 Subject: [PATCH 09/24] typing_extensions --- stubs/protobuf/google/protobuf/internal/well_known_types.pyi | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/stubs/protobuf/google/protobuf/internal/well_known_types.pyi b/stubs/protobuf/google/protobuf/internal/well_known_types.pyi index d787a3958cad..1fc98827cb3f 100644 --- a/stubs/protobuf/google/protobuf/internal/well_known_types.pyi +++ b/stubs/protobuf/google/protobuf/internal/well_known_types.pyi @@ -1,8 +1,9 @@ from collections.abc import Iterable, Iterator, Mapping, Sequence from datetime import datetime, timedelta, tzinfo -from typing import Any as tAny, TypeAlias +from typing import Any as tAny from google.protobuf import struct_pb2 +from typing_extensions import TypeAlias class Any: type_url: tAny = ... From f4a0efc945cf0bd7cbef063cdc708a94016c44f3 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 2 Dec 2022 21:13:29 +0000 Subject: [PATCH 10/24] [pre-commit.ci] auto fixes from pre-commit.com hooks --- stubs/protobuf/google/protobuf/internal/well_known_types.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stubs/protobuf/google/protobuf/internal/well_known_types.pyi b/stubs/protobuf/google/protobuf/internal/well_known_types.pyi index 1fc98827cb3f..b55d2d84bc8e 100644 --- a/stubs/protobuf/google/protobuf/internal/well_known_types.pyi +++ b/stubs/protobuf/google/protobuf/internal/well_known_types.pyi @@ -1,9 +1,9 @@ from collections.abc import Iterable, Iterator, Mapping, Sequence from datetime import datetime, timedelta, tzinfo from typing import Any as tAny +from typing_extensions import TypeAlias from google.protobuf import struct_pb2 -from typing_extensions import TypeAlias class Any: type_url: tAny = ... From 467cd5412a969cefc3b681605f3d2aadd30df997 Mon Sep 17 00:00:00 2001 From: Ilya Konstantinov Date: Fri, 2 Dec 2022 16:33:21 -0500 Subject: [PATCH 11/24] move test, add fake __iter__ --- stubs/protobuf/@tests/{struct.py => test_cases/check_struct.py} | 0 stubs/protobuf/google/protobuf/internal/well_known_types.pyi | 2 ++ 2 files changed, 2 insertions(+) rename stubs/protobuf/@tests/{struct.py => test_cases/check_struct.py} (100%) diff --git a/stubs/protobuf/@tests/struct.py b/stubs/protobuf/@tests/test_cases/check_struct.py similarity index 100% rename from stubs/protobuf/@tests/struct.py rename to stubs/protobuf/@tests/test_cases/check_struct.py diff --git a/stubs/protobuf/google/protobuf/internal/well_known_types.pyi b/stubs/protobuf/google/protobuf/internal/well_known_types.pyi index b55d2d84bc8e..7e5cf6f2e416 100644 --- a/stubs/protobuf/google/protobuf/internal/well_known_types.pyi +++ b/stubs/protobuf/google/protobuf/internal/well_known_types.pyi @@ -91,6 +91,8 @@ class ListValue: def __getitem__(self, index: int) -> _StructValue: ... def __setitem__(self, index: int, value: _StructValueArg) -> None: ... def __delitem__(self, key: int) -> None: ... + # runtime does not have __iter__ (yet...): this is a hack for https://github.com/python/typeshed/issues/7813 + def __iter__() -> Iterator[_StructValue]: ... def items(self) -> Iterator[_StructValue]: ... def add_struct(self) -> struct_pb2.Struct: ... def add_list(self) -> struct_pb2.ListValue: ... From 22bce744402dfd22ee9e6c3d55acd1afa98fe233 Mon Sep 17 00:00:00 2001 From: Ilya Konstantinov Date: Fri, 2 Dec 2022 16:35:27 -0500 Subject: [PATCH 12/24] from __future__ import annotations --- stubs/protobuf/@tests/test_cases/check_struct.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/stubs/protobuf/@tests/test_cases/check_struct.py b/stubs/protobuf/@tests/test_cases/check_struct.py index a8f6e940c37c..435107ef87e9 100644 --- a/stubs/protobuf/@tests/test_cases/check_struct.py +++ b/stubs/protobuf/@tests/test_cases/check_struct.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from google.protobuf.struct_pb2 import ListValue, Struct list_value = ListValue() From f22fbe5ed4d97fe24d03a587bd5ca988f3c52278 Mon Sep 17 00:00:00 2001 From: Ilya Konstantinov Date: Fri, 2 Dec 2022 16:39:43 -0500 Subject: [PATCH 13/24] # pyright: reportUnnecessaryTypeIgnoreComment=true --- stubs/protobuf/@tests/test_cases/check_struct.py | 1 + 1 file changed, 1 insertion(+) diff --git a/stubs/protobuf/@tests/test_cases/check_struct.py b/stubs/protobuf/@tests/test_cases/check_struct.py index 435107ef87e9..f15fc9b3615a 100644 --- a/stubs/protobuf/@tests/test_cases/check_struct.py +++ b/stubs/protobuf/@tests/test_cases/check_struct.py @@ -1,3 +1,4 @@ +# pyright: reportUnnecessaryTypeIgnoreComment=true from __future__ import annotations from google.protobuf.struct_pb2 import ListValue, Struct From a51922617ec03a49a8472c333433a0cb3995b1d8 Mon Sep 17 00:00:00 2001 From: Ilya Konstantinov Date: Fri, 2 Dec 2022 16:40:17 -0500 Subject: [PATCH 14/24] oops --- stubs/protobuf/google/protobuf/internal/well_known_types.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stubs/protobuf/google/protobuf/internal/well_known_types.pyi b/stubs/protobuf/google/protobuf/internal/well_known_types.pyi index 7e5cf6f2e416..683525470369 100644 --- a/stubs/protobuf/google/protobuf/internal/well_known_types.pyi +++ b/stubs/protobuf/google/protobuf/internal/well_known_types.pyi @@ -92,7 +92,7 @@ class ListValue: def __setitem__(self, index: int, value: _StructValueArg) -> None: ... def __delitem__(self, key: int) -> None: ... # runtime does not have __iter__ (yet...): this is a hack for https://github.com/python/typeshed/issues/7813 - def __iter__() -> Iterator[_StructValue]: ... + def __iter__(self) -> Iterator[_StructValue]: ... def items(self) -> Iterator[_StructValue]: ... def add_struct(self) -> struct_pb2.Struct: ... def add_list(self) -> struct_pb2.ListValue: ... From c54c518be469dbdd33b8ee113f27626836783b2e Mon Sep 17 00:00:00 2001 From: Ilya Konstantinov Date: Fri, 2 Dec 2022 16:54:16 -0500 Subject: [PATCH 15/24] stubtest_allowlist --- stubs/protobuf/@tests/stubtest_allowlist.txt | 3 +++ stubs/protobuf/google/protobuf/internal/well_known_types.pyi | 1 - 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/stubs/protobuf/@tests/stubtest_allowlist.txt b/stubs/protobuf/@tests/stubtest_allowlist.txt index 21264e021de3..00cbe18a4119 100644 --- a/stubs/protobuf/@tests/stubtest_allowlist.txt +++ b/stubs/protobuf/@tests/stubtest_allowlist.txt @@ -36,3 +36,6 @@ google.protobuf.internal.containers.BaseContainer.__hash__ # Metaclass differs: google.protobuf.descriptor.OneofDescriptor + +# Runtime does not have __iter__ (yet...): hack in spirit of https://github.com/python/typeshed/issues/7813 +google.protobuf.internal.well_known_types.ListValue.__getitem__ diff --git a/stubs/protobuf/google/protobuf/internal/well_known_types.pyi b/stubs/protobuf/google/protobuf/internal/well_known_types.pyi index 683525470369..944fef6d1d6a 100644 --- a/stubs/protobuf/google/protobuf/internal/well_known_types.pyi +++ b/stubs/protobuf/google/protobuf/internal/well_known_types.pyi @@ -91,7 +91,6 @@ class ListValue: def __getitem__(self, index: int) -> _StructValue: ... def __setitem__(self, index: int, value: _StructValueArg) -> None: ... def __delitem__(self, key: int) -> None: ... - # runtime does not have __iter__ (yet...): this is a hack for https://github.com/python/typeshed/issues/7813 def __iter__(self) -> Iterator[_StructValue]: ... def items(self) -> Iterator[_StructValue]: ... def add_struct(self) -> struct_pb2.Struct: ... From 2c0127899eeb1ced5c96268880d794adaf617904 Mon Sep 17 00:00:00 2001 From: Ilya Konstantinov Date: Fri, 2 Dec 2022 16:56:31 -0500 Subject: [PATCH 16/24] lol I'm tired --- stubs/protobuf/@tests/stubtest_allowlist.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stubs/protobuf/@tests/stubtest_allowlist.txt b/stubs/protobuf/@tests/stubtest_allowlist.txt index 00cbe18a4119..9b135c70d20b 100644 --- a/stubs/protobuf/@tests/stubtest_allowlist.txt +++ b/stubs/protobuf/@tests/stubtest_allowlist.txt @@ -38,4 +38,4 @@ google.protobuf.internal.containers.BaseContainer.__hash__ google.protobuf.descriptor.OneofDescriptor # Runtime does not have __iter__ (yet...): hack in spirit of https://github.com/python/typeshed/issues/7813 -google.protobuf.internal.well_known_types.ListValue.__getitem__ +google.protobuf.internal.well_known_types.ListValue.__iter__ From 2bf61670fe9155442a7b4d29d6422adbfc394fe1 Mon Sep 17 00:00:00 2001 From: Ilya Konstantinov Date: Fri, 2 Dec 2022 17:15:18 -0500 Subject: [PATCH 17/24] add well_known_types to pytype exclude list :( --- tests/pytype_exclude_list.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/pytype_exclude_list.txt b/tests/pytype_exclude_list.txt index fd47c9ec0dc9..5a857e22c161 100644 --- a/tests/pytype_exclude_list.txt +++ b/tests/pytype_exclude_list.txt @@ -27,6 +27,7 @@ stubs/protobuf/google/protobuf/internal/decoder.pyi stubs/protobuf/google/protobuf/internal/encoder.pyi stubs/protobuf/google/protobuf/internal/enum_type_wrapper.pyi stubs/protobuf/google/protobuf/internal/extension_dict.pyi +stubs/protobuf/google/protobuf/internal/well_known_types.pyi stubs/protobuf/google/protobuf/json_format.pyi stubs/protobuf/google/protobuf/message.pyi stubs/protobuf/google/protobuf/message_factory.pyi From 93c95782add935c3a0fb0583caa355cd1bfb737d Mon Sep 17 00:00:00 2001 From: Ilya Konstantinov Date: Mon, 5 Dec 2022 13:03:41 -0500 Subject: [PATCH 18/24] fix out-of-date comment on `list(list_value)` Co-authored-by: Alex Waygood --- stubs/protobuf/@tests/test_cases/check_struct.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stubs/protobuf/@tests/test_cases/check_struct.py b/stubs/protobuf/@tests/test_cases/check_struct.py index f15fc9b3615a..87bb72264fb5 100644 --- a/stubs/protobuf/@tests/test_cases/check_struct.py +++ b/stubs/protobuf/@tests/test_cases/check_struct.py @@ -5,7 +5,7 @@ list_value = ListValue() -lst = list(list_value) # ensure ListValue's __len__ + __getitem__ make it iterable +lst = list(list_value) # Ensure type checkers recognise that the class is iterable (doesn't have an `__iter__` method at runtime) list_value[0] = 42.42 list_value[0] = "42" From b7710a1d0b2ada51efe6514a399a44416a255879 Mon Sep 17 00:00:00 2001 From: Ilya Konstantinov Date: Mon, 5 Dec 2022 13:04:02 -0500 Subject: [PATCH 19/24] Change __contains__ to accept object Co-authored-by: Alex Waygood --- stubs/protobuf/google/protobuf/internal/well_known_types.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stubs/protobuf/google/protobuf/internal/well_known_types.pyi b/stubs/protobuf/google/protobuf/internal/well_known_types.pyi index 944fef6d1d6a..8db5817ce298 100644 --- a/stubs/protobuf/google/protobuf/internal/well_known_types.pyi +++ b/stubs/protobuf/google/protobuf/internal/well_known_types.pyi @@ -72,7 +72,7 @@ _StructValueArg: TypeAlias = _StructValue | Mapping[str, _StructValueArg] | Sequ class Struct: def __getitem__(self, key: str) -> _StructValue: ... - def __contains__(self, item: _StructValueArg) -> bool: ... + def __contains__(self, item: object) -> bool: ... def __setitem__(self, key: str, value: _StructValueArg) -> None: ... def __delitem__(self, key: str) -> None: ... def __len__(self) -> int: ... From 83ffc90cbc94fd5b7e95b5a2a740aca2d17a73c2 Mon Sep 17 00:00:00 2001 From: Ilya Konstantinov Date: Mon, 5 Dec 2022 13:10:16 -0500 Subject: [PATCH 20/24] return KeyView and list --- .../google/protobuf/internal/well_known_types.pyi | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/stubs/protobuf/google/protobuf/internal/well_known_types.pyi b/stubs/protobuf/google/protobuf/internal/well_known_types.pyi index 8db5817ce298..85b9d4c17b8a 100644 --- a/stubs/protobuf/google/protobuf/internal/well_known_types.pyi +++ b/stubs/protobuf/google/protobuf/internal/well_known_types.pyi @@ -1,4 +1,4 @@ -from collections.abc import Iterable, Iterator, Mapping, Sequence +from collections.abc import Iterable, Iterator, KeyView, Mapping, Sequence from datetime import datetime, timedelta, tzinfo from typing import Any as tAny from typing_extensions import TypeAlias @@ -77,9 +77,9 @@ class Struct: def __delitem__(self, key: str) -> None: ... def __len__(self) -> int: ... def __iter__(self) -> Iterator[str]: ... - def keys(self) -> Iterator[str]: ... - def values(self) -> Iterator[_StructValue]: ... - def items(self) -> Iterator[tuple[str, _StructValue]]: ... + def keys(self) -> KeyView[str]: ... + def values(self) -> list[_StructValue]: ... + def items(self) -> list[tuple[str, _StructValue]]: ... def get_or_create_list(self, key: str) -> struct_pb2.ListValue: ... def get_or_create_struct(self, key: str) -> struct_pb2.Struct: ... def update(self, dictionary: Mapping[str, _StructValueArg]) -> None: ... From c02609eb697b48d36d707dd6621b0019b8825149 Mon Sep 17 00:00:00 2001 From: Ilya Konstantinov Date: Mon, 5 Dec 2022 13:15:16 -0500 Subject: [PATCH 21/24] Add comment to def __iter__ Co-authored-by: Alex Waygood --- stubs/protobuf/google/protobuf/internal/well_known_types.pyi | 1 + 1 file changed, 1 insertion(+) diff --git a/stubs/protobuf/google/protobuf/internal/well_known_types.pyi b/stubs/protobuf/google/protobuf/internal/well_known_types.pyi index 85b9d4c17b8a..f155305a5013 100644 --- a/stubs/protobuf/google/protobuf/internal/well_known_types.pyi +++ b/stubs/protobuf/google/protobuf/internal/well_known_types.pyi @@ -91,6 +91,7 @@ class ListValue: def __getitem__(self, index: int) -> _StructValue: ... def __setitem__(self, index: int, value: _StructValueArg) -> None: ... def __delitem__(self, key: int) -> None: ... + # Doesn't actually exist at runtime; needed so type checkers understand the class is iterable def __iter__(self) -> Iterator[_StructValue]: ... def items(self) -> Iterator[_StructValue]: ... def add_struct(self) -> struct_pb2.Struct: ... From 4cce6b90baaa9eb70a6b2d656ae090b82027c622 Mon Sep 17 00:00:00 2001 From: Ilya Konstantinov Date: Mon, 5 Dec 2022 13:17:46 -0500 Subject: [PATCH 22/24] KeyView -> KeysView --- stubs/protobuf/google/protobuf/internal/well_known_types.pyi | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stubs/protobuf/google/protobuf/internal/well_known_types.pyi b/stubs/protobuf/google/protobuf/internal/well_known_types.pyi index f155305a5013..386dfc8e8cc4 100644 --- a/stubs/protobuf/google/protobuf/internal/well_known_types.pyi +++ b/stubs/protobuf/google/protobuf/internal/well_known_types.pyi @@ -1,4 +1,4 @@ -from collections.abc import Iterable, Iterator, KeyView, Mapping, Sequence +from collections.abc import Iterable, Iterator, KeysView, Mapping, Sequence from datetime import datetime, timedelta, tzinfo from typing import Any as tAny from typing_extensions import TypeAlias @@ -77,7 +77,7 @@ class Struct: def __delitem__(self, key: str) -> None: ... def __len__(self) -> int: ... def __iter__(self) -> Iterator[str]: ... - def keys(self) -> KeyView[str]: ... + def keys(self) -> KeysView[str]: ... def values(self) -> list[_StructValue]: ... def items(self) -> list[tuple[str, _StructValue]]: ... def get_or_create_list(self, key: str) -> struct_pb2.ListValue: ... From f2a1cfd143eba6025b6afbfddc06b01647d82562 Mon Sep 17 00:00:00 2001 From: Ilya Konstantinov Date: Mon, 5 Dec 2022 13:26:01 -0500 Subject: [PATCH 23/24] update can accept a SupportsItems Co-authored-by: Alex Waygood --- stubs/protobuf/google/protobuf/internal/well_known_types.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stubs/protobuf/google/protobuf/internal/well_known_types.pyi b/stubs/protobuf/google/protobuf/internal/well_known_types.pyi index 386dfc8e8cc4..ec52c09696ff 100644 --- a/stubs/protobuf/google/protobuf/internal/well_known_types.pyi +++ b/stubs/protobuf/google/protobuf/internal/well_known_types.pyi @@ -82,7 +82,7 @@ class Struct: def items(self) -> list[tuple[str, _StructValue]]: ... def get_or_create_list(self, key: str) -> struct_pb2.ListValue: ... def get_or_create_struct(self, key: str) -> struct_pb2.Struct: ... - def update(self, dictionary: Mapping[str, _StructValueArg]) -> None: ... + def update(self, dictionary: SupportsItems[str, _StructValueArg]) -> None: ... class ListValue: def __len__(self) -> int: ... From f4aff4cab0707b02bffe4ce15a1dada1254be5c4 Mon Sep 17 00:00:00 2001 From: Ilya Konstantinov Date: Mon, 5 Dec 2022 13:34:28 -0500 Subject: [PATCH 24/24] import SupportsItems --- stubs/protobuf/google/protobuf/internal/well_known_types.pyi | 1 + 1 file changed, 1 insertion(+) diff --git a/stubs/protobuf/google/protobuf/internal/well_known_types.pyi b/stubs/protobuf/google/protobuf/internal/well_known_types.pyi index ec52c09696ff..8c1592952c73 100644 --- a/stubs/protobuf/google/protobuf/internal/well_known_types.pyi +++ b/stubs/protobuf/google/protobuf/internal/well_known_types.pyi @@ -1,3 +1,4 @@ +from _typeshed import SupportsItems from collections.abc import Iterable, Iterator, KeysView, Mapping, Sequence from datetime import datetime, timedelta, tzinfo from typing import Any as tAny