From c5cf009c571cf9ad1f67f8b3a7ff0a8319ceb0af Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Sat, 4 Apr 2020 01:37:45 +0800 Subject: [PATCH 1/7] Use "mypy-extensions==0.4.3" --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 35b1a2c8a3..b42618af28 100644 --- a/setup.py +++ b/setup.py @@ -1040,7 +1040,7 @@ def run(self): python_requires=">=3.8, <4", extras_require={ "test": ["pytest>=4.4", "pytest-cov", "pytest-xdist"], - "lint": ["flake8==3.7.7", "mypy==0.812", "pylint==2.12.2"], + "lint": ["flake8==3.7.7", "mypy==0.812", "pylint==2.12.2", "mypy-extensions==0.4.3"], "generator": ["python-snappy==0.5.4"], }, install_requires=[ From c245a13e2fa02f4493348e11355b16934a5532f8 Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Sat, 4 Apr 2020 01:37:59 +0800 Subject: [PATCH 2/7] Add mypy config file --- mypy.ini | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 mypy.ini diff --git a/mypy.ini b/mypy.ini new file mode 100644 index 0000000000..9c02106de3 --- /dev/null +++ b/mypy.ini @@ -0,0 +1,11 @@ +[mypy] + +disallow_incomplete_defs = True +disallow_untyped_defs = True +disallow_subclassing_any = True + +warn_unused_ignores = True +warn_unused_configs = True +warn_redundant_casts = True + +ignore_missing_imports = True From 5a9b5ee8327c60061ee9b487c95e5ece0933c78f Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Sat, 4 Apr 2020 01:38:20 +0800 Subject: [PATCH 3/7] Add mypy stubs for third party packages --- mypy_stubs/eth2spec/utils/ssz/__init__.pyi | 0 mypy_stubs/eth2spec/utils/ssz/ssz_impl.pyi | 5 + mypy_stubs/eth2spec/utils/ssz/ssz_typing.pyi | 5 + mypy_stubs/remerkleable/__init__.pyi | 4 + mypy_stubs/remerkleable/basic.pyi | 56 +++++++ mypy_stubs/remerkleable/bitfields.pyi | 83 ++++++++++ mypy_stubs/remerkleable/byte_arrays.pyi | 76 +++++++++ mypy_stubs/remerkleable/complex.pyi | 155 +++++++++++++++++++ mypy_stubs/remerkleable/core.pyi | 114 ++++++++++++++ mypy_stubs/remerkleable/history.pyi | 7 + mypy_stubs/remerkleable/subtree.pyi | 13 ++ mypy_stubs/remerkleable/tree.pyi | 75 +++++++++ mypy_stubs/remerkleable/virtual.pyi | 16 ++ 13 files changed, 609 insertions(+) create mode 100644 mypy_stubs/eth2spec/utils/ssz/__init__.pyi create mode 100644 mypy_stubs/eth2spec/utils/ssz/ssz_impl.pyi create mode 100644 mypy_stubs/eth2spec/utils/ssz/ssz_typing.pyi create mode 100644 mypy_stubs/remerkleable/__init__.pyi create mode 100644 mypy_stubs/remerkleable/basic.pyi create mode 100644 mypy_stubs/remerkleable/bitfields.pyi create mode 100644 mypy_stubs/remerkleable/byte_arrays.pyi create mode 100644 mypy_stubs/remerkleable/complex.pyi create mode 100644 mypy_stubs/remerkleable/core.pyi create mode 100644 mypy_stubs/remerkleable/history.pyi create mode 100644 mypy_stubs/remerkleable/subtree.pyi create mode 100644 mypy_stubs/remerkleable/tree.pyi create mode 100644 mypy_stubs/remerkleable/virtual.pyi diff --git a/mypy_stubs/eth2spec/utils/ssz/__init__.pyi b/mypy_stubs/eth2spec/utils/ssz/__init__.pyi new file mode 100644 index 0000000000..e69de29bb2 diff --git a/mypy_stubs/eth2spec/utils/ssz/ssz_impl.pyi b/mypy_stubs/eth2spec/utils/ssz/ssz_impl.pyi new file mode 100644 index 0000000000..430fe735b2 --- /dev/null +++ b/mypy_stubs/eth2spec/utils/ssz/ssz_impl.pyi @@ -0,0 +1,5 @@ +from remerkleable.byte_arrays import Bytes32 +from remerkleable.core import View as View + +def serialize(obj: View) -> bytes: ... +def hash_tree_root(obj: View) -> Bytes32: ... diff --git a/mypy_stubs/eth2spec/utils/ssz/ssz_typing.pyi b/mypy_stubs/eth2spec/utils/ssz/ssz_typing.pyi new file mode 100644 index 0000000000..9ed955c03e --- /dev/null +++ b/mypy_stubs/eth2spec/utils/ssz/ssz_typing.pyi @@ -0,0 +1,5 @@ +from remerkleable.basic import bit as bit, boolean as boolean, byte as byte, uint as uint, uint128 as uint128, uint16 as uint16, uint256 as uint256, uint32 as uint32, uint64 as uint64, uint8 as uint8 +from remerkleable.bitfields import Bitlist as Bitlist, Bitvector as Bitvector +from remerkleable.byte_arrays import ByteList as ByteList, ByteVector as ByteVector, Bytes1 as Bytes1, Bytes32 as Bytes32, Bytes4 as Bytes4, Bytes48 as Bytes48, Bytes8 as Bytes8, Bytes96 as Bytes96 +from remerkleable.complex import Container as Container, List as List, Vector as Vector +from remerkleable.core import BasicView as BasicView, TypeDef as TypeDef, View as View diff --git a/mypy_stubs/remerkleable/__init__.pyi b/mypy_stubs/remerkleable/__init__.pyi new file mode 100644 index 0000000000..a32cfa53c1 --- /dev/null +++ b/mypy_stubs/remerkleable/__init__.pyi @@ -0,0 +1,4 @@ +# Stubs for remerkleable (Python 3) +# +# NOTE: This dynamically typed stub was automatically generated by stubgen. + diff --git a/mypy_stubs/remerkleable/basic.pyi b/mypy_stubs/remerkleable/basic.pyi new file mode 100644 index 0000000000..5b19aa4931 --- /dev/null +++ b/mypy_stubs/remerkleable/basic.pyi @@ -0,0 +1,56 @@ +from remerkleable.core import BasicView as BasicView, View as View +from typing import Any, Type, TypeVar + +V = TypeVar('V', bound=View) + +class boolean(int, BasicView): + def encode_bytes(self) -> bytes: ... + def __new__(cls: Any, value: int) -> Any: ... + def __bool__(self): ... + @classmethod + def coerce_view(cls: Type[V], v: Any) -> V: ... + @classmethod + def type_byte_length(cls: Any) -> int: ... + @classmethod + def decode_bytes(cls: Type[V], bytez: bytes) -> V: ... + @classmethod + def type_repr(cls: Any) -> str: ... + +class uint(int, BasicView): + def __new__(cls: Any, value: int) -> Any: ... + def __add__(self, other: Any): ... + def __sub__(self, other: Any): ... + @classmethod + def coerce_view(cls: Type[V], v: Any) -> V: ... + @classmethod + def decode_bytes(cls: Type[V], bytez: bytes) -> V: ... + def encode_bytes(self) -> bytes: ... + @classmethod + def type_repr(cls: Any) -> str: ... + +class uint8(uint): + @classmethod + def type_byte_length(cls: Any) -> int: ... + +class uint16(uint): + @classmethod + def type_byte_length(cls: Any) -> int: ... + +class uint32(uint): + @classmethod + def type_byte_length(cls: Any) -> int: ... + +class uint64(uint): + @classmethod + def type_byte_length(cls: Any) -> int: ... + +class uint128(uint): + @classmethod + def type_byte_length(cls: Any) -> int: ... + +class uint256(uint): + @classmethod + def type_byte_length(cls: Any) -> int: ... + +class bit(boolean): ... +class byte(uint8): ... diff --git a/mypy_stubs/remerkleable/bitfields.pyi b/mypy_stubs/remerkleable/bitfields.pyi new file mode 100644 index 0000000000..7849db7159 --- /dev/null +++ b/mypy_stubs/remerkleable/bitfields.pyi @@ -0,0 +1,83 @@ +from collections.abc import Sequence as ColSequence +from remerkleable.basic import boolean as boolean, uint256 as uint256 +from remerkleable.core import BackedView as BackedView, FixedByteLengthViewHelper as FixedByteLengthViewHelper, View as View, pack_bits_to_chunks as pack_bits_to_chunks +from remerkleable.tree import Gindex as Gindex, Link as Link, NavigationError as NavigationError, Node as Node, PairNode as PairNode, Root as Root, RootNode as RootNode, get_depth as get_depth, subtree_fill_to_contents as subtree_fill_to_contents, subtree_fill_to_length as subtree_fill_to_length, to_gindex as to_gindex, zero_node as zero_node +from typing import Any, BinaryIO, Type, TypeVar + +V = TypeVar('V', bound=View) + +class BitsView(BackedView, ColSequence): + @classmethod + def coerce_view(cls: Type[V], v: Any) -> V: ... + @classmethod + def tree_depth(cls: Any) -> int: ... + def length(self) -> int: ... + def get(self, i: int) -> boolean: ... + def set(self, i: int, v: boolean) -> None: ... + def __len__(self): ... + def __iter__(self) -> Any: ... + def __getitem__(self, k: Any): ... + def __setitem__(self, k: Any, v: Any) -> None: ... + def encode_bytes(self) -> bytes: ... + @classmethod + def decode_bytes(cls: Type[V], bytez: bytes) -> V: ... + def navigate_view(self, key: Any) -> View: ... + +class Bitlist(BitsView): + def __new__(cls, *args: Any, **kwargs: Any): ... + def __class_getitem__(cls: Any, limit: Any) -> Type[Bitlist]: ... + @classmethod + def contents_depth(cls: Any) -> int: ... + @classmethod + def tree_depth(cls: Any) -> int: ... + @classmethod + def limit(cls: Any) -> int: ... + @classmethod + def default_node(cls: Any) -> Node: ... + @classmethod + def type_repr(cls: Any) -> str: ... + @classmethod + def is_fixed_byte_length(cls: Any) -> bool: ... + @classmethod + def min_byte_length(cls: Any) -> int: ... + @classmethod + def max_byte_length(cls: Any) -> int: ... + def length(self) -> int: ... + def append(self, v: boolean) -> Any: ... + def pop(self) -> None: ... + def get(self, i: int) -> boolean: ... + def set(self, i: int, v: boolean) -> None: ... + def value_byte_length(self) -> int: ... + @classmethod + def decode_bytes(cls: Type[V], bytez: bytes) -> V: ... + @classmethod + def deserialize(cls: Type[V], stream: BinaryIO, scope: int) -> V: ... + def serialize(self, stream: BinaryIO) -> int: ... + @classmethod + def navigate_type(cls: Any, key: Any) -> Type[View]: ... + @classmethod + def key_to_static_gindex(cls: Any, key: Any) -> Gindex: ... + +class Bitvector(BitsView, FixedByteLengthViewHelper): + def __new__(cls, *args: Any, **kwargs: Any): ... + def __class_getitem__(cls: Any, length: Any) -> Type[Bitvector]: ... + @classmethod + def tree_depth(cls: Any) -> int: ... + @classmethod + def vector_length(cls: Any) -> int: ... + @classmethod + def default_node(cls: Any) -> Node: ... + @classmethod + def type_repr(cls: Any) -> str: ... + @classmethod + def type_byte_length(cls: Any) -> int: ... + def length(self) -> int: ... + def get(self, i: int) -> boolean: ... + def set(self, i: int, v: boolean) -> None: ... + @classmethod + def deserialize(cls: Type[V], stream: BinaryIO, scope: int) -> V: ... + def serialize(self, stream: BinaryIO) -> int: ... + @classmethod + def navigate_type(cls: Any, key: Any) -> Type[View]: ... + @classmethod + def key_to_static_gindex(cls: Any, key: Any) -> Gindex: ... diff --git a/mypy_stubs/remerkleable/byte_arrays.pyi b/mypy_stubs/remerkleable/byte_arrays.pyi new file mode 100644 index 0000000000..9f75376fae --- /dev/null +++ b/mypy_stubs/remerkleable/byte_arrays.pyi @@ -0,0 +1,76 @@ +from remerkleable.basic import byte as byte, uint256 as uint256 +from remerkleable.core import FixedByteLengthViewHelper as FixedByteLengthViewHelper, View as View, ViewHook as ViewHook, pack_bytes_to_chunks as pack_bytes_to_chunks, zero_node as zero_node +from remerkleable.tree import Gindex as Gindex, Node as Node, PairNode as PairNode, Root as Root, RootNode as RootNode, get_depth as get_depth, subtree_fill_to_contents as subtree_fill_to_contents, subtree_fill_to_length as subtree_fill_to_length, to_gindex as to_gindex +from typing import Any, BinaryIO, Optional, Type, TypeVar + +V = TypeVar('V', bound=View) + +class RawBytesView(bytes, View): + def __new__(cls, *args: Any, **kwargs: Any): ... + @classmethod + def default_bytes(cls: Any) -> bytes: ... + @classmethod + def coerce_view(cls: Type[V], v: Any) -> V: ... + @classmethod + def tree_depth(cls: Any) -> int: ... + def set_backing(self, value: Any) -> None: ... + @classmethod + def decode_bytes(cls: Type[V], bytez: bytes) -> V: ... + def encode_bytes(self) -> bytes: ... + def navigate_view(self, key: Any) -> View: ... + +class ByteVector(RawBytesView, FixedByteLengthViewHelper, View): + def __new__(cls, *args: Any, **kwargs: Any): ... + def __class_getitem__(cls: Any, length: Any) -> Type[ByteVector]: ... + @classmethod + def vector_length(cls): ... + @classmethod + def default_bytes(cls: Any) -> bytes: ... + @classmethod + def type_repr(cls: Any) -> str: ... + @classmethod + def view_from_backing(cls: Type[V], node: Node, hook: Optional[ViewHook[V]]=...) -> V: ... + def get_backing(self) -> Node: ... + @classmethod + def navigate_type(cls: Any, key: Any) -> Type[View]: ... + @classmethod + def key_to_static_gindex(cls: Any, key: Any) -> Gindex: ... + +Bytes1: Any +Bytes4: Any +Bytes8: Any +Bytes32: Any +Bytes48: Any +Bytes96: Any + +class ByteList(RawBytesView, FixedByteLengthViewHelper, View): + def __new__(cls, *args: Any, **kwargs: Any): ... + def __class_getitem__(cls: Any, limit: Any) -> Type[ByteList]: ... + @classmethod + def limit(cls: Any) -> int: ... + @classmethod + def default_bytes(cls: Any) -> bytes: ... + @classmethod + def type_repr(cls: Any) -> str: ... + @classmethod + def view_from_backing(cls: Type[V], node: Node, hook: Optional[ViewHook[V]]=...) -> V: ... + def get_backing(self) -> Node: ... + @classmethod + def contents_depth(cls: Any) -> int: ... + @classmethod + def tree_depth(cls: Any) -> int: ... + @classmethod + def default_node(cls: Any) -> Node: ... + @classmethod + def navigate_type(cls: Any, key: Any) -> Type[View]: ... + @classmethod + def key_to_static_gindex(cls: Any, key: Any) -> Gindex: ... + @classmethod + def is_fixed_byte_length(cls: Any) -> bool: ... + @classmethod + def min_byte_length(cls: Any) -> int: ... + @classmethod + def max_byte_length(cls: Any) -> int: ... + @classmethod + def deserialize(cls: Type[V], stream: BinaryIO, scope: int) -> V: ... + def value_byte_length(self) -> int: ... diff --git a/mypy_stubs/remerkleable/complex.pyi b/mypy_stubs/remerkleable/complex.pyi new file mode 100644 index 0000000000..0e0fc7f35e --- /dev/null +++ b/mypy_stubs/remerkleable/complex.pyi @@ -0,0 +1,155 @@ +from collections.abc import Sequence as ColSequence +from remerkleable.basic import uint256 as uint256, uint32 as uint32, uint8 as uint8 +from remerkleable.core import BasicTypeDef as BasicTypeDef, BasicView as BasicView, OFFSET_BYTE_LENGTH as OFFSET_BYTE_LENGTH, View as View, ViewHook as ViewHook +from remerkleable.subtree import SubtreeView as SubtreeView +from remerkleable.tree import Gindex as Gindex, NavigationError as NavigationError, Node as Node, PairNode as PairNode, get_depth as get_depth, subtree_fill_to_contents as subtree_fill_to_contents, subtree_fill_to_length as subtree_fill_to_length, to_gindex as to_gindex, zero_node as zero_node +from typing import Any, BinaryIO, Dict, List as PyList, NamedTuple, Optional, Type, TypeVar + +V = TypeVar('V', bound=View) + +def decode_offset(stream: BinaryIO) -> uint32: ... +def encode_offset(stream: BinaryIO, offset: int) -> Any: ... + +class ComplexView(SubtreeView): + def encode_bytes(self) -> bytes: ... + @classmethod + def decode_bytes(cls: Type[V], bytez: bytes) -> V: ... + +class MonoSubtreeView(ColSequence, ComplexView): + def length(self) -> int: ... + @classmethod + def coerce_view(cls: Type[V], v: Any) -> V: ... + @classmethod + def element_cls(cls: Any) -> Type[View]: ... + @classmethod + def item_elem_cls(cls: Any, i: int) -> Type[View]: ... + @classmethod + def to_chunk_length(cls: Any, elems_length: int) -> int: ... + @classmethod + def views_into_chunks(cls: Any, views: PyList[View]) -> PyList[Node]: ... + @classmethod + def is_valid_count(cls: Any, count: int) -> bool: ... + def __iter__(self) -> Any: ... + def readonly_iter(self): ... + @classmethod + def deserialize(cls: Type[V], stream: BinaryIO, scope: int) -> V: ... + def serialize(self, stream: BinaryIO) -> int: ... + @classmethod + def navigate_type(cls: Any, key: Any) -> Type[View]: ... + @classmethod + def key_to_static_gindex(cls: Any, key: Any) -> Gindex: ... + def navigate_view(self, key: Any) -> View: ... + def __len__(self): ... + def __add__(self, other: Any): ... + def __getitem__(self, k: Any): ... + def __setitem__(self, k: Any, v: Any) -> None: ... + +class List(MonoSubtreeView): + def __new__(cls: Any, *args: Any, backing: Optional[Node]=..., hook: Optional[ViewHook]=..., **kwargs: Any) -> Any: ... + def __class_getitem__(cls: Any, params: Any) -> Type[List]: ... + def length(self) -> int: ... + def value_byte_length(self) -> int: ... + def append(self, v: View) -> Any: ... + def pop(self) -> None: ... + def get(self, i: int) -> View: ... + def set(self, i: int, v: View) -> None: ... + @classmethod + def type_repr(cls: Any) -> str: ... + @classmethod + def is_packed(cls: Any) -> bool: ... + @classmethod + def contents_depth(cls: Any) -> int: ... + @classmethod + def tree_depth(cls: Any) -> int: ... + @classmethod + def item_elem_cls(cls: Any, i: int) -> Type[V]: ... + @classmethod + def limit(cls: Any) -> int: ... + @classmethod + def is_valid_count(cls: Any, count: int) -> bool: ... + @classmethod + def navigate_type(cls: Any, key: Any) -> Type[View]: ... + @classmethod + def key_to_static_gindex(cls: Any, key: Any) -> Gindex: ... + @classmethod + def default_node(cls: Any) -> Node: ... + @classmethod + def is_fixed_byte_length(cls: Any) -> bool: ... + @classmethod + def min_byte_length(cls: Any) -> int: ... + @classmethod + def max_byte_length(cls: Any) -> int: ... + +class Vector(MonoSubtreeView): + def __new__(cls: Any, *args: Any, backing: Optional[Node]=..., hook: Optional[ViewHook]=..., **kwargs: Any) -> Any: ... + def __class_getitem__(cls: Any, params: Any) -> Type[Vector]: ... + def get(self, i: int) -> View: ... + def set(self, i: int, v: View) -> None: ... + def length(self) -> int: ... + def value_byte_length(self) -> int: ... + @classmethod + def type_repr(cls: Any) -> str: ... + @classmethod + def vector_length(cls: Any) -> int: ... + @classmethod + def is_valid_count(cls: Any, count: int) -> bool: ... + @classmethod + def navigate_type(cls: Any, key: Any) -> Type[View]: ... + @classmethod + def key_to_static_gindex(cls: Any, key: Any) -> Gindex: ... + @classmethod + def default_node(cls: Any) -> Node: ... + @classmethod + def is_fixed_byte_length(cls: Any) -> bool: ... + @classmethod + def min_byte_length(cls: Any) -> int: ... + @classmethod + def max_byte_length(cls: Any) -> int: ... +Fields = Dict[str, Type[View]] + +class FieldOffset(NamedTuple): + key: str + typ: Type[View] + offset: int + +class _ContainerLike: + @classmethod + def fields(cls: Any) -> Fields: ... + +class Container(ComplexView): + def __new__(cls: Any, *args: Any, backing: Optional[Node]=..., hook: Optional[ViewHook]=..., **kwargs: Any) -> Any: ... + @classmethod + def coerce_view(cls: Type[V], v: Any) -> V: ... + @classmethod + def fields(cls: Any) -> Fields: ... + @classmethod + def is_fixed_byte_length(cls: Any) -> bool: ... + @classmethod + def type_byte_length(cls: Any) -> int: ... + @classmethod + def min_byte_length(cls: Any) -> int: ... + @classmethod + def max_byte_length(cls: Any) -> int: ... + @classmethod + def is_packed(cls: Any) -> bool: ... + @classmethod + def tree_depth(cls: Any) -> int: ... + @classmethod + def item_elem_cls(cls: Any, i: int) -> Type[View]: ... + @classmethod + def default_node(cls: Any) -> Node: ... + def value_byte_length(self) -> int: ... + def __getattr__(self, item: Any): ... + def __setattr__(self, key: Any, value: Any) -> None: ... + @classmethod + def type_repr(cls: Any) -> str: ... + @classmethod + def decode_bytes(cls: Type[V], bytez: bytes) -> V: ... + @classmethod + def deserialize(cls: Type[V], stream: BinaryIO, scope: int) -> V: ... + def serialize(self, stream: BinaryIO) -> int: ... + @classmethod + def key_to_static_gindex(cls: Any, key: Any) -> Gindex: ... + @classmethod + def navigate_type(cls: Any, key: Any) -> Type[View]: ... + def navigate_view(self, key: Any) -> View: ... diff --git a/mypy_stubs/remerkleable/core.pyi b/mypy_stubs/remerkleable/core.pyi new file mode 100644 index 0000000000..820ade4023 --- /dev/null +++ b/mypy_stubs/remerkleable/core.pyi @@ -0,0 +1,114 @@ +from remerkleable.tree import Gindex as Gindex, Node as Node, Root as Root, RootNode as RootNode, concat_gindices as concat_gindices, zero_node as zero_node +from typing import Any, BinaryIO, Callable, Iterable, List as PyList, Optional, Tuple, Type, TypeVar, _ProtocolMeta + +OFFSET_BYTE_LENGTH: int +V = TypeVar('V', bound='View') + +class TypeDefMeta(_ProtocolMeta): + def __truediv__(self, other: Any) -> Path: ... + +class TypeDef(metaclass=TypeDefMeta): + @classmethod + def coerce_view(cls: Type[V], v: Any) -> V: ... + @classmethod + def default(cls: Type[V], hook: Optional[ViewHook[V]]) -> V: ... + @classmethod + def default_node(cls: Any) -> Node: ... + @classmethod + def view_from_backing(cls: Type[V], node: Node, hook: Optional[ViewHook[V]]=...) -> V: ... + @classmethod + def is_fixed_byte_length(cls: Any) -> bool: ... + @classmethod + def type_byte_length(cls: Any) -> int: ... + @classmethod + def min_byte_length(cls: Any) -> int: ... + @classmethod + def max_byte_length(cls: Any) -> int: ... + @classmethod + def decode_bytes(cls: Type[V], bytez: bytes) -> V: ... + @classmethod + def deserialize(cls: Type[V], stream: BinaryIO, scope: int) -> V: ... + @classmethod + def type_repr(cls: Any) -> str: ... + @classmethod + def navigate_type(cls: Any, key: Any) -> Type[View]: ... + @classmethod + def key_to_static_gindex(cls: Any, key: Any) -> Gindex: ... + +class Path: + anchor: Type[View] + path: PyList[Tuple[Any, Type[View]]] + def __init__(self, anchor: Type[View], path: Optional[PyList[Tuple[Any, Type[View]]]]=...) -> Any: ... + @staticmethod + def from_raw_path(anchor: Type[View], path: PyList[Any]) -> Path: ... + def __truediv__(self, other: Any) -> Path: ... + def gindex(self, view: Optional[View]=...) -> Gindex: ... + def navigate_type(self) -> Type[View]: ... + def navigate_view(self, v: View) -> View: ... +HV = TypeVar('HV', bound='View') +ViewHook = Callable[[HV], None] + +class View(TypeDef): + @classmethod + def default(cls: Type[V], hook: Optional[ViewHook[V]]) -> V: ... + def get_backing(self) -> Node: ... + def set_backing(self, value: Any) -> None: ... + def copy(self) -> V: ... + @classmethod + def type_byte_length(cls: Any) -> int: ... + def value_byte_length(self) -> int: ... + def __bytes__(self): ... + def encode_bytes(self) -> bytes: ... + def serialize(self, stream: BinaryIO) -> int: ... + def navigate_view(self, key: Any) -> View: ... + def key_to_dynamic_gindex(self, key: Any) -> Gindex: ... + def hash_tree_root(self) -> Root: ... + def __eq__(self, other: Any) -> Any: ... + def __hash__(self) -> Any: ... + +class FixedByteLengthViewHelper(View): + @classmethod + def is_fixed_byte_length(cls: Any) -> bool: ... + @classmethod + def min_byte_length(cls: Any) -> int: ... + @classmethod + def max_byte_length(cls: Any) -> int: ... + @classmethod + def deserialize(cls: Type[V], stream: BinaryIO, scope: int) -> V: ... + def value_byte_length(self) -> int: ... + +class BackedView(View, TypeDef): + @classmethod + def view_from_backing(cls: Type[V], node: Node, hook: Optional[ViewHook[V]]=...) -> V: ... + def __new__(cls: Any, backing: Optional[Node]=..., hook: Optional[ViewHook[V]]=..., **kwargs: Any) -> Any: ... + def get_backing(self) -> Node: ... + def set_backing(self, value: Any) -> None: ... +BV = TypeVar('BV', bound='BasicView') + +class BasicTypeDef(TypeDef): + @classmethod + def basic_view_from_backing(cls: Type[BV], node: Node, i: int) -> BV: ... + @classmethod + def pack_views(cls: Type[BV], views: PyList[BV]) -> PyList[Node]: ... + +class BasicView(FixedByteLengthViewHelper, BasicTypeDef): + @classmethod + def default_node(cls: Any) -> Node: ... + @classmethod + def view_from_backing(cls: Type[BV], node: Node, hook: Optional[ViewHook[BV]]=...) -> V: ... + @classmethod + def basic_view_from_backing(cls: Type[BV], node: Node, i: int) -> BV: ... + @classmethod + def pack_views(cls: Type[BV], views: PyList[BV]) -> PyList[Node]: ... + def copy(self) -> V: ... + def backing_from_base(self, base: Node, i: int) -> Node: ... + def get_backing(self) -> Node: ... + def set_backing(self, value: Any) -> None: ... + +def grouper(items: Iterable, n: int, fillvalue: Any=...) -> Iterable[Tuple]: ... +def pack_ints_to_chunks(items: Iterable[int], items_per_chunk: int) -> PyList[Node]: ... +def bits_to_byte_int(byte: Tuple[bool, bool, bool, bool, bool, bool, bool, bool]) -> int: ... +def byte_int_to_byte(b: int) -> bytes: ... +def pack_bits_to_chunks(items: Iterable[bool]) -> PyList[Node]: ... +def pack_byte_ints_to_chunks(items: Iterable[int]) -> PyList[Node]: ... +def pack_bytes_to_chunks(bytez: bytes) -> PyList[Node]: ... diff --git a/mypy_stubs/remerkleable/history.pyi b/mypy_stubs/remerkleable/history.pyi new file mode 100644 index 0000000000..45ef15571d --- /dev/null +++ b/mypy_stubs/remerkleable/history.pyi @@ -0,0 +1,7 @@ +from remerkleable.tree import Gindex as Gindex, Node as Node, ROOT_GINDEX as ROOT_GINDEX, get_anchor_gindex as get_anchor_gindex +from typing import List as Iterable, Tuple, TypeVar + +K = TypeVar('K') +History = Iterable[Tuple[K, Node]] + +def get_target_history(history: History, target: Gindex) -> History: ... diff --git a/mypy_stubs/remerkleable/subtree.pyi b/mypy_stubs/remerkleable/subtree.pyi new file mode 100644 index 0000000000..44b7d3db46 --- /dev/null +++ b/mypy_stubs/remerkleable/subtree.pyi @@ -0,0 +1,13 @@ +from remerkleable.core import BackedView as BackedView, BasicTypeDef as BasicTypeDef, BasicView as BasicView, TypeDef as TypeDef, View as View +from remerkleable.tree import Link as Link, to_gindex as to_gindex +from typing import Any, Type + +class SubtreeView(BackedView, TypeDef): + @classmethod + def is_packed(cls: Any) -> bool: ... + @classmethod + def tree_depth(cls: Any) -> int: ... + @classmethod + def item_elem_cls(cls: Any, i: int) -> Type[View]: ... + def get(self, i: int) -> View: ... + def set(self, i: int, v: View) -> None: ... diff --git a/mypy_stubs/remerkleable/tree.pyi b/mypy_stubs/remerkleable/tree.pyi new file mode 100644 index 0000000000..f1db1360bf --- /dev/null +++ b/mypy_stubs/remerkleable/tree.pyi @@ -0,0 +1,75 @@ +from typing import Any, Iterable, Iterator, List, Tuple, TypeVar + +def get_depth(elem_count: int) -> int: ... + +Gindex: Any +ROOT_GINDEX: Any +LEFT_GINDEX: Any +RIGHT_GINDEX: Any + +def to_gindex(index: int, depth: int) -> Any: ... +def get_anchor_gindex(gindex: Gindex) -> Gindex: ... +def gindex_bit_iter(gindex: Gindex) -> Tuple[Iterator[bool], int]: ... +def concat_gindices(steps: Iterable[Gindex]) -> Gindex: ... + +Root: Any +MerkleFn: Any +ZERO_ROOT: Root + +def merkle_hash(left: Root, right: Root) -> Root: ... + +Link: Any +SummaryLink: Any + +class Node: + def get_left(self) -> Node: ... + def get_right(self) -> Node: ... + def getter(self, target: Gindex) -> Node: ... + def is_leaf(self) -> bool: ... + def rebind_left(self, v: Node) -> Node: ... + def rebind_right(self, v: Node) -> Node: ... + def setter(self, target: Gindex, expand: bool=...) -> Link: ... + def summarize_into(self, target: Gindex) -> SummaryLink: ... + @property + def root(self) -> Root: ... + def merkle_root(self) -> Root: ... + +zero_hashes: List[Root] + +def zero_node(depth: int) -> RootNode: ... +def identity(v: Node) -> Node: ... +def compose(inner: Link, outer: Link) -> Link: ... + +class NavigationError(RuntimeError): ... +V = TypeVar('V', bound=Node) + +class RebindableNode(Node): + def combine(self, left: Node, right: Node) -> Node: ... + def rebind_left(self, v: Node) -> Node: ... + def rebind_right(self, v: Node) -> Node: ... + def setter(self, target: Gindex, expand: bool=...) -> Link: ... + +class PairNode(RebindableNode, Node): + left: Node + right: Node + def __init__(self, left: Node, right: Node) -> Any: ... + def get_left(self) -> Node: ... + def get_right(self) -> Node: ... + def is_leaf(self) -> bool: ... + def merkle_root(self) -> Root: ... + +def subtree_fill_to_depth(bottom: Node, depth: int) -> Node: ... +def subtree_fill_to_length(bottom: Node, depth: int, length: int) -> Node: ... +def subtree_fill_to_contents(nodes: List[Node], depth: int) -> Node: ... + +class RootNode(Node): + def __init__(self, root: Root) -> Any: ... + def getter(self, target: Gindex) -> Node: ... + def is_leaf(self) -> bool: ... + def setter(self, target: Gindex, expand: bool=...) -> Link: ... + @property + def root(self) -> Root: ... + def merkle_root(self) -> Root: ... + +def leaf_iter(node: Node) -> Iterator[Node]: ... +def get_diff(a: Node, b: Node) -> Iterator[Tuple[Node, Node]]: ... diff --git a/mypy_stubs/remerkleable/virtual.pyi b/mypy_stubs/remerkleable/virtual.pyi new file mode 100644 index 0000000000..8d983248de --- /dev/null +++ b/mypy_stubs/remerkleable/virtual.pyi @@ -0,0 +1,16 @@ +from remerkleable.tree import NavigationError as NavigationError, Node as Node, RebindableNode as RebindableNode, Root as Root +from typing import Any + +class VirtualSource: + def get_left(self, key: Root) -> Node: ... + def get_right(self, key: Root) -> Node: ... + def is_leaf(self, key: Root) -> bool: ... + +class VirtualNode(RebindableNode, Node): + def __init__(self, root: Root, src: VirtualSource) -> Any: ... + def get_left(self) -> Node: ... + def get_right(self) -> Node: ... + def is_leaf(self) -> bool: ... + @property + def root(self) -> Root: ... + def merkle_root(self) -> Root: ... From 54ff9cd9d0fbb83686bff62695d293ba2ad7b6c3 Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Sat, 4 Apr 2020 01:38:54 +0800 Subject: [PATCH 4/7] Set MYPYPATH and use mypy config file --- Makefile | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Makefile b/Makefile index e3ff8e455c..e87dd14d1f 100644 --- a/Makefile +++ b/Makefile @@ -14,6 +14,9 @@ SOLIDITY_FILE_NAME = deposit_contract.json DEPOSIT_CONTRACT_TESTER_DIR = ${SOLIDITY_DEPOSIT_CONTRACT_DIR}/web3_tester CONFIGS_DIR = ./configs +CURRENT_DIR = ${CURDIR} +MYPY_STUBS_DIR = $(CURRENT_DIR)/mypy_stubs + # Collect a list of generator names GENERATORS = $(sort $(dir $(wildcard $(GENERATOR_DIR)/*/.))) # Map this list of generator paths to "gen_{generator name}" entries @@ -44,6 +47,8 @@ export DAPP_SRC:=$(SOLIDITY_DEPOSIT_CONTRACT_DIR) export DAPP_LIB:=$(SOLIDITY_DEPOSIT_CONTRACT_DIR)/lib export DAPP_JSON:=build/combined.json +MYPY_CONFIG_NAME = mypy.ini + .PHONY: clean partial_clean all test citest lint generate_tests pyspec install_test open_cov \ install_deposit_contract_tester test_deposit_contract install_deposit_contract_compiler \ compile_deposit_contract test_compile_deposit_contract check_toc \ @@ -136,6 +141,7 @@ codespell: lint: pyspec . venv/bin/activate; cd $(PY_SPEC_DIR); \ flake8 --config $(LINTER_CONFIG_FILE) ./eth2spec \ + && export MYPYPATH=$(MYPY_STUBS_DIR) \ && pylint --disable=all --enable unused-argument ./eth2spec/phase0 ./eth2spec/altair ./eth2spec/bellatrix \ && mypy --config-file $(LINTER_CONFIG_FILE) -p eth2spec.phase0 -p eth2spec.altair -p eth2spec.bellatrix -p eth2spec.capella From 5c9ad0df20fc9860aa4ead97d57f46435a91a1f2 Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Mon, 6 Apr 2020 17:43:19 +0800 Subject: [PATCH 5/7] mypy_stubs WIP --- mypy_stubs/__init__.py | 0 mypy_stubs/__init__.pyi | 4 ++++ mypy_stubs/remerkleable/basic.pyi | 8 ++++---- mypy_stubs/remerkleable/bitfields.pyi | 8 ++++---- mypy_stubs/remerkleable/byte_arrays.pyi | 8 ++++---- mypy_stubs/remerkleable/complex.pyi | 12 ++++++------ mypy_stubs/remerkleable/core.pyi | 1 + mypy_stubs/remerkleable/tree.pyi | 4 ++-- 8 files changed, 25 insertions(+), 20 deletions(-) create mode 100644 mypy_stubs/__init__.py create mode 100644 mypy_stubs/__init__.pyi diff --git a/mypy_stubs/__init__.py b/mypy_stubs/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/mypy_stubs/__init__.pyi b/mypy_stubs/__init__.pyi new file mode 100644 index 0000000000..a32cfa53c1 --- /dev/null +++ b/mypy_stubs/__init__.pyi @@ -0,0 +1,4 @@ +# Stubs for remerkleable (Python 3) +# +# NOTE: This dynamically typed stub was automatically generated by stubgen. + diff --git a/mypy_stubs/remerkleable/basic.pyi b/mypy_stubs/remerkleable/basic.pyi index 5b19aa4931..a69c72590f 100644 --- a/mypy_stubs/remerkleable/basic.pyi +++ b/mypy_stubs/remerkleable/basic.pyi @@ -6,7 +6,7 @@ V = TypeVar('V', bound=View) class boolean(int, BasicView): def encode_bytes(self) -> bytes: ... def __new__(cls: Any, value: int) -> Any: ... - def __bool__(self): ... + def __bool__(self) -> None: ... @classmethod def coerce_view(cls: Type[V], v: Any) -> V: ... @classmethod @@ -17,9 +17,9 @@ class boolean(int, BasicView): def type_repr(cls: Any) -> str: ... class uint(int, BasicView): - def __new__(cls: Any, value: int) -> Any: ... - def __add__(self, other: Any): ... - def __sub__(self, other: Any): ... + def __new__(cls: Any, value: int) -> Type[V]: ... + def __add__(self, other: Any) -> "uint": ... + def __sub__(self, other: Any) -> "uint": ... @classmethod def coerce_view(cls: Type[V], v: Any) -> V: ... @classmethod diff --git a/mypy_stubs/remerkleable/bitfields.pyi b/mypy_stubs/remerkleable/bitfields.pyi index 7849db7159..df4bc29ee1 100644 --- a/mypy_stubs/remerkleable/bitfields.pyi +++ b/mypy_stubs/remerkleable/bitfields.pyi @@ -14,9 +14,9 @@ class BitsView(BackedView, ColSequence): def length(self) -> int: ... def get(self, i: int) -> boolean: ... def set(self, i: int, v: boolean) -> None: ... - def __len__(self): ... + def __len__(self) -> int: ... def __iter__(self) -> Any: ... - def __getitem__(self, k: Any): ... + def __getitem__(self, k: Any) -> Any: ... def __setitem__(self, k: Any, v: Any) -> None: ... def encode_bytes(self) -> bytes: ... @classmethod @@ -24,7 +24,7 @@ class BitsView(BackedView, ColSequence): def navigate_view(self, key: Any) -> View: ... class Bitlist(BitsView): - def __new__(cls, *args: Any, **kwargs: Any): ... + def __new__(cls, *args: Any, **kwargs: Any) -> "Bitlist": ... def __class_getitem__(cls: Any, limit: Any) -> Type[Bitlist]: ... @classmethod def contents_depth(cls: Any) -> int: ... @@ -59,7 +59,7 @@ class Bitlist(BitsView): def key_to_static_gindex(cls: Any, key: Any) -> Gindex: ... class Bitvector(BitsView, FixedByteLengthViewHelper): - def __new__(cls, *args: Any, **kwargs: Any): ... + def __new__(cls, *args: Any, **kwargs: Any) -> "Bitvector": ... def __class_getitem__(cls: Any, length: Any) -> Type[Bitvector]: ... @classmethod def tree_depth(cls: Any) -> int: ... diff --git a/mypy_stubs/remerkleable/byte_arrays.pyi b/mypy_stubs/remerkleable/byte_arrays.pyi index 9f75376fae..bf31a131d7 100644 --- a/mypy_stubs/remerkleable/byte_arrays.pyi +++ b/mypy_stubs/remerkleable/byte_arrays.pyi @@ -6,7 +6,7 @@ from typing import Any, BinaryIO, Optional, Type, TypeVar V = TypeVar('V', bound=View) class RawBytesView(bytes, View): - def __new__(cls, *args: Any, **kwargs: Any): ... + def __new__(cls, *args: Any, **kwargs: Any) -> "RawBytesView": ... @classmethod def default_bytes(cls: Any) -> bytes: ... @classmethod @@ -20,10 +20,10 @@ class RawBytesView(bytes, View): def navigate_view(self, key: Any) -> View: ... class ByteVector(RawBytesView, FixedByteLengthViewHelper, View): - def __new__(cls, *args: Any, **kwargs: Any): ... + def __new__(cls, *args: Any, **kwargs: Any) -> "ByteVector": ... def __class_getitem__(cls: Any, length: Any) -> Type[ByteVector]: ... @classmethod - def vector_length(cls): ... + def vector_length(cls) -> int: ... @classmethod def default_bytes(cls: Any) -> bytes: ... @classmethod @@ -44,7 +44,7 @@ Bytes48: Any Bytes96: Any class ByteList(RawBytesView, FixedByteLengthViewHelper, View): - def __new__(cls, *args: Any, **kwargs: Any): ... + def __new__(cls, *args: Any, **kwargs: Any) -> "ByteList": ... def __class_getitem__(cls: Any, limit: Any) -> Type[ByteList]: ... @classmethod def limit(cls: Any) -> int: ... diff --git a/mypy_stubs/remerkleable/complex.pyi b/mypy_stubs/remerkleable/complex.pyi index 0e0fc7f35e..d8c533e0fe 100644 --- a/mypy_stubs/remerkleable/complex.pyi +++ b/mypy_stubs/remerkleable/complex.pyi @@ -30,7 +30,7 @@ class MonoSubtreeView(ColSequence, ComplexView): @classmethod def is_valid_count(cls: Any, count: int) -> bool: ... def __iter__(self) -> Any: ... - def readonly_iter(self): ... + def readonly_iter(self) -> Any: ... @classmethod def deserialize(cls: Type[V], stream: BinaryIO, scope: int) -> V: ... def serialize(self, stream: BinaryIO) -> int: ... @@ -39,9 +39,9 @@ class MonoSubtreeView(ColSequence, ComplexView): @classmethod def key_to_static_gindex(cls: Any, key: Any) -> Gindex: ... def navigate_view(self, key: Any) -> View: ... - def __len__(self): ... - def __add__(self, other: Any): ... - def __getitem__(self, k: Any): ... + def __len__(self) -> int: ... + def __add__(self, other: Any) -> Any: ... + def __getitem__(self, k: Any) -> Any: ... def __setitem__(self, k: Any, v: Any) -> None: ... class List(MonoSubtreeView): @@ -117,7 +117,7 @@ class _ContainerLike: def fields(cls: Any) -> Fields: ... class Container(ComplexView): - def __new__(cls: Any, *args: Any, backing: Optional[Node]=..., hook: Optional[ViewHook]=..., **kwargs: Any) -> Any: ... + def __new__(cls: Any, *args: Any, backing: Optional[Node]=..., hook: Optional[ViewHook]=..., **kwargs: Any) -> "Container": ... @classmethod def coerce_view(cls: Type[V], v: Any) -> V: ... @classmethod @@ -139,7 +139,7 @@ class Container(ComplexView): @classmethod def default_node(cls: Any) -> Node: ... def value_byte_length(self) -> int: ... - def __getattr__(self, item: Any): ... + def __getattr__(self, item: Any) -> Any: ... def __setattr__(self, key: Any, value: Any) -> None: ... @classmethod def type_repr(cls: Any) -> str: ... diff --git a/mypy_stubs/remerkleable/core.pyi b/mypy_stubs/remerkleable/core.pyi index 820ade4023..019ae792cc 100644 --- a/mypy_stubs/remerkleable/core.pyi +++ b/mypy_stubs/remerkleable/core.pyi @@ -83,6 +83,7 @@ class BackedView(View, TypeDef): def __new__(cls: Any, backing: Optional[Node]=..., hook: Optional[ViewHook[V]]=..., **kwargs: Any) -> Any: ... def get_backing(self) -> Node: ... def set_backing(self, value: Any) -> None: ... + BV = TypeVar('BV', bound='BasicView') class BasicTypeDef(TypeDef): diff --git a/mypy_stubs/remerkleable/tree.pyi b/mypy_stubs/remerkleable/tree.pyi index f1db1360bf..d966072b77 100644 --- a/mypy_stubs/remerkleable/tree.pyi +++ b/mypy_stubs/remerkleable/tree.pyi @@ -52,7 +52,7 @@ class RebindableNode(Node): class PairNode(RebindableNode, Node): left: Node right: Node - def __init__(self, left: Node, right: Node) -> Any: ... + def __init__(self, left: Node, right: Node): ... def get_left(self) -> Node: ... def get_right(self) -> Node: ... def is_leaf(self) -> bool: ... @@ -63,7 +63,7 @@ def subtree_fill_to_length(bottom: Node, depth: int, length: int) -> Node: ... def subtree_fill_to_contents(nodes: List[Node], depth: int) -> Node: ... class RootNode(Node): - def __init__(self, root: Root) -> Any: ... + def __init__(self, root: Root): ... def getter(self, target: Gindex) -> Node: ... def is_leaf(self) -> bool: ... def setter(self, target: Gindex, expand: bool=...) -> Link: ... From b418363b39d8db21b5b5b8e27d31246cb70256a7 Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Thu, 3 Sep 2020 18:39:26 +0800 Subject: [PATCH 6/7] Update remerkleable mypy_stub --- mypy_stubs/remerkleable/__init__.pyi | 4 -- mypy_stubs/remerkleable/basic.pyi | 62 ++++++++++++++++++---- mypy_stubs/remerkleable/bitfields.pyi | 17 +++--- mypy_stubs/remerkleable/byte_arrays.pyi | 27 ++++++---- mypy_stubs/remerkleable/complex.pyi | 36 ++++++++----- mypy_stubs/remerkleable/core.pyi | 62 ++++++++++------------ mypy_stubs/remerkleable/history.pyi | 4 +- mypy_stubs/remerkleable/readonly_iters.pyi | 58 ++++++++++++++++++++ mypy_stubs/remerkleable/subtree.pyi | 4 +- mypy_stubs/remerkleable/tree.pyi | 10 ++-- 10 files changed, 199 insertions(+), 85 deletions(-) create mode 100644 mypy_stubs/remerkleable/readonly_iters.pyi diff --git a/mypy_stubs/remerkleable/__init__.pyi b/mypy_stubs/remerkleable/__init__.pyi index a32cfa53c1..e69de29bb2 100644 --- a/mypy_stubs/remerkleable/__init__.pyi +++ b/mypy_stubs/remerkleable/__init__.pyi @@ -1,4 +0,0 @@ -# Stubs for remerkleable (Python 3) -# -# NOTE: This dynamically typed stub was automatically generated by stubgen. - diff --git a/mypy_stubs/remerkleable/basic.pyi b/mypy_stubs/remerkleable/basic.pyi index a69c72590f..6c62875425 100644 --- a/mypy_stubs/remerkleable/basic.pyi +++ b/mypy_stubs/remerkleable/basic.pyi @@ -1,31 +1,73 @@ -from remerkleable.core import BasicView as BasicView, View as View +from remerkleable.core import BasicView as BasicView, ObjParseException as ObjParseException, ObjType as ObjType, View as View from typing import Any, Type, TypeVar V = TypeVar('V', bound=View) +class OperationNotSupported(Exception): ... +BoolV = TypeVar('BoolV', bound='boolean') + class boolean(int, BasicView): def encode_bytes(self) -> bytes: ... def __new__(cls: Any, value: int) -> Any: ... - def __bool__(self) -> None: ... + def __add__(self, other: Any) -> None: ... + def __sub__(self, other: Any) -> None: ... + def __mul__(self, other: Any) -> None: ... + def __floordiv__(self, other: Any) -> None: ... + def __truediv__(self, other: Any) -> None: ... + def __bool__(self): ... @classmethod - def coerce_view(cls: Type[V], v: Any) -> V: ... + def coerce_view(cls: Type[BoolV], v: Any) -> BoolV: ... @classmethod def type_byte_length(cls: Any) -> int: ... @classmethod - def decode_bytes(cls: Type[V], bytez: bytes) -> V: ... + def decode_bytes(cls: Type[BoolV], bytez: bytes) -> BoolV: ... + @classmethod + def from_obj(cls: Type[BoolV], obj: ObjType) -> BoolV: ... + def to_obj(self) -> ObjType: ... @classmethod def type_repr(cls: Any) -> str: ... +T = TypeVar('T', bound='uint') +W = TypeVar('W', bound=int) class uint(int, BasicView): - def __new__(cls: Any, value: int) -> Type[V]: ... - def __add__(self, other: Any) -> "uint": ... - def __sub__(self, other: Any) -> "uint": ... + def __new__(cls: Any, value: int) -> Any: ... + def __add__(self, other: int) -> T: ... + def __radd__(self, other: int) -> T: ... + def __sub__(self, other: int) -> T: ... + def __rsub__(self, other: int) -> T: ... + def __mul__(self, other: Any): ... + def __rmul__(self, other: Any): ... + def __mod__(self, other: int) -> T: ... + def __rmod__(self, other: int) -> T: ... + def __floordiv__(self, other: int) -> T: ... + def __rfloordiv__(self, other: int) -> T: ... + def __truediv__(self, other: Any) -> None: ... + def __rtruediv__(self, other: Any) -> None: ... + def __pow__(self, other: int, modulo: Any=...) -> T: ... + def __rpow__(self, other: Any, modulo: Any=...) -> T: ... + def __lshift__(self, other: int) -> T: ... + def __rlshift__(self, other: W) -> W: ... + def __rshift__(self, other: int) -> T: ... + def __rrshift__(self, other: W) -> W: ... + def __and__(self, other: int) -> T: ... + def __rand__(self, other: int) -> T: ... + def __xor__(self, other: int) -> T: ... + def __rxor__(self, other: int) -> T: ... + def __or__(self, other: int) -> T: ... + def __ror__(self, other: int) -> T: ... + def __neg__(self) -> None: ... + def __invert__(self) -> T: ... + def __pos__(self) -> T: ... + def __abs__(self) -> T: ... @classmethod - def coerce_view(cls: Type[V], v: Any) -> V: ... + def coerce_view(cls: Type[T], v: Any) -> T: ... @classmethod - def decode_bytes(cls: Type[V], bytez: bytes) -> V: ... + def decode_bytes(cls: Type[T], bytez: bytes) -> T: ... def encode_bytes(self) -> bytes: ... @classmethod + def from_obj(cls: Type[T], obj: ObjType) -> T: ... + def to_obj(self) -> ObjType: ... + @classmethod def type_repr(cls: Any) -> str: ... class uint8(uint): @@ -47,10 +89,12 @@ class uint64(uint): class uint128(uint): @classmethod def type_byte_length(cls: Any) -> int: ... + def to_obj(self) -> ObjType: ... class uint256(uint): @classmethod def type_byte_length(cls: Any) -> int: ... + def to_obj(self) -> ObjType: ... class bit(boolean): ... class byte(uint8): ... diff --git a/mypy_stubs/remerkleable/bitfields.pyi b/mypy_stubs/remerkleable/bitfields.pyi index df4bc29ee1..8d77aa5d07 100644 --- a/mypy_stubs/remerkleable/bitfields.pyi +++ b/mypy_stubs/remerkleable/bitfields.pyi @@ -1,6 +1,7 @@ from collections.abc import Sequence as ColSequence from remerkleable.basic import boolean as boolean, uint256 as uint256 -from remerkleable.core import BackedView as BackedView, FixedByteLengthViewHelper as FixedByteLengthViewHelper, View as View, pack_bits_to_chunks as pack_bits_to_chunks +from remerkleable.core import BackedView as BackedView, FixedByteLengthViewHelper as FixedByteLengthViewHelper, ObjParseException as ObjParseException, ObjType as ObjType, View as View, pack_bits_to_chunks as pack_bits_to_chunks +from remerkleable.readonly_iters import BitfieldIter as BitfieldIter from remerkleable.tree import Gindex as Gindex, Link as Link, NavigationError as NavigationError, Node as Node, PairNode as PairNode, Root as Root, RootNode as RootNode, get_depth as get_depth, subtree_fill_to_contents as subtree_fill_to_contents, subtree_fill_to_length as subtree_fill_to_length, to_gindex as to_gindex, zero_node as zero_node from typing import Any, BinaryIO, Type, TypeVar @@ -14,18 +15,21 @@ class BitsView(BackedView, ColSequence): def length(self) -> int: ... def get(self, i: int) -> boolean: ... def set(self, i: int, v: boolean) -> None: ... - def __len__(self) -> int: ... - def __iter__(self) -> Any: ... - def __getitem__(self, k: Any) -> Any: ... + def __len__(self): ... + def __getitem__(self, k: Any): ... def __setitem__(self, k: Any, v: Any) -> None: ... def encode_bytes(self) -> bytes: ... @classmethod def decode_bytes(cls: Type[V], bytez: bytes) -> V: ... + @classmethod + def from_obj(cls: Type[V], obj: ObjType) -> V: ... + def to_obj(self) -> ObjType: ... def navigate_view(self, key: Any) -> View: ... class Bitlist(BitsView): - def __new__(cls, *args: Any, **kwargs: Any) -> "Bitlist": ... + def __new__(cls, *args: Any, **kwargs: Any): ... def __class_getitem__(cls: Any, limit: Any) -> Type[Bitlist]: ... + def __iter__(self) -> Any: ... @classmethod def contents_depth(cls: Any) -> int: ... @classmethod @@ -59,8 +63,9 @@ class Bitlist(BitsView): def key_to_static_gindex(cls: Any, key: Any) -> Gindex: ... class Bitvector(BitsView, FixedByteLengthViewHelper): - def __new__(cls, *args: Any, **kwargs: Any) -> "Bitvector": ... + def __new__(cls, *args: Any, **kwargs: Any): ... def __class_getitem__(cls: Any, length: Any) -> Type[Bitvector]: ... + def __iter__(self) -> Any: ... @classmethod def tree_depth(cls: Any) -> int: ... @classmethod diff --git a/mypy_stubs/remerkleable/byte_arrays.pyi b/mypy_stubs/remerkleable/byte_arrays.pyi index bf31a131d7..174c2033b7 100644 --- a/mypy_stubs/remerkleable/byte_arrays.pyi +++ b/mypy_stubs/remerkleable/byte_arrays.pyi @@ -1,35 +1,39 @@ from remerkleable.basic import byte as byte, uint256 as uint256 -from remerkleable.core import FixedByteLengthViewHelper as FixedByteLengthViewHelper, View as View, ViewHook as ViewHook, pack_bytes_to_chunks as pack_bytes_to_chunks, zero_node as zero_node +from remerkleable.core import FixedByteLengthViewHelper as FixedByteLengthViewHelper, ObjParseException as ObjParseException, ObjType as ObjType, View as View, ViewHook as ViewHook, pack_bytes_to_chunks as pack_bytes_to_chunks, zero_node as zero_node from remerkleable.tree import Gindex as Gindex, Node as Node, PairNode as PairNode, Root as Root, RootNode as RootNode, get_depth as get_depth, subtree_fill_to_contents as subtree_fill_to_contents, subtree_fill_to_length as subtree_fill_to_length, to_gindex as to_gindex from typing import Any, BinaryIO, Optional, Type, TypeVar -V = TypeVar('V', bound=View) +RV = TypeVar('RV', bound='RawBytesView') class RawBytesView(bytes, View): - def __new__(cls, *args: Any, **kwargs: Any) -> "RawBytesView": ... + def __new__(cls, *args: Any, **kwargs: Any): ... @classmethod def default_bytes(cls: Any) -> bytes: ... @classmethod - def coerce_view(cls: Type[V], v: Any) -> V: ... + def coerce_view(cls: Type[RV], v: Any) -> RV: ... @classmethod def tree_depth(cls: Any) -> int: ... def set_backing(self, value: Any) -> None: ... @classmethod - def decode_bytes(cls: Type[V], bytez: bytes) -> V: ... + def decode_bytes(cls: Type[RV], bytez: bytes) -> RV: ... def encode_bytes(self) -> bytes: ... + @classmethod + def from_obj(cls: Type[RV], obj: ObjType) -> RV: ... + def to_obj(self) -> ObjType: ... def navigate_view(self, key: Any) -> View: ... +BV = TypeVar('BV', bound='ByteVector') class ByteVector(RawBytesView, FixedByteLengthViewHelper, View): - def __new__(cls, *args: Any, **kwargs: Any) -> "ByteVector": ... + def __new__(cls, *args: Any, **kwargs: Any): ... def __class_getitem__(cls: Any, length: Any) -> Type[ByteVector]: ... @classmethod - def vector_length(cls) -> int: ... + def vector_length(cls): ... @classmethod def default_bytes(cls: Any) -> bytes: ... @classmethod def type_repr(cls: Any) -> str: ... @classmethod - def view_from_backing(cls: Type[V], node: Node, hook: Optional[ViewHook[V]]=...) -> V: ... + def view_from_backing(cls: Type[BV], node: Node, hook: Optional[ViewHook]=...) -> BV: ... def get_backing(self) -> Node: ... @classmethod def navigate_type(cls: Any, key: Any) -> Type[View]: ... @@ -42,9 +46,10 @@ Bytes8: Any Bytes32: Any Bytes48: Any Bytes96: Any +BL = TypeVar('BL', bound='ByteList') class ByteList(RawBytesView, FixedByteLengthViewHelper, View): - def __new__(cls, *args: Any, **kwargs: Any) -> "ByteList": ... + def __new__(cls, *args: Any, **kwargs: Any): ... def __class_getitem__(cls: Any, limit: Any) -> Type[ByteList]: ... @classmethod def limit(cls: Any) -> int: ... @@ -53,7 +58,7 @@ class ByteList(RawBytesView, FixedByteLengthViewHelper, View): @classmethod def type_repr(cls: Any) -> str: ... @classmethod - def view_from_backing(cls: Type[V], node: Node, hook: Optional[ViewHook[V]]=...) -> V: ... + def view_from_backing(cls: Type[BL], node: Node, hook: Optional[ViewHook]=...) -> BL: ... def get_backing(self) -> Node: ... @classmethod def contents_depth(cls: Any) -> int: ... @@ -72,5 +77,5 @@ class ByteList(RawBytesView, FixedByteLengthViewHelper, View): @classmethod def max_byte_length(cls: Any) -> int: ... @classmethod - def deserialize(cls: Type[V], stream: BinaryIO, scope: int) -> V: ... + def deserialize(cls: Type[BL], stream: BinaryIO, scope: int) -> BL: ... def value_byte_length(self) -> int: ... diff --git a/mypy_stubs/remerkleable/complex.pyi b/mypy_stubs/remerkleable/complex.pyi index d8c533e0fe..baf030a06d 100644 --- a/mypy_stubs/remerkleable/complex.pyi +++ b/mypy_stubs/remerkleable/complex.pyi @@ -1,6 +1,7 @@ from collections.abc import Sequence as ColSequence from remerkleable.basic import uint256 as uint256, uint32 as uint32, uint8 as uint8 -from remerkleable.core import BasicTypeDef as BasicTypeDef, BasicView as BasicView, OFFSET_BYTE_LENGTH as OFFSET_BYTE_LENGTH, View as View, ViewHook as ViewHook +from remerkleable.core import BasicView as BasicView, OFFSET_BYTE_LENGTH as OFFSET_BYTE_LENGTH, ObjParseException as ObjParseException, ObjType as ObjType, View as View, ViewHook as ViewHook +from remerkleable.readonly_iters import ComplexElemIter as ComplexElemIter, ComplexFreshElemIter as ComplexFreshElemIter, ContainerElemIter as ContainerElemIter, PackedIter as PackedIter from remerkleable.subtree import SubtreeView as SubtreeView from remerkleable.tree import Gindex as Gindex, NavigationError as NavigationError, Node as Node, PairNode as PairNode, get_depth as get_depth, subtree_fill_to_contents as subtree_fill_to_contents, subtree_fill_to_length as subtree_fill_to_length, to_gindex as to_gindex, zero_node as zero_node from typing import Any, BinaryIO, Dict, List as PyList, NamedTuple, Optional, Type, TypeVar @@ -14,11 +15,12 @@ class ComplexView(SubtreeView): def encode_bytes(self) -> bytes: ... @classmethod def decode_bytes(cls: Type[V], bytez: bytes) -> V: ... +M = TypeVar('M', bound='MonoSubtreeView') class MonoSubtreeView(ColSequence, ComplexView): def length(self) -> int: ... @classmethod - def coerce_view(cls: Type[V], v: Any) -> V: ... + def coerce_view(cls: Type[M], v: Any) -> M: ... @classmethod def element_cls(cls: Any) -> Type[View]: ... @classmethod @@ -30,18 +32,20 @@ class MonoSubtreeView(ColSequence, ComplexView): @classmethod def is_valid_count(cls: Any, count: int) -> bool: ... def __iter__(self) -> Any: ... - def readonly_iter(self) -> Any: ... + def readonly_iter(self): ... @classmethod - def deserialize(cls: Type[V], stream: BinaryIO, scope: int) -> V: ... + def deserialize(cls: Type[M], stream: BinaryIO, scope: int) -> M: ... def serialize(self, stream: BinaryIO) -> int: ... @classmethod + def from_obj(cls: Type[M], obj: ObjType) -> M: ... + @classmethod def navigate_type(cls: Any, key: Any) -> Type[View]: ... @classmethod def key_to_static_gindex(cls: Any, key: Any) -> Gindex: ... def navigate_view(self, key: Any) -> View: ... - def __len__(self) -> int: ... - def __add__(self, other: Any) -> Any: ... - def __getitem__(self, k: Any) -> Any: ... + def __len__(self): ... + def __add__(self, other: Any): ... + def __getitem__(self, k: Any): ... def __setitem__(self, k: Any, v: Any) -> None: ... class List(MonoSubtreeView): @@ -62,7 +66,7 @@ class List(MonoSubtreeView): @classmethod def tree_depth(cls: Any) -> int: ... @classmethod - def item_elem_cls(cls: Any, i: int) -> Type[V]: ... + def item_elem_cls(cls: Any, i: int) -> Type[View]: ... @classmethod def limit(cls: Any) -> int: ... @classmethod @@ -79,6 +83,7 @@ class List(MonoSubtreeView): def min_byte_length(cls: Any) -> int: ... @classmethod def max_byte_length(cls: Any) -> int: ... + def to_obj(self) -> ObjType: ... class Vector(MonoSubtreeView): def __new__(cls: Any, *args: Any, backing: Optional[Node]=..., hook: Optional[ViewHook]=..., **kwargs: Any) -> Any: ... @@ -105,6 +110,7 @@ class Vector(MonoSubtreeView): def min_byte_length(cls: Any) -> int: ... @classmethod def max_byte_length(cls: Any) -> int: ... + def to_obj(self) -> ObjType: ... Fields = Dict[str, Type[View]] class FieldOffset(NamedTuple): @@ -115,11 +121,13 @@ class FieldOffset(NamedTuple): class _ContainerLike: @classmethod def fields(cls: Any) -> Fields: ... +CV = TypeVar('CV', bound='Container') class Container(ComplexView): - def __new__(cls: Any, *args: Any, backing: Optional[Node]=..., hook: Optional[ViewHook]=..., **kwargs: Any) -> "Container": ... + def __new__(cls: Any, *args: Any, backing: Optional[Node]=..., hook: Optional[ViewHook]=..., **kwargs: Any) -> Any: ... + def __init_subclass__(cls, *args: Any, **kwargs: Any) -> None: ... @classmethod - def coerce_view(cls: Type[V], v: Any) -> V: ... + def coerce_view(cls: Type[CV], v: Any) -> CV: ... @classmethod def fields(cls: Any) -> Fields: ... @classmethod @@ -139,16 +147,20 @@ class Container(ComplexView): @classmethod def default_node(cls: Any) -> Node: ... def value_byte_length(self) -> int: ... - def __getattr__(self, item: Any) -> Any: ... + def __getattr__(self, item: Any): ... def __setattr__(self, key: Any, value: Any) -> None: ... @classmethod def type_repr(cls: Any) -> str: ... + def __iter__(self) -> Any: ... @classmethod def decode_bytes(cls: Type[V], bytez: bytes) -> V: ... @classmethod - def deserialize(cls: Type[V], stream: BinaryIO, scope: int) -> V: ... + def deserialize(cls: Type[CV], stream: BinaryIO, scope: int) -> CV: ... def serialize(self, stream: BinaryIO) -> int: ... @classmethod + def from_obj(cls: Type[CV], obj: ObjType) -> CV: ... + def to_obj(self) -> ObjType: ... + @classmethod def key_to_static_gindex(cls: Any, key: Any) -> Gindex: ... @classmethod def navigate_type(cls: Any, key: Any) -> Type[View]: ... diff --git a/mypy_stubs/remerkleable/core.pyi b/mypy_stubs/remerkleable/core.pyi index 019ae792cc..8619a0759b 100644 --- a/mypy_stubs/remerkleable/core.pyi +++ b/mypy_stubs/remerkleable/core.pyi @@ -1,26 +1,38 @@ from remerkleable.tree import Gindex as Gindex, Node as Node, Root as Root, RootNode as RootNode, concat_gindices as concat_gindices, zero_node as zero_node -from typing import Any, BinaryIO, Callable, Iterable, List as PyList, Optional, Tuple, Type, TypeVar, _ProtocolMeta +from typing import Any, BinaryIO, Callable, Iterable, List as PyList, Optional, Tuple, Type, TypeVar, Union, _ProtocolMeta OFFSET_BYTE_LENGTH: int V = TypeVar('V', bound='View') -class TypeDefMeta(_ProtocolMeta): +class ViewMeta(_ProtocolMeta): def __truediv__(self, other: Any) -> Path: ... +ObjType = Union[dict, list, tuple, str, int, bool, None] -class TypeDef(metaclass=TypeDefMeta): +class ObjParseException(Exception): ... + +class Path: + anchor: Type[View] + path: PyList[Tuple[Any, Type[View]]] + def __init__(self, anchor: Type[View], path: Optional[PyList[Tuple[Any, Type[View]]]]=...) -> Any: ... + @staticmethod + def from_raw_path(anchor: Type[View], path: PyList[Any]) -> Path: ... + def __truediv__(self, other: Any) -> Path: ... + def gindex(self, view: Optional[View]=...) -> Gindex: ... + def navigate_type(self) -> Type[View]: ... + def navigate_view(self, v: View) -> View: ... +HV = TypeVar('HV', bound='View') +ViewHook = Callable[[HV], None] + +class View(metaclass=ViewMeta): @classmethod def coerce_view(cls: Type[V], v: Any) -> V: ... @classmethod - def default(cls: Type[V], hook: Optional[ViewHook[V]]) -> V: ... - @classmethod def default_node(cls: Any) -> Node: ... @classmethod def view_from_backing(cls: Type[V], node: Node, hook: Optional[ViewHook[V]]=...) -> V: ... @classmethod def is_fixed_byte_length(cls: Any) -> bool: ... @classmethod - def type_byte_length(cls: Any) -> int: ... - @classmethod def min_byte_length(cls: Any) -> int: ... @classmethod def max_byte_length(cls: Any) -> int: ... @@ -29,26 +41,13 @@ class TypeDef(metaclass=TypeDefMeta): @classmethod def deserialize(cls: Type[V], stream: BinaryIO, scope: int) -> V: ... @classmethod + def from_obj(cls: Type[V], obj: ObjType) -> V: ... + @classmethod def type_repr(cls: Any) -> str: ... @classmethod def navigate_type(cls: Any, key: Any) -> Type[View]: ... @classmethod def key_to_static_gindex(cls: Any, key: Any) -> Gindex: ... - -class Path: - anchor: Type[View] - path: PyList[Tuple[Any, Type[View]]] - def __init__(self, anchor: Type[View], path: Optional[PyList[Tuple[Any, Type[View]]]]=...) -> Any: ... - @staticmethod - def from_raw_path(anchor: Type[View], path: PyList[Any]) -> Path: ... - def __truediv__(self, other: Any) -> Path: ... - def gindex(self, view: Optional[View]=...) -> Gindex: ... - def navigate_type(self) -> Type[View]: ... - def navigate_view(self, v: View) -> View: ... -HV = TypeVar('HV', bound='View') -ViewHook = Callable[[HV], None] - -class View(TypeDef): @classmethod def default(cls: Type[V], hook: Optional[ViewHook[V]]) -> V: ... def get_backing(self) -> Node: ... @@ -60,6 +59,7 @@ class View(TypeDef): def __bytes__(self): ... def encode_bytes(self) -> bytes: ... def serialize(self, stream: BinaryIO) -> int: ... + def to_obj(self) -> ObjType: ... def navigate_view(self, key: Any) -> View: ... def key_to_dynamic_gindex(self, key: Any) -> Gindex: ... def hash_tree_root(self) -> Root: ... @@ -76,27 +76,21 @@ class FixedByteLengthViewHelper(View): @classmethod def deserialize(cls: Type[V], stream: BinaryIO, scope: int) -> V: ... def value_byte_length(self) -> int: ... +BackedV = TypeVar('BackedV', bound='BackedView') -class BackedView(View, TypeDef): +class BackedView(View): @classmethod - def view_from_backing(cls: Type[V], node: Node, hook: Optional[ViewHook[V]]=...) -> V: ... - def __new__(cls: Any, backing: Optional[Node]=..., hook: Optional[ViewHook[V]]=..., **kwargs: Any) -> Any: ... + def view_from_backing(cls: Type[BackedV], node: Node, hook: Optional[ViewHook]=...) -> BackedV: ... + def __new__(cls: Any, backing: Optional[Node]=..., hook: Optional[ViewHook]=..., **kwargs: Any) -> Any: ... def get_backing(self) -> Node: ... def set_backing(self, value: Any) -> None: ... - BV = TypeVar('BV', bound='BasicView') -class BasicTypeDef(TypeDef): - @classmethod - def basic_view_from_backing(cls: Type[BV], node: Node, i: int) -> BV: ... - @classmethod - def pack_views(cls: Type[BV], views: PyList[BV]) -> PyList[Node]: ... - -class BasicView(FixedByteLengthViewHelper, BasicTypeDef): +class BasicView(FixedByteLengthViewHelper): @classmethod def default_node(cls: Any) -> Node: ... @classmethod - def view_from_backing(cls: Type[BV], node: Node, hook: Optional[ViewHook[BV]]=...) -> V: ... + def view_from_backing(cls: Type[BV], node: Node, hook: Optional[ViewHook[BV]]=...) -> BV: ... @classmethod def basic_view_from_backing(cls: Type[BV], node: Node, i: int) -> BV: ... @classmethod diff --git a/mypy_stubs/remerkleable/history.pyi b/mypy_stubs/remerkleable/history.pyi index 45ef15571d..d8b7fd81e4 100644 --- a/mypy_stubs/remerkleable/history.pyi +++ b/mypy_stubs/remerkleable/history.pyi @@ -1,7 +1,7 @@ from remerkleable.tree import Gindex as Gindex, Node as Node, ROOT_GINDEX as ROOT_GINDEX, get_anchor_gindex as get_anchor_gindex -from typing import List as Iterable, Tuple, TypeVar +from typing import List, Tuple, TypeVar K = TypeVar('K') -History = Iterable[Tuple[K, Node]] +History = List[Tuple[K, Node]] def get_target_history(history: History, target: Gindex) -> History: ... diff --git a/mypy_stubs/remerkleable/readonly_iters.pyi b/mypy_stubs/remerkleable/readonly_iters.pyi new file mode 100644 index 0000000000..ff4988cbd6 --- /dev/null +++ b/mypy_stubs/remerkleable/readonly_iters.pyi @@ -0,0 +1,58 @@ +from remerkleable.core import BasicView as BasicView, View as View +from remerkleable.tree import Node as Node, Root as Root, RootNode as RootNode, ZERO_ROOT as ZERO_ROOT +from typing import Any, List as PyList, Optional, Sequence, Type + +class BitfieldIter: + anchor: Node + depth: int + i: int + j: int + rootIndex: int + length: int + currentRoot: Root + stack: PyList[Optional[Node]] + def __init__(self, anchor: Node, depth: int, length: int) -> Any: ... + def __iter__(self) -> Any: ... + def __next__(self): ... + +class PackedIter: + anchor: Node + depth: int + i: int + j: int + rootIndex: int + length: int + per_node: int + currentRoot: RootNode + elem_type: Type[BasicView] + stack: PyList[Optional[Node]] + def __init__(self, anchor: Node, depth: int, length: int, elem_type: Type[BasicView]) -> Any: ... + def __iter__(self) -> Any: ... + def __next__(self): ... + +class NodeIter: + anchor: Node + depth: int + i: int + length: int + per_node: int + stack: PyList[Optional[Node]] + currentRoot: Any = ... + def __init__(self, anchor: Node, depth: int, length: int) -> Any: ... + def __iter__(self) -> Any: ... + def __next__(self): ... + +class ComplexElemIter(NodeIter): + reused_view: View + def __init__(self, anchor: Node, depth: int, length: int, elem_type: Type[View]) -> Any: ... + def __next__(self): ... + +class ComplexFreshElemIter(NodeIter): + elem_type: Type[View] + def __init__(self, anchor: Node, depth: int, length: int, elem_type: Type[View]) -> Any: ... + def __next__(self): ... + +class ContainerElemIter(NodeIter): + elem_types: Sequence[Type[View]] + def __init__(self, anchor: Node, depth: int, elem_types: Sequence[Type[View]]) -> Any: ... + def __next__(self): ... diff --git a/mypy_stubs/remerkleable/subtree.pyi b/mypy_stubs/remerkleable/subtree.pyi index 44b7d3db46..22eb028a3e 100644 --- a/mypy_stubs/remerkleable/subtree.pyi +++ b/mypy_stubs/remerkleable/subtree.pyi @@ -1,8 +1,8 @@ -from remerkleable.core import BackedView as BackedView, BasicTypeDef as BasicTypeDef, BasicView as BasicView, TypeDef as TypeDef, View as View +from remerkleable.core import BackedView as BackedView, BasicView as BasicView, View as View from remerkleable.tree import Link as Link, to_gindex as to_gindex from typing import Any, Type -class SubtreeView(BackedView, TypeDef): +class SubtreeView(BackedView): @classmethod def is_packed(cls: Any) -> bool: ... @classmethod diff --git a/mypy_stubs/remerkleable/tree.pyi b/mypy_stubs/remerkleable/tree.pyi index d966072b77..5b3818db32 100644 --- a/mypy_stubs/remerkleable/tree.pyi +++ b/mypy_stubs/remerkleable/tree.pyi @@ -1,4 +1,4 @@ -from typing import Any, Iterable, Iterator, List, Tuple, TypeVar +from typing import Any, Callable, Iterable, Iterator, List, Tuple, TypeVar def get_depth(elem_count: int) -> int: ... @@ -7,13 +7,13 @@ ROOT_GINDEX: Any LEFT_GINDEX: Any RIGHT_GINDEX: Any -def to_gindex(index: int, depth: int) -> Any: ... +def to_gindex(index: int, depth: int) -> Gindex: ... def get_anchor_gindex(gindex: Gindex) -> Gindex: ... def gindex_bit_iter(gindex: Gindex) -> Tuple[Iterator[bool], int]: ... def concat_gindices(steps: Iterable[Gindex]) -> Gindex: ... Root: Any -MerkleFn: Any +MerkleFn = Callable[[Root, Root], Root] ZERO_ROOT: Root def merkle_hash(left: Root, right: Root) -> Root: ... @@ -52,7 +52,7 @@ class RebindableNode(Node): class PairNode(RebindableNode, Node): left: Node right: Node - def __init__(self, left: Node, right: Node): ... + def __init__(self, left: Node, right: Node) -> Any: ... def get_left(self) -> Node: ... def get_right(self) -> Node: ... def is_leaf(self) -> bool: ... @@ -63,7 +63,7 @@ def subtree_fill_to_length(bottom: Node, depth: int, length: int) -> Node: ... def subtree_fill_to_contents(nodes: List[Node], depth: int) -> Node: ... class RootNode(Node): - def __init__(self, root: Root): ... + def __init__(self, root: Root) -> Any: ... def getter(self, target: Gindex) -> Node: ... def is_leaf(self) -> bool: ... def setter(self, target: Gindex, expand: bool=...) -> Link: ... From a77f7829b4b6c5dd31f5ac7033bca877d009584f Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Wed, 12 May 2021 10:22:22 +0800 Subject: [PATCH 7/7] Update stubs --- mypy_stubs/eth2spec/utils/ssz/ssz_impl.pyi | 8 +++++++- mypy_stubs/eth2spec/utils/ssz/ssz_typing.pyi | 7 +++++-- mypy_stubs/remerkleable/complex.pyi | 9 +++++++-- mypy_stubs/remerkleable/core.pyi | 2 +- mypy_stubs/remerkleable/readonly_iters.pyi | 12 ++++++------ mypy_stubs/remerkleable/tree.pyi | 4 ++-- mypy_stubs/remerkleable/virtual.pyi | 3 +-- 7 files changed, 29 insertions(+), 16 deletions(-) diff --git a/mypy_stubs/eth2spec/utils/ssz/ssz_impl.pyi b/mypy_stubs/eth2spec/utils/ssz/ssz_impl.pyi index 430fe735b2..36d441ae3e 100644 --- a/mypy_stubs/eth2spec/utils/ssz/ssz_impl.pyi +++ b/mypy_stubs/eth2spec/utils/ssz/ssz_impl.pyi @@ -1,5 +1,11 @@ +from remerkleable.basic import uint as uint from remerkleable.byte_arrays import Bytes32 -from remerkleable.core import View as View +from remerkleable.core import View +from typing import TypeVar def serialize(obj: View) -> bytes: ... def hash_tree_root(obj: View) -> Bytes32: ... +def uint_to_bytes(n: uint) -> bytes: ... +V = TypeVar('V', bound=View) + +def copy(obj: V) -> V: ... diff --git a/mypy_stubs/eth2spec/utils/ssz/ssz_typing.pyi b/mypy_stubs/eth2spec/utils/ssz/ssz_typing.pyi index 9ed955c03e..dbe22d5728 100644 --- a/mypy_stubs/eth2spec/utils/ssz/ssz_typing.pyi +++ b/mypy_stubs/eth2spec/utils/ssz/ssz_typing.pyi @@ -1,5 +1,8 @@ from remerkleable.basic import bit as bit, boolean as boolean, byte as byte, uint as uint, uint128 as uint128, uint16 as uint16, uint256 as uint256, uint32 as uint32, uint64 as uint64, uint8 as uint8 from remerkleable.bitfields import Bitlist as Bitlist, Bitvector as Bitvector -from remerkleable.byte_arrays import ByteList as ByteList, ByteVector as ByteVector, Bytes1 as Bytes1, Bytes32 as Bytes32, Bytes4 as Bytes4, Bytes48 as Bytes48, Bytes8 as Bytes8, Bytes96 as Bytes96 +from remerkleable.byte_arrays import ByteList as ByteList, Bytes1 as Bytes1, Bytes32 as Bytes32, Bytes4 as Bytes4, Bytes48 as Bytes48, Bytes8 as Bytes8, Bytes96 as Bytes96 from remerkleable.complex import Container as Container, List as List, Vector as Vector -from remerkleable.core import BasicView as BasicView, TypeDef as TypeDef, View as View +from remerkleable.core import BasicView as BasicView, Path as Path, View as View +from typing import Any + +Bytes20: Any diff --git a/mypy_stubs/remerkleable/complex.pyi b/mypy_stubs/remerkleable/complex.pyi index baf030a06d..9fc43b6b5e 100644 --- a/mypy_stubs/remerkleable/complex.pyi +++ b/mypy_stubs/remerkleable/complex.pyi @@ -123,8 +123,13 @@ class _ContainerLike: def fields(cls: Any) -> Fields: ... CV = TypeVar('CV', bound='Container') -class Container(ComplexView): - def __new__(cls: Any, *args: Any, backing: Optional[Node]=..., hook: Optional[ViewHook]=..., **kwargs: Any) -> Any: ... +class _ContainerBase(ComplexView): + def __new__(cls: Any, *args: Any, backing: Optional[Node]=..., hook: Optional[ViewHook]=..., append_nodes: Optional[PyList[Node]]=..., **kwargs: Any) -> Any: ... + @classmethod + def fields(cls: Any) -> Fields: ... + +class Container(_ContainerBase): + def __new__(cls: Any, *args: Any, backing: Optional[Node]=..., hook: Optional[ViewHook]=..., append_nodes: Optional[PyList[Node]]=..., **kwargs: Any) -> Any: ... def __init_subclass__(cls, *args: Any, **kwargs: Any) -> None: ... @classmethod def coerce_view(cls: Type[CV], v: Any) -> CV: ... diff --git a/mypy_stubs/remerkleable/core.pyi b/mypy_stubs/remerkleable/core.pyi index 8619a0759b..eeba1aa39d 100644 --- a/mypy_stubs/remerkleable/core.pyi +++ b/mypy_stubs/remerkleable/core.pyi @@ -13,7 +13,7 @@ class ObjParseException(Exception): ... class Path: anchor: Type[View] path: PyList[Tuple[Any, Type[View]]] - def __init__(self, anchor: Type[View], path: Optional[PyList[Tuple[Any, Type[View]]]]=...) -> Any: ... + def __init__(self, anchor: Type[View], path: Optional[PyList[Tuple[Any, Type[View]]]]=...) -> None: ... @staticmethod def from_raw_path(anchor: Type[View], path: PyList[Any]) -> Path: ... def __truediv__(self, other: Any) -> Path: ... diff --git a/mypy_stubs/remerkleable/readonly_iters.pyi b/mypy_stubs/remerkleable/readonly_iters.pyi index ff4988cbd6..7eb271c1a8 100644 --- a/mypy_stubs/remerkleable/readonly_iters.pyi +++ b/mypy_stubs/remerkleable/readonly_iters.pyi @@ -11,7 +11,7 @@ class BitfieldIter: length: int currentRoot: Root stack: PyList[Optional[Node]] - def __init__(self, anchor: Node, depth: int, length: int) -> Any: ... + def __init__(self, anchor: Node, depth: int, length: int) -> None: ... def __iter__(self) -> Any: ... def __next__(self): ... @@ -26,7 +26,7 @@ class PackedIter: currentRoot: RootNode elem_type: Type[BasicView] stack: PyList[Optional[Node]] - def __init__(self, anchor: Node, depth: int, length: int, elem_type: Type[BasicView]) -> Any: ... + def __init__(self, anchor: Node, depth: int, length: int, elem_type: Type[BasicView]) -> None: ... def __iter__(self) -> Any: ... def __next__(self): ... @@ -38,21 +38,21 @@ class NodeIter: per_node: int stack: PyList[Optional[Node]] currentRoot: Any = ... - def __init__(self, anchor: Node, depth: int, length: int) -> Any: ... + def __init__(self, anchor: Node, depth: int, length: int) -> None: ... def __iter__(self) -> Any: ... def __next__(self): ... class ComplexElemIter(NodeIter): reused_view: View - def __init__(self, anchor: Node, depth: int, length: int, elem_type: Type[View]) -> Any: ... + def __init__(self, anchor: Node, depth: int, length: int, elem_type: Type[View]) -> None: ... def __next__(self): ... class ComplexFreshElemIter(NodeIter): elem_type: Type[View] - def __init__(self, anchor: Node, depth: int, length: int, elem_type: Type[View]) -> Any: ... + def __init__(self, anchor: Node, depth: int, length: int, elem_type: Type[View]) -> None: ... def __next__(self): ... class ContainerElemIter(NodeIter): elem_types: Sequence[Type[View]] - def __init__(self, anchor: Node, depth: int, elem_types: Sequence[Type[View]]) -> Any: ... + def __init__(self, anchor: Node, depth: int, elem_types: Sequence[Type[View]]) -> None: ... def __next__(self): ... diff --git a/mypy_stubs/remerkleable/tree.pyi b/mypy_stubs/remerkleable/tree.pyi index 5b3818db32..c01c335069 100644 --- a/mypy_stubs/remerkleable/tree.pyi +++ b/mypy_stubs/remerkleable/tree.pyi @@ -52,7 +52,7 @@ class RebindableNode(Node): class PairNode(RebindableNode, Node): left: Node right: Node - def __init__(self, left: Node, right: Node) -> Any: ... + def __init__(self, left: Node, right: Node) -> None: ... def get_left(self) -> Node: ... def get_right(self) -> Node: ... def is_leaf(self) -> bool: ... @@ -63,7 +63,7 @@ def subtree_fill_to_length(bottom: Node, depth: int, length: int) -> Node: ... def subtree_fill_to_contents(nodes: List[Node], depth: int) -> Node: ... class RootNode(Node): - def __init__(self, root: Root) -> Any: ... + def __init__(self, root: Root) -> None: ... def getter(self, target: Gindex) -> Node: ... def is_leaf(self) -> bool: ... def setter(self, target: Gindex, expand: bool=...) -> Link: ... diff --git a/mypy_stubs/remerkleable/virtual.pyi b/mypy_stubs/remerkleable/virtual.pyi index 8d983248de..a86d4763bd 100644 --- a/mypy_stubs/remerkleable/virtual.pyi +++ b/mypy_stubs/remerkleable/virtual.pyi @@ -1,5 +1,4 @@ from remerkleable.tree import NavigationError as NavigationError, Node as Node, RebindableNode as RebindableNode, Root as Root -from typing import Any class VirtualSource: def get_left(self, key: Root) -> Node: ... @@ -7,7 +6,7 @@ class VirtualSource: def is_leaf(self, key: Root) -> bool: ... class VirtualNode(RebindableNode, Node): - def __init__(self, root: Root, src: VirtualSource) -> Any: ... + def __init__(self, root: Root, src: VirtualSource) -> None: ... def get_left(self) -> Node: ... def get_right(self) -> Node: ... def is_leaf(self) -> bool: ...