Better type annotations for simple_upsert_txn

most of these params don't really need to be lists.
This commit is contained in:
Richard van der Hoff 2020-05-06 01:08:15 +01:00
parent b2dba06079
commit 0f6ebf393d
1 changed files with 43 additions and 30 deletions

View File

@ -49,6 +49,7 @@ from synapse.metrics.background_process_metrics import run_as_background_process
from synapse.storage.background_updates import BackgroundUpdater from synapse.storage.background_updates import BackgroundUpdater
from synapse.storage.engines import BaseDatabaseEngine, PostgresEngine, Sqlite3Engine from synapse.storage.engines import BaseDatabaseEngine, PostgresEngine, Sqlite3Engine
from synapse.storage.types import Connection, Cursor from synapse.storage.types import Connection, Cursor
from synapse.types import Collection
from synapse.util.stringutils import exception_to_unicode from synapse.util.stringutils import exception_to_unicode
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -889,20 +890,24 @@ class Database(object):
txn.execute(sql, list(allvalues.values())) txn.execute(sql, list(allvalues.values()))
def simple_upsert_many_txn( def simple_upsert_many_txn(
self, txn, table, key_names, key_values, value_names, value_values self,
): txn: LoggingTransaction,
table: str,
key_names: Collection[str],
key_values: Collection[Iterable[Any]],
value_names: Collection[str],
value_values: Iterable[Iterable[str]],
) -> None:
""" """
Upsert, many times. Upsert, many times.
Args: Args:
table (str): The table to upsert into table: The table to upsert into
key_names (list[str]): The key column names. key_names: The key column names.
key_values (list[list]): A list of each row's key column values. key_values: A list of each row's key column values.
value_names (list[str]): The value column names. If empty, no value_names: The value column names
values will be used, even if value_values is provided. value_values: A list of each row's value column values.
value_values (list[list]): A list of each row's value column values. Ignored if value_names is empty.
Returns:
None
""" """
if self.engine.can_native_upsert and table not in self._unsafe_to_upsert_tables: if self.engine.can_native_upsert and table not in self._unsafe_to_upsert_tables:
return self.simple_upsert_many_txn_native_upsert( return self.simple_upsert_many_txn_native_upsert(
@ -914,20 +919,24 @@ class Database(object):
) )
def simple_upsert_many_txn_emulated( def simple_upsert_many_txn_emulated(
self, txn, table, key_names, key_values, value_names, value_values self,
): txn: LoggingTransaction,
table: str,
key_names: Iterable[str],
key_values: Collection[Iterable[Any]],
value_names: Collection[str],
value_values: Iterable[Iterable[str]],
) -> None:
""" """
Upsert, many times, but without native UPSERT support or batching. Upsert, many times, but without native UPSERT support or batching.
Args: Args:
table (str): The table to upsert into table: The table to upsert into
key_names (list[str]): The key column names. key_names: The key column names.
key_values (list[list]): A list of each row's key column values. key_values: A list of each row's key column values.
value_names (list[str]): The value column names. If empty, no value_names: The value column names
values will be used, even if value_values is provided. value_values: A list of each row's value column values.
value_values (list[list]): A list of each row's value column values. Ignored if value_names is empty.
Returns:
None
""" """
# No value columns, therefore make a blank list so that the following # No value columns, therefore make a blank list so that the following
# zip() works correctly. # zip() works correctly.
@ -941,20 +950,24 @@ class Database(object):
self.simple_upsert_txn_emulated(txn, table, _keys, _vals) self.simple_upsert_txn_emulated(txn, table, _keys, _vals)
def simple_upsert_many_txn_native_upsert( def simple_upsert_many_txn_native_upsert(
self, txn, table, key_names, key_values, value_names, value_values self,
): txn: LoggingTransaction,
table: str,
key_names: Collection[str],
key_values: Collection[Iterable[Any]],
value_names: Collection[str],
value_values: Iterable[Iterable[Any]],
) -> None:
""" """
Upsert, many times, using batching where possible. Upsert, many times, using batching where possible.
Args: Args:
table (str): The table to upsert into table: The table to upsert into
key_names (list[str]): The key column names. key_names: The key column names.
key_values (list[list]): A list of each row's key column values. key_values: A list of each row's key column values.
value_names (list[str]): The value column names. If empty, no value_names: The value column names
values will be used, even if value_values is provided. value_values: A list of each row's value column values.
value_values (list[list]): A list of each row's value column values. Ignored if value_names is empty.
Returns:
None
""" """
allnames = [] # type: List[str] allnames = [] # type: List[str]
allnames.extend(key_names) allnames.extend(key_names)