From 46be4f8cff11ae0163953cdbf89d0864c50fac2e Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Thu, 26 Aug 2021 21:32:44 -0700 Subject: [PATCH] Improve normalize_db_row by making it more strict on types. --- ycdl/objects.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/ycdl/objects.py b/ycdl/objects.py index b630c37..a98ebb3 100644 --- a/ycdl/objects.py +++ b/ycdl/objects.py @@ -4,10 +4,19 @@ from . import constants from . import exceptions from . import ytrss -def normalize_db_row(db_row, table): +def normalize_db_row(db_row, table) -> dict: + ''' + Raises KeyError if table is not one of the recognized tables. + + Raises TypeError if db_row is not the right type. + ''' + if isinstance(db_row, dict): + return db_row + if isinstance(db_row, (list, tuple)): - db_row = dict(zip(constants.SQL_COLUMNS[table], db_row)) - return db_row + return dict(zip(constants.SQL_COLUMNS[table], db_row)) + + raise TypeError(f'db_row should be {dict}, {list}, or {tuple}, not {type(db_row)}.') class Base: def __init__(self, ycdldb):