Skip to content

Commit

Permalink
type codec.py and exceptions, change mypy ignore from files to error …
Browse files Browse the repository at this point in the history
…types, add py.typed back at top level
  • Loading branch information
pacrob committed Feb 14, 2024
1 parent d063fc6 commit abbe210
Show file tree
Hide file tree
Showing 12 changed files with 40 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,4 @@ repos:
hooks:
- id: mypy
args: [--follow-imports=silent, --disable-error-code=no-untyped-call]
exclude: tests\/|eth_abi\/(base\.py|registry\.py|grammar\.py|packed\.py|decoding\.py|encoding\.py|exceptions\.py|codec\.py|constants\.py)
exclude: tests/
8 changes: 1 addition & 7 deletions eth_abi/base.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import functools

from eth_typing.abi import (
TypeStr,
)

from .grammar import (
BasicType,
TupleType,
Expand Down Expand Up @@ -142,9 +138,7 @@ def validate(self):
pass

@classmethod
def from_type_str(
cls, type_str: TypeStr, registry
) -> "BaseCoder": # pragma: no cover
def from_type_str(cls, type_str, registry): # pragma: no cover
"""
Used by :any:`ABIRegistry` to get an appropriate encoder or decoder
instance for the given type string and type registry.
Expand Down
3 changes: 2 additions & 1 deletion eth_abi/codec.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Any,
Iterable,
Tuple,
cast,
)

from eth_typing.abi import (
Expand Down Expand Up @@ -158,7 +159,7 @@ def decode(
decoder = TupleDecoder(decoders=decoders)
stream = self.stream_class(data)

return decoder(stream)
return cast(Tuple[Any, ...], decoder(stream))


class ABICodec(ABIEncoder, ABIDecoder):
Expand Down
13 changes: 11 additions & 2 deletions eth_abi/decoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io
from typing import (
Any,
Generator,
)

from eth_utils import (
Expand Down Expand Up @@ -144,6 +145,8 @@ def decode(self, stream):
start_pos = decode_uint_256(stream)

stream.push_frame(start_pos)
if self.tail_decoder is None:
raise AssertionError("`tail_decoder` is None")
value = self.tail_decoder(stream)
stream.pop_frame()

Expand All @@ -169,8 +172,8 @@ def validate(self):
if self.decoders is None:
raise ValueError("No `decoders` set")

@to_tuple
def decode(self, stream):
@to_tuple # type: ignore[misc] # untyped decorator
def decode(self, stream: ContextFramesBytesIO) -> Generator[Any, None, None]:
for decoder in self.decoders:
yield decoder(stream)

Expand Down Expand Up @@ -198,6 +201,8 @@ def validate_padding_bytes(self, value, padding_bytes):
def decode(self, stream):
raw_data = self.read_data_from_stream(stream)
data, padding_bytes = self.split_data_and_padding(raw_data)
if self.decoder_fn is None:
raise AssertionError("`decoder_fn` is None")
value = self.decoder_fn(data)
self.validate_padding_bytes(value, padding_bytes)

Expand Down Expand Up @@ -254,6 +259,8 @@ def __init__(self, **kwargs):

@to_tuple
def decode(self, stream):
if self.item_decoder is None:
raise AssertionError("`item_decoder` is None")
for _ in range(self.array_size):
yield self.item_decoder(stream)

Expand All @@ -266,6 +273,8 @@ class DynamicArrayDecoder(BaseArrayDecoder):
def decode(self, stream):
array_size = decode_uint_256(stream)
stream.push_frame(32)
if self.item_decoder is None:
raise AssertionError("`item_decoder` is None")
for _ in range(array_size):
yield self.item_decoder(stream)
stream.pop_frame()
Expand Down
6 changes: 6 additions & 0 deletions eth_abi/encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ def validate_value(self, value):

def encode(self, value):
self.validate_value(value)
if self.encode_fn is None:
raise AssertionError("`encode_fn` is None")
base_encoded_value = self.encode_fn(value)

if self.is_big_endian:
Expand Down Expand Up @@ -249,6 +251,8 @@ def validate(self):
raise ValueError("`type_check_fn` cannot be null")

def validate_value(self, value):
if self.type_check_fn is None:
raise AssertionError("`type_check_fn` is None")
if not self.type_check_fn(value):
self.invalidate_value(value)

Expand Down Expand Up @@ -586,6 +590,8 @@ def encode_elements(self, value):
self.validate_value(value)

item_encoder = self.item_encoder
if item_encoder is None:
raise AssertionError("`item_encoder` is None")
tail_chunks = tuple(item_encoder(i) for i in value)

items_are_dynamic = getattr(item_encoder, "is_dynamic", False)
Expand Down
4 changes: 2 additions & 2 deletions eth_abi/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ class NonEmptyPaddingBytes(DecodingError):
"""


class ParseError(parsimonious.ParseError):
class ParseError(parsimonious.ParseError): # type: ignore[misc] # subclasses Any
"""
Raised when an ABI type string cannot be parsed.
"""

def __str__(self):
def __str__(self) -> str:
return (
f"Parse error at '{self.text[self.pos : self.pos + 5]}' "
f"(column {self.column()}) in type string '{self.text}'"
Expand Down
2 changes: 1 addition & 1 deletion eth_abi/grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
)


class NodeVisitor(parsimonious.NodeVisitor):
class NodeVisitor(parsimonious.NodeVisitor): # type: ignore[misc] # subclasses Any
"""
Parsimonious node visitor which performs both parsing of type strings and
post-processing of parse trees. Parsing operations are cached.
Expand Down
File renamed without changes.
21 changes: 13 additions & 8 deletions eth_abi/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import (
Any,
Callable,
Optional,
Type,
Union,
)
Expand Down Expand Up @@ -276,18 +277,18 @@ def is_base_tuple(type_str):
return isinstance(abi_type, grammar.TupleType) and abi_type.arrlist is None


def _clear_encoder_cache(old_method):
def _clear_encoder_cache(old_method: Callable[..., None]) -> Callable[..., None]:
@functools.wraps(old_method)
def new_method(self, *args, **kwargs):
def new_method(self: "ABIRegistry", *args: Any, **kwargs: Any) -> None:
self.get_encoder.cache_clear()
return old_method(self, *args, **kwargs)

return new_method


def _clear_decoder_cache(old_method):
def _clear_decoder_cache(old_method: Callable[..., None]) -> Callable[..., None]:
@functools.wraps(old_method)
def new_method(self, *args, **kwargs):
def new_method(self: "ABIRegistry", *args: Any, **kwargs: Any) -> None:
self.get_decoder.cache_clear()
return old_method(self, *args, **kwargs)

Expand Down Expand Up @@ -354,7 +355,7 @@ def _get_registration(self, mapping, type_str):

@_clear_encoder_cache
def register_encoder(
self, lookup: Lookup, encoder: Encoder, label: str = None
self, lookup: Lookup, encoder: Encoder, label: Optional[str] = None
) -> None:
"""
Registers the given ``encoder`` under the given ``lookup``. A unique
Expand All @@ -377,7 +378,7 @@ def unregister_encoder(self, lookup_or_label: Lookup) -> None:

@_clear_decoder_cache
def register_decoder(
self, lookup: Lookup, decoder: Decoder, label: str = None
self, lookup: Lookup, decoder: Decoder, label: Optional[str] = None
) -> None:
"""
Registers the given ``decoder`` under the given ``lookup``. A unique
Expand All @@ -399,7 +400,11 @@ def unregister_decoder(self, lookup_or_label: Lookup) -> None:
self._unregister(self._decoders, lookup_or_label)

def register(
self, lookup: Lookup, encoder: Encoder, decoder: Decoder, label: str = None
self,
lookup: Lookup,
encoder: Encoder,
decoder: Decoder,
label: Optional[str] = None,
) -> None:
"""
Registers the given ``encoder`` and ``decoder`` under the given
Expand Down Expand Up @@ -438,7 +443,7 @@ def register(
self.register_encoder(lookup, encoder, label=label)
self.register_decoder(lookup, decoder, label=label)

def unregister(self, label: str) -> None:
def unregister(self, label: Optional[str]) -> None:
"""
Unregisters the entries in the encoder and decoder registries which
have the label ``label``.
Expand Down
Empty file removed eth_abi/utils/py.typed
Empty file.
1 change: 1 addition & 0 deletions newsfragments/221.internal.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Clear mypy ``misc``-type errors and add top-level ``py.typed`` file back
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ profile = "black"
[tool.mypy]
check_untyped_defs = true
disallow_incomplete_defs = true
disallow_untyped_defs = true
disallow_untyped_defs = false
disallow_any_generics = true
disallow_untyped_calls = true
disallow_untyped_decorators = true
Expand All @@ -27,6 +27,7 @@ warn_redundant_casts = true
warn_return_any = true
warn_unused_configs = true
warn_unused_ignores = true
disable_error_code = ["arg-type", "call-arg", "call-overload", "assignment", "operator", "var-annotated", "attr-defined", "has-type", "union-attr"]


[tool.pydocstyle]
Expand Down

0 comments on commit abbe210

Please sign in to comment.