refactor: update return types to Union[str, None] for better type checking

This commit is contained in:
2025-07-04 08:22:42 +02:00
parent 426c12f5cb
commit 09a2a3278b

View File

@@ -1,5 +1,6 @@
import sqlite3 as sql import sqlite3 as sql
from src.utils import SignatureType from src.utils import SignatureType
from typing import Union
MA_TABLE = """ MA_TABLE = """
CREATE TABLE IF NOT EXISTS ma ( CREATE TABLE IF NOT EXISTS ma (
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,
@@ -34,7 +35,7 @@ class Database:
cursor.execute(MC_TABLE) cursor.execute(MC_TABLE)
self.connection.commit() self.connection.commit()
def create_new_signature(self, table:str)->str: def create_new_signature(self, table: str) -> Union[str, None]:
"""Create a new signature for the given table. """Create a new signature for the given table.
The new signature is generated by using 01 / as prefix, then incrementing the number by 1 for each new entry.""" The new signature is generated by using 01 / as prefix, then incrementing the number by 1 for each new entry."""
cursor = self.connection.cursor() cursor = self.connection.cursor()
@@ -51,7 +52,7 @@ class Database:
count = cursor.fetchone()[0] count = cursor.fetchone()[0]
return count > 0 return count > 0
def insert(self, old_signature: str) -> str | None: def insert(self, old_signature: str) -> Union[str, None]:
table = old_signature.split(" ")[0].lower() table = old_signature.split(" ")[0].lower()
if table == "ma": if table == "ma":
return self.insert_ma(old_signature) return self.insert_ma(old_signature)
@@ -63,7 +64,7 @@ class Database:
# print(f"Error: Unknown signature type '{table}'. Please use 'ma', 'mb', or 'mc'.") # print(f"Error: Unknown signature type '{table}'. Please use 'ma', 'mb', or 'mc'.")
return self.insert_ma(old_signature) return self.insert_ma(old_signature)
def get_signature(self, old_signature: str, table: str) -> str | None: def get_signature(self, old_signature: str, table: str) -> Union[str, None]:
"""Get the new signature for the given old signature from the specified table.""" """Get the new signature for the given old signature from the specified table."""
cursor = self.connection.cursor() cursor = self.connection.cursor()
cursor.execute( cursor.execute(
@@ -79,7 +80,7 @@ class Database:
) )
return None return None
def insert_ma(self, old_signature: str) -> str|None: def insert_ma(self, old_signature: str) -> Union[str, None]:
"""Insert an old signature into the ma table and return the new signature. """Insert an old signature into the ma table and return the new signature.
New Signatures is generated by using 01 / as prefix, then incrementing the number by 1 for each new entry.""" New Signatures is generated by using 01 / as prefix, then incrementing the number by 1 for each new entry."""
if self.signature_exists(old_signature, "ma"): if self.signature_exists(old_signature, "ma"):
@@ -96,8 +97,7 @@ class Database:
print(f"Error: The combination of old_signature '{old_signature}' and new_signature '{new_signature}' already exists.") print(f"Error: The combination of old_signature '{old_signature}' and new_signature '{new_signature}' already exists.")
return None return None
def insert_mb(self, old_signature: str) -> Union[str, None]:
def insert_mb(self, old_signature: str) -> str:
"""Insert an old signature into the mb table and return the new signature. """Insert an old signature into the mb table and return the new signature.
New Signatures is generated by using 02 / as prefix, then incrementing the number by 1 for each new entry.""" New Signatures is generated by using 02 / as prefix, then incrementing the number by 1 for each new entry."""
new_signature = self.create_new_signature("mb") new_signature = self.create_new_signature("mb")
@@ -115,8 +115,8 @@ class Database:
except sql.IntegrityError: except sql.IntegrityError:
print(f"Error: The combination of old_signature '{old_signature}' and new_signature '{new_signature}' already exists.") print(f"Error: The combination of old_signature '{old_signature}' and new_signature '{new_signature}' already exists.")
return None return None
def insert_mc(self, old_signature: str) -> str: def insert_mc(self, old_signature: str) -> Union[str, None]:
"""Insert an old signature into the mc table and return the new signature. """Insert an old signature into the mc table and return the new signature.
New Signatures is generated by using 03 / as prefix, then incrementing the number by 1 for each new entry.""" New Signatures is generated by using 03 / as prefix, then incrementing the number by 1 for each new entry."""
new_signature = self.create_new_signature("mc") new_signature = self.create_new_signature("mc")