Skip to content

Commit

Permalink
style(core): improve types in rlp.py
Browse files Browse the repository at this point in the history
  • Loading branch information
matejcik committed Jan 5, 2022
1 parent dc178b4 commit e68e814
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions core/src/trezor/crypto/rlp.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@
from typing import Union
from trezor.utils import Writer

# what we want:
# RLPItem = Union[list["RLPItem"], bytes, int]
# what mypy can process:
RLPItem = Union[list, bytes, int]
# The intention below is basically:
# RLPItem = int | bytes | list[RLPItem]
# That will not typecheck though. Type `list` is invariant in its parameter, meaning
# that we cannot pass list[bytes] into a list[RLPItem] parameter (what if the
# function wanted to append an int?). We do want to enforce that it's a `list`, not
# a generic `Sequence` (because we do isinstance checks for a list). We are however
# only reading from the list and passing into things that consume a RLPItem. Hence
# we have to enumerate single-type lists as well as the universal list[RLPItem].
RLPList = Union[list[int], list[bytes], list["RLPItem"]]
RLPItem = Union[RLPList, bytes, int]


STRING_HEADER_BYTE = const(0x80)
Expand Down Expand Up @@ -80,7 +86,7 @@ def write_string(w: Writer, string: bytes) -> None:
w.extend(string)


def write_list(w: Writer, lst: list[RLPItem]) -> None:
def write_list(w: Writer, lst: RLPList) -> None:
payload_length = sum(length(item) for item in lst)
write_header(w, payload_length, LIST_HEADER_BYTE)
for item in lst:
Expand Down

0 comments on commit e68e814

Please sign in to comment.