From 8ba3fbe93be9a0ac1a1ac6d242373318b26325a9 Mon Sep 17 00:00:00 2001 From: beauxq Date: Thu, 4 Apr 2024 09:20:42 -0700 Subject: [PATCH 1/2] some worlds: some typing in `LocalRom` ### `read_bytes` It's not safe to return `bytearray` when we think it's `bytes` ```python a = rom.read_bytes(8, 3) hash(a) # This won't crash, right? ``` ### `write_bytes` `Iterable[SupportsIndex]` is what's required for `bytearray.__setitem__(slice, values)` We need to add `__len__` for the `len(values)` in this function. --- worlds/alttp/Rom.py | 6 +++--- worlds/dkc3/Rom.py | 2 +- worlds/smw/Rom.py | 2 +- worlds/yoshisisland/Rom.py | 6 +++--- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/worlds/alttp/Rom.py b/worlds/alttp/Rom.py index 6ef1f0db194..3bd31d581b1 100644 --- a/worlds/alttp/Rom.py +++ b/worlds/alttp/Rom.py @@ -18,7 +18,7 @@ import threading import concurrent.futures import bsdiff4 -from typing import Optional, List +from typing import Collection, Optional, List, SupportsIndex from BaseClasses import CollectionState, Region, Location, MultiWorld from Utils import local_path, user_path, int16_as_bytes, int32_as_bytes, snes_to_pc, is_frozen, parse_yaml, read_snes_rom @@ -71,13 +71,13 @@ def __init__(self, file, patch=True, vanillaRom=None, name=None, hash=None): def read_byte(self, address: int) -> int: return self.buffer[address] - def read_bytes(self, startaddress: int, length: int) -> bytes: + def read_bytes(self, startaddress: int, length: int) -> bytearray: return self.buffer[startaddress:startaddress + length] def write_byte(self, address: int, value: int): self.buffer[address] = value - def write_bytes(self, startaddress: int, values): + def write_bytes(self, startaddress: int, values: Collection[SupportsIndex]) -> None: self.buffer[startaddress:startaddress + len(values)] = values def encrypt_range(self, startaddress: int, length: int, key: bytes): diff --git a/worlds/dkc3/Rom.py b/worlds/dkc3/Rom.py index efe8033d0fa..91828943feb 100644 --- a/worlds/dkc3/Rom.py +++ b/worlds/dkc3/Rom.py @@ -457,7 +457,7 @@ def read_bit(self, address: int, bit_number: int) -> bool: def read_byte(self, address: int) -> int: return self.buffer[address] - def read_bytes(self, startaddress: int, length: int) -> bytes: + def read_bytes(self, startaddress: int, length: int) -> bytearray: return self.buffer[startaddress:startaddress + length] def write_byte(self, address: int, value: int): diff --git a/worlds/smw/Rom.py b/worlds/smw/Rom.py index 36078d4622b..ff3b5c31634 100644 --- a/worlds/smw/Rom.py +++ b/worlds/smw/Rom.py @@ -83,7 +83,7 @@ def read_bit(self, address: int, bit_number: int) -> bool: def read_byte(self, address: int) -> int: return self.buffer[address] - def read_bytes(self, startaddress: int, length: int) -> bytes: + def read_bytes(self, startaddress: int, length: int) -> bytearray: return self.buffer[startaddress:startaddress + length] def write_byte(self, address: int, value: int): diff --git a/worlds/yoshisisland/Rom.py b/worlds/yoshisisland/Rom.py index fa3006afcf9..37c4196b7c3 100644 --- a/worlds/yoshisisland/Rom.py +++ b/worlds/yoshisisland/Rom.py @@ -3,7 +3,7 @@ import Utils from worlds.Files import APDeltaPatch from settings import get_settings -from typing import TYPE_CHECKING +from typing import TYPE_CHECKING, Collection, SupportsIndex from .Options import YoshiColors, BowserDoor, PlayerGoal, MinigameChecks @@ -413,13 +413,13 @@ def read_bit(self, address: int, bit_number: int) -> bool: def read_byte(self, address: int) -> int: return self.buffer[address] - def read_bytes(self, startaddress: int, length: int) -> bytes: + def read_bytes(self, startaddress: int, length: int) -> bytearray: return self.buffer[startaddress:startaddress + length] def write_byte(self, address: int, value: int) -> None: self.buffer[address] = value - def write_bytes(self, startaddress: int, values: bytearray) -> None: + def write_bytes(self, startaddress: int, values: Collection[SupportsIndex]) -> None: self.buffer[startaddress:startaddress + len(values)] = values def write_to_file(self, file: str) -> None: From 58eb9cb223c002cbc9fe3961d953de2a20b530ca Mon Sep 17 00:00:00 2001 From: beauxq Date: Fri, 17 May 2024 11:43:25 -0700 Subject: [PATCH 2/2] remove `object` inheritance --- worlds/alttp/Rom.py | 2 +- worlds/dkc3/Rom.py | 2 +- worlds/yoshisisland/Rom.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/worlds/alttp/Rom.py b/worlds/alttp/Rom.py index a0121448f03..05113514e48 100644 --- a/worlds/alttp/Rom.py +++ b/worlds/alttp/Rom.py @@ -52,7 +52,7 @@ enemizer_logger = logging.getLogger("Enemizer") -class LocalRom(object): +class LocalRom: def __init__(self, file, patch=True, vanillaRom=None, name=None, hash=None): self.name = name diff --git a/worlds/dkc3/Rom.py b/worlds/dkc3/Rom.py index 91828943feb..0dc722a7386 100644 --- a/worlds/dkc3/Rom.py +++ b/worlds/dkc3/Rom.py @@ -434,7 +434,7 @@ 0x21, ] -class LocalRom(object): +class LocalRom: def __init__(self, file, patch=True, vanillaRom=None, name=None, hash=None): self.name = name diff --git a/worlds/yoshisisland/Rom.py b/worlds/yoshisisland/Rom.py index 37c4196b7c3..0943ba82514 100644 --- a/worlds/yoshisisland/Rom.py +++ b/worlds/yoshisisland/Rom.py @@ -396,7 +396,7 @@ 0x30510B: [0x14B2, 4] } -class LocalRom(object): +class LocalRom: def __init__(self, file: str) -> None: self.name = None