mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-01-22 21:30:56 +00:00
style(core): improve types in rlp.py
This commit is contained in:
parent
8d8cfd6692
commit
228c4db646
@ -5,10 +5,16 @@ if TYPE_CHECKING:
|
|||||||
from typing import Union
|
from typing import Union
|
||||||
from trezor.utils import Writer
|
from trezor.utils import Writer
|
||||||
|
|
||||||
# what we want:
|
# The intention below is basically:
|
||||||
# RLPItem = Union[list["RLPItem"], bytes, int]
|
# RLPItem = int | bytes | list[RLPItem]
|
||||||
# what mypy can process:
|
# That will not typecheck though. Type `list` is invariant in its parameter, meaning
|
||||||
RLPItem = Union[list, bytes, int]
|
# 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)
|
STRING_HEADER_BYTE = const(0x80)
|
||||||
@ -80,7 +86,7 @@ def write_string(w: Writer, string: bytes) -> None:
|
|||||||
w.extend(string)
|
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)
|
payload_length = sum(length(item) for item in lst)
|
||||||
write_header(w, payload_length, LIST_HEADER_BYTE)
|
write_header(w, payload_length, LIST_HEADER_BYTE)
|
||||||
for item in lst:
|
for item in lst:
|
||||||
|
Loading…
Reference in New Issue
Block a user