From f08bed06853aab75c85423e2714f27288d060b50 Mon Sep 17 00:00:00 2001 From: KG Date: Tue, 12 Sep 2023 12:23:42 -0400 Subject: [PATCH] Using Sequence type hints for iterables --- tivars/types/group.py | 11 ++++++----- tivars/types/list.py | 8 ++++---- tivars/types/matrix.py | 8 ++++---- tivars/types/picture.py | 20 ++++++++++---------- 4 files changed, 24 insertions(+), 23 deletions(-) diff --git a/tivars/types/group.py b/tivars/types/group.py index 87be4f5..7875d45 100644 --- a/tivars/types/group.py +++ b/tivars/types/group.py @@ -1,5 +1,6 @@ import io +from typing import Sequence from warnings import warn from tivars.models import * @@ -37,9 +38,9 @@ def __init__(self, init=None, *, super().__init__(init, for_flash=for_flash, name=name, version=version, archived=archived, data=data) @staticmethod - def group(entries: list[TIEntry], *, name: str = "GROUP") -> 'TIGroup': + def group(entries: Sequence[TIEntry], *, name: str = "GROUP") -> 'TIGroup': """ - Creates a new `TIGroup` by packaging a ``list`` of entries using defaulted VAT data + Creates a new `TIGroup` by packaging a sequence of entries using defaulted VAT data :param entries: The entries to group :param name: The name of the group (defaults to ``GROUP``) @@ -145,10 +146,10 @@ def ungroup(self, data: bytes = None) -> list[TIEntry]: return entries - @Loader[list] - def load_from_entries(self, entries: list[TIEntry]): + @Loader[Sequence] + def load_from_entries(self, entries: Sequence[TIEntry]): """ - Loads a ``list`` of entries into this group + Loads a sequence of entries into this group All VAT data is cleared. diff --git a/tivars/types/list.py b/tivars/types/list.py index f6cc664..5ce615c 100644 --- a/tivars/types/list.py +++ b/tivars/types/list.py @@ -1,7 +1,7 @@ import re from io import BytesIO -from typing import ByteString, Iterator +from typing import ByteString, Iterator, Sequence from warnings import warn from tivars.models import * @@ -160,10 +160,10 @@ def load_bytes(self, data: bytes | BytesIO): f"(expected {self.calc_data_length // self._E.min_data_length}, got {self.length}).", BytesWarning) - @Loader[list] - def load_list(self, lst: list[_E]): + @Loader[Sequence] + def load_list(self, lst: Sequence[_E]): """ - Loads a ``list`` into this list + Loads a sequence into this list :param lst: The list to load """ diff --git a/tivars/types/matrix.py b/tivars/types/matrix.py index 474e03e..3aa64ad 100644 --- a/tivars/types/matrix.py +++ b/tivars/types/matrix.py @@ -1,5 +1,5 @@ from io import BytesIO -from typing import ByteString, Iterator +from typing import ByteString, Iterator, Sequence from warnings import warn from tivars.models import * @@ -141,10 +141,10 @@ def load_data_section(self, data: BytesIO): height = int.from_bytes(height_byte := data.read(1), 'little') self.raw.calc_data = bytearray(width_byte + height_byte + data.read(width * height)) - @Loader[list] - def load_matrix(self, matrix: list[list[RealEntry]]): + @Loader[Sequence] + def load_matrix(self, matrix: Sequence[Sequence[RealEntry]]): """ - Loads a two-dimensional ``list`` into this matrix + Loads a two-dimensional sequence into this matrix :param matrix: The matrix to load """ diff --git a/tivars/types/picture.py b/tivars/types/picture.py index dc1f4d5..8f9d3b9 100644 --- a/tivars/types/picture.py +++ b/tivars/types/picture.py @@ -1,4 +1,4 @@ -from typing import Iterator +from typing import Iterator, Sequence from warnings import warn from tivars.models import * @@ -192,10 +192,10 @@ def __iter__(self) -> Iterator[pixel_type]: for col in row: yield col - @Loader[list, ] - def load_array(self, arr: list[list[pixel_type]]): + @Loader[Sequence] + def load_array(self, arr: Sequence[Sequence[pixel_type]]): """ - Loads a two-dimensional ``list`` of pixels into this picture + Loads a two-dimensional sequence of pixels into this picture :param arr: The array to load """ @@ -257,8 +257,8 @@ def __init__(self, init=None, *, def get_min_os(self, data: bytes = None) -> OsVersion: return TI_83P.OS() - @Loader[list] - def load_array(self, arr: list[list[pixel_type]]): + @Loader[Sequence] + def load_array(self, arr: Sequence[Sequence[pixel_type]]): self.data = b''.join(L1.set(entry) for row in arr for entry in zip(*[iter(row)] * 8, strict=True)) def array(self) -> list[list[pixel_type]]: @@ -308,8 +308,8 @@ def __init__(self, init=None, *, def get_min_os(self, data: bytes = None) -> OsVersion: return TI_84PCSE.OS() - @Loader[list] - def load_array(self, arr: list[list[pixel_type]]): + @Loader[Sequence] + def load_array(self, arr: Sequence[Sequence[pixel_type]]): self.data = b''.join(RGBPalette.set(entry) for row in arr for entry in zip(row[::2], row[1::2])) def array(self) -> list[list[pixel_type]]: @@ -400,8 +400,8 @@ def data(self) -> bytes: def get_min_os(self, data: bytes = None) -> OsVersion: return TI_84PCSE.OS() - @Loader[list] - def load_array(self, arr: list[list[pixel_type]]): + @Loader[Sequence] + def load_array(self, arr: Sequence[Sequence[pixel_type]]): self.data = b''.join(RGB565.set(entry) for row in arr[::-1] for entry in row + [(0, 0, 0)]) def array(self) -> list[list[pixel_type]]: