Skip to content

Commit 2ca2ff1

Browse files
committed
Fix all mypy errors
1 parent 79ab33c commit 2ca2ff1

File tree

15 files changed

+77
-75
lines changed

15 files changed

+77
-75
lines changed

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ classifiers = [
2323
]
2424
dependencies = [
2525
"pycryptodome",
26-
"requests"
26+
"requests",
27+
"types-requests"
2728
]
2829
keywords = ["Wii", "wii"]
2930

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
build
22
pycryptodome
33
requests
4+
types-requests
45
sphinx
56
sphinx-book-theme
67
myst-parser

src/libWiiPy/archive/ash.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
# See <link pending> for details about the ASH compression format.
99

1010
import io
11-
from dataclasses import dataclass as _dataclass
11+
from dataclasses import dataclass
12+
from typing import List
1213

1314

14-
@_dataclass
15+
@dataclass
1516
class _ASHBitReader:
1617
"""
1718
An _ASHBitReader class used to parse individual words in an ASH file. Private class used by the ASH module.
@@ -93,7 +94,7 @@ def _ash_bit_reader_read_bits(bit_reader: _ASHBitReader, num_bits: int):
9394
return bits
9495

9596

96-
def _ash_read_tree(bit_reader: _ASHBitReader, width: int, left_tree: [int], right_tree: [int]):
97+
def _ash_read_tree(bit_reader: _ASHBitReader, width: int, left_tree: List[int], right_tree: List[int]):
9798
# Read either the symbol or distance tree from the ASH file, and return the root of that tree.
9899
work = [0] * (2 * (1 << width))
99100
work_pos = 0

src/libWiiPy/archive/lz77.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import io
77
from dataclasses import dataclass as _dataclass
8-
from typing import List as _List
8+
from typing import List, Tuple
99

1010

1111
_LZ_MIN_DISTANCE = 0x01 # Minimum distance for each reference.
@@ -21,7 +21,7 @@ class _LZNode:
2121
weight: int = 0
2222

2323

24-
def _compress_compare_bytes(buffer: _List[int], offset1: int, offset2: int, abs_len_max: int) -> int:
24+
def _compress_compare_bytes(buffer: List[int], offset1: int, offset2: int, abs_len_max: int) -> int:
2525
# Compare bytes up to the maximum length we can match. Start by comparing the first 3 bytes, since that's the
2626
# minimum match length and this allows for a more optimized early exit.
2727
num_matched = 0
@@ -32,7 +32,7 @@ def _compress_compare_bytes(buffer: _List[int], offset1: int, offset2: int, abs_
3232
return num_matched
3333

3434

35-
def _compress_search_matches_optimized(buffer: _List[int], pos: int) -> (int, int):
35+
def _compress_search_matches_optimized(buffer: List[int], pos: int) -> Tuple[int, int]:
3636
bytes_left = len(buffer) - pos
3737
global _LZ_MAX_DISTANCE, _LZ_MIN_LENGTH, _LZ_MAX_LENGTH, _LZ_MIN_DISTANCE
3838
# Default to only looking back 4096 bytes, unless we've moved fewer than 4096 bytes, in which case we should
@@ -54,7 +54,7 @@ def _compress_search_matches_optimized(buffer: _List[int], pos: int) -> (int, in
5454
return biggest_match, biggest_match_pos
5555

5656

57-
def _compress_search_matches_greedy(buffer: _List[int], pos: int) -> (int, int):
57+
def _compress_search_matches_greedy(buffer: List[int], pos: int) -> Tuple[int, int]:
5858
# Finds and returns the first valid match, rather that finding the best one.
5959
bytes_left = len(buffer) - pos
6060
global _LZ_MAX_DISTANCE, _LZ_MAX_LENGTH, _LZ_MIN_DISTANCE

src/libWiiPy/archive/u8.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,16 @@ def __init__(self):
6868
self.root_node: _U8Node = _U8Node(0, 0, 0, 0)
6969
self.imet_header: IMETHeader = IMETHeader()
7070

71-
def load(self, u8_data: bytes) -> None:
71+
def load(self, u8: bytes) -> None:
7272
"""
7373
Loads raw U8 data into a new U8 object. This allows for extracting the file and updating its contents.
7474
7575
Parameters
7676
----------
77-
u8_data : bytes
77+
u8 : bytes
7878
The data for the U8 file to load.
7979
"""
80-
with io.BytesIO(u8_data) as u8_data:
80+
with io.BytesIO(u8) as u8_data:
8181
# Read the first 4 bytes of the file to ensure that it's a U8 archive.
8282
u8_data.seek(0x0)
8383
self.u8_magic = u8_data.read(4)
@@ -126,7 +126,7 @@ def load(self, u8_data: bytes) -> None:
126126
# Seek back before the root node so that it gets read with all the rest.
127127
u8_data.seek(u8_data.tell() - 12)
128128
# Iterate over the number of nodes that the root node lists.
129-
for node in range(root_node_size):
129+
for _ in range(root_node_size):
130130
node_type = int.from_bytes(u8_data.read(1))
131131
node_name_offset = int.from_bytes(u8_data.read(3))
132132
node_data_offset = int.from_bytes(u8_data.read(4))
@@ -160,7 +160,7 @@ def dump(self) -> bytes:
160160
# This is 0 because the header size DOES NOT include the initial 32 bytes describing the file.
161161
header_size = 0
162162
# Add 12 bytes for each node, since that's how many bytes each one is made up of.
163-
for node in range(len(self.u8_node_list)):
163+
for _ in range(len(self.u8_node_list)):
164164
header_size += 12
165165
# Add the number of bytes used for each file/folder name in the string table.
166166
for file_name in self.file_name_list:
@@ -170,13 +170,13 @@ def dump(self) -> bytes:
170170
# Adjust all nodes to place file data in the same order as the nodes. Why isn't it already like this?
171171
current_data_offset = data_offset
172172
current_name_offset = 0
173-
for node in range(len(self.u8_node_list)):
174-
if self.u8_node_list[node].type == 0:
175-
self.u8_node_list[node].data_offset = _align_value(current_data_offset, 32)
176-
current_data_offset += _align_value(self.u8_node_list[node].size, 32)
173+
for idx in range(len(self.u8_node_list)):
174+
if self.u8_node_list[idx].type == 0:
175+
self.u8_node_list[idx].data_offset = _align_value(current_data_offset, 32)
176+
current_data_offset += _align_value(self.u8_node_list[idx].size, 32)
177177
# Calculate the name offsets, including the extra 1 for the NULL byte at the end of each name.
178-
self.u8_node_list[node].name_offset = current_name_offset
179-
current_name_offset += len(self.file_name_list[node]) + 1
178+
self.u8_node_list[idx].name_offset = current_name_offset
179+
current_name_offset += len(self.file_name_list[idx]) + 1
180180
# Begin joining all the U8 archive data into bytes.
181181
u8_data = b''
182182
# Magic number.
@@ -300,7 +300,7 @@ def _pack_u8_dir(u8_archive: U8Archive, current_path, node_count, parent_node):
300300
return u8_archive, node_count
301301

302302

303-
def pack_u8(input_path, generate_imet=False, imet_titles:List[str]=None) -> bytes:
303+
def pack_u8(input_path, generate_imet=False, imet_titles:List[str] | None = None) -> bytes:
304304
"""
305305
Packs the provided file or folder into a new U8 archive, and returns the raw file data for it.
306306
@@ -513,13 +513,15 @@ def get_channel_names(self, target_languages: int | List[int]) -> str | List[str
513513
raise ValueError(f"The specified language is not valid!")
514514
return self.channel_names[target_languages]
515515
# If multiple channel names were requested.
516-
else:
516+
elif type(target_languages) == List:
517517
channel_names = []
518518
for lang in target_languages:
519519
if lang not in self.LocalizedTitles:
520520
raise ValueError(f"The specified language at index {target_languages.index(lang)} is not valid!")
521521
channel_names.append(self.channel_names[lang])
522522
return channel_names
523+
else:
524+
raise TypeError("Target languages must be type int or List[int]!")
523525

524526
def set_channel_names(self, channel_names: Tuple[int, str] | List[Tuple[int, str]]) -> None:
525527
"""
@@ -544,7 +546,7 @@ def set_channel_names(self, channel_names: Tuple[int, str] | List[Tuple[int, str
544546
f"42 characters!")
545547
self.channel_names[channel_names[0]] = channel_names[1]
546548
# If a list of channel names was provided.
547-
else:
549+
elif type(channel_names) == list:
548550
for name in channel_names:
549551
if name[0] not in self.LocalizedTitles:
550552
raise ValueError(f"The target language \"{name[0]}\" for the name at index {channel_names.index(name)} "
@@ -553,3 +555,5 @@ def set_channel_names(self, channel_names: Tuple[int, str] | List[Tuple[int, str
553555
raise ValueError(f"The channel name \"{name[1]}\" at index {channel_names.index(name)} is too long! "
554556
f"Channel names cannot exceed 42 characters!")
555557
self.channel_names[name[0]] = name[1]
558+
else:
559+
raise TypeError("Channel names must be type Tuple[int, str] or List[Tuple[int, str]]!")

src/libWiiPy/nand/emunand.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import pathlib
88
import shutil
99
from dataclasses import dataclass as _dataclass
10-
from typing import List
10+
from typing import Callable, List
1111
from ..title.ticket import Ticket
1212
from ..title.title import Title
1313
from ..title.tmd import TMD
@@ -32,7 +32,7 @@ class EmuNAND:
3232
emunand_root : pathlib.Path
3333
The path to the EmuNAND root directory.
3434
"""
35-
def __init__(self, emunand_root: str | pathlib.Path, callback: callable = None):
35+
def __init__(self, emunand_root: str | pathlib.Path, callback: Callable | None = None):
3636
self.emunand_root = pathlib.Path(emunand_root)
3737
self.log = callback if callback is not None else None
3838

src/libWiiPy/nand/setting.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# See https://wiibrew.org/wiki//title/00000001/00000002/data/setting.txt for information about setting.txt.
55

66
import io
7+
from typing import List
78
from ..shared import _pad_bytes
89

910

@@ -53,16 +54,16 @@ def load(self, setting_txt: bytes) -> None:
5354
"""
5455
with io.BytesIO(setting_txt) as setting_data:
5556
global _key # I still don't actually know what *kind* of encryption this is.
56-
setting_txt_dec: [int] = []
57+
setting_txt_dec: List[int] = []
5758
for i in range(0, 256):
5859
setting_txt_dec.append(int.from_bytes(setting_data.read(1)) ^ (_key & 0xff))
5960
_key = (_key << 1) | (_key >> 31)
60-
setting_txt_dec = bytes(setting_txt_dec)
61+
setting_txt_bytes = bytes(setting_txt_dec)
6162
try:
62-
setting_str = setting_txt_dec.decode('utf-8')
63+
setting_str = setting_txt_bytes.decode('utf-8')
6364
except UnicodeDecodeError:
64-
last_newline_pos = setting_txt_dec.rfind(b'\n') # This makes sure we don't try to decode any garbage data.
65-
setting_str = setting_txt_dec[:last_newline_pos + 1].decode('utf-8')
65+
last_newline_pos = setting_txt_bytes.rfind(b'\n') # This makes sure we don't try to decode any garbage data.
66+
setting_str = setting_txt_bytes[:last_newline_pos + 1].decode('utf-8')
6667
self.load_decrypted(setting_str)
6768

6869
def load_decrypted(self, setting_txt: str) -> None:
@@ -104,13 +105,13 @@ def dump(self) -> bytes:
104105
setting_txt_dec = setting_str.encode()
105106
global _key
106107
# This could probably be made more efficient somehow.
107-
setting_txt_enc: [int] = []
108+
setting_txt_enc: List[int] = []
108109
with io.BytesIO(setting_txt_dec) as setting_data:
109110
for i in range(0, len(setting_txt_dec)):
110111
setting_txt_enc.append(int.from_bytes(setting_data.read(1)) ^ (_key & 0xff))
111112
_key = (_key << 1) | (_key >> 31)
112-
setting_txt_enc = _pad_bytes(bytes(setting_txt_enc), 256)
113-
return setting_txt_enc
113+
setting_txt_bytes = _pad_bytes(bytes(setting_txt_enc), 256)
114+
return setting_txt_bytes
114115

115116
def dump_decrypted(self) -> str:
116117
"""

src/libWiiPy/nand/sys.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,8 @@ def add(self, title_id: str | bytes) -> int:
9191
The UID assigned to the new Title ID.
9292
"""
9393
if type(title_id) is bytes:
94-
# This catches the format b'0000000100000002'
95-
if len(title_id) == 16:
96-
title_id_converted = title_id.encode()
9794
# This catches the format b'\x00\x00\x00\x01\x00\x00\x00\x02'
98-
elif len(title_id) == 8:
95+
if len(title_id) == 8:
9996
title_id_converted = binascii.hexlify(title_id).decode()
10097
# If it isn't one of those lengths, it cannot possibly be valid, so reject it.
10198
else:

src/libWiiPy/py.typed

Whitespace-only changes.

src/libWiiPy/title/cert.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ class Certificate:
6161
The exponent of this certificate's public key. Combined with the modulus to get the full key.
6262
"""
6363
def __init__(self):
64-
self.type: CertificateType | None = None
64+
self.type: CertificateType = CertificateType.RSA_4096
6565
self.signature: bytes = b''
6666
self.issuer: str = ""
67-
self.pub_key_type: CertificateKeyType | None = None
67+
self.pub_key_type: CertificateKeyType = CertificateKeyType.RSA_4096
6868
self.child_name: str = ""
6969
self.pub_key_id: int = 0
7070
self.pub_key_modulus: int = 0

0 commit comments

Comments
 (0)