From 9c9fdc37901aa244d7f8fc7484cb11e1a44cd182 Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Date: Sun, 24 Nov 2024 11:53:00 +0200 Subject: [PATCH] Format with Black, remove unused imports, fix implicitly concatenated string --- docs/tutorial.ipynb | 102 ++++++++++++++++++++++----------------- setup.py | 1 - sqlite_utils/cli.py | 4 +- sqlite_utils/db.py | 45 +++++------------ sqlite_utils/utils.py | 6 +-- tests/test_cli.py | 24 ++++----- tests/test_cli_insert.py | 3 +- tests/test_cli_memory.py | 2 +- tests/test_create.py | 15 ++---- tests/test_extract.py | 3 +- tests/test_extracts.py | 2 +- 11 files changed, 95 insertions(+), 112 deletions(-) diff --git a/docs/tutorial.ipynb b/docs/tutorial.ipynb index 179f0104d..2260b1d1a 100644 --- a/docs/tutorial.ipynb +++ b/docs/tutorial.ipynb @@ -127,19 +127,21 @@ } ], "source": [ - "db[\"creatures\"].insert_all([{\n", - " \"name\": \"Cleo\",\n", - " \"species\": \"dog\",\n", - " \"age\": 6\n", - "}, {\n", - " \"name\": \"Lila\",\n", - " \"species\": \"chicken\",\n", - " \"age\": 0.8,\n", - "}, {\n", - " \"name\": \"Bants\",\n", - " \"species\": \"chicken\",\n", - " \"age\": 0.8,\n", - "}])" + "db[\"creatures\"].insert_all(\n", + " [\n", + " {\"name\": \"Cleo\", \"species\": \"dog\", \"age\": 6},\n", + " {\n", + " \"name\": \"Lila\",\n", + " \"species\": \"chicken\",\n", + " \"age\": 0.8,\n", + " },\n", + " {\n", + " \"name\": \"Bants\",\n", + " \"species\": \"chicken\",\n", + " \"age\": 0.8,\n", + " },\n", + " ]\n", + ")" ] }, { @@ -341,7 +343,9 @@ } ], "source": [ - "list(db.query(\"select * from creatures where species = :species\", {\"species\": \"chicken\"}))" + "list(\n", + " db.query(\"select * from creatures where species = :species\", {\"species\": \"chicken\"})\n", + ")" ] }, { @@ -506,22 +510,24 @@ } ], "source": [ - "db[\"creatures\"].insert_all([{\n", - " \"id\": 1,\n", - " \"name\": \"Cleo\",\n", - " \"species\": \"dog\",\n", - " \"age\": 6\n", - "}, {\n", - " \"id\": 2,\n", - " \"name\": \"Lila\",\n", - " \"species\": \"chicken\",\n", - " \"age\": 0.8,\n", - "}, {\n", - " \"id\": 3,\n", - " \"name\": \"Bants\",\n", - " \"species\": \"chicken\",\n", - " \"age\": 0.8,\n", - "}], pk=\"id\")" + "db[\"creatures\"].insert_all(\n", + " [\n", + " {\"id\": 1, \"name\": \"Cleo\", \"species\": \"dog\", \"age\": 6},\n", + " {\n", + " \"id\": 2,\n", + " \"name\": \"Lila\",\n", + " \"species\": \"chicken\",\n", + " \"age\": 0.8,\n", + " },\n", + " {\n", + " \"id\": 3,\n", + " \"name\": \"Bants\",\n", + " \"species\": \"chicken\",\n", + " \"age\": 0.8,\n", + " },\n", + " ],\n", + " pk=\"id\",\n", + ")" ] }, { @@ -575,17 +581,23 @@ } ], "source": [ - "table.insert_all([{\n", - " \"id\": 4,\n", - " \"name\": \"Azi\",\n", - " \"species\": \"chicken\",\n", - " \"age\": 0.8,\n", - "}, {\n", - " \"id\": 5,\n", - " \"name\": \"Snowy\",\n", - " \"species\": \"chicken\",\n", - " \"age\": 0.9,\n", - "}], pk=\"id\")" + "table.insert_all(\n", + " [\n", + " {\n", + " \"id\": 4,\n", + " \"name\": \"Azi\",\n", + " \"species\": \"chicken\",\n", + " \"age\": 0.8,\n", + " },\n", + " {\n", + " \"id\": 5,\n", + " \"name\": \"Snowy\",\n", + " \"species\": \"chicken\",\n", + " \"age\": 0.9,\n", + " },\n", + " ],\n", + " pk=\"id\",\n", + ")" ] }, { @@ -1006,7 +1018,9 @@ } ], "source": [ - "list(db.query(\"\"\"\n", + "list(\n", + " db.query(\n", + " \"\"\"\n", " select\n", " creatures.id,\n", " creatures.name,\n", @@ -1015,7 +1029,9 @@ " species.species\n", " from creatures\n", " join species on creatures.species_id = species.id\n", - "\"\"\"))" + "\"\"\"\n", + " )\n", + ")" ] }, { diff --git a/setup.py b/setup.py index 64cee7cb7..1ad373961 100644 --- a/setup.py +++ b/setup.py @@ -1,5 +1,4 @@ from setuptools import setup, find_packages -import io import os VERSION = "3.38" diff --git a/sqlite_utils/cli.py b/sqlite_utils/cli.py index 09eab47d6..9eac6260f 100644 --- a/sqlite_utils/cli.py +++ b/sqlite_utils/cli.py @@ -2137,9 +2137,7 @@ def search( table_columns = table_obj.columns_dict for c in column: if c not in table_columns: - raise click.ClickException( - f"Table '{dbtable}' has no column '{c}" - ) + raise click.ClickException(f"Table '{dbtable}' has no column '{c}") sql = table_obj.search_sql(columns=column, order_by=order, limit=limit) if show_sql: click.echo(sql) diff --git a/sqlite_utils/db.py b/sqlite_utils/db.py index 981d49304..2e1020777 100644 --- a/sqlite_utils/db.py +++ b/sqlite_utils/db.py @@ -28,11 +28,8 @@ cast, Any, Callable, - Dict, Union, Optional, - List, - Tuple, ) from collections.abc import Generator, Iterable import uuid @@ -590,9 +587,7 @@ def quote_fts(self, query: str) -> str: query += '"' bits = _quote_fts_re.split(query) bits = [b for b in bits if b and b != '""'] - return " ".join( - f'"{bit}"' if not bit.startswith('"') else bit for bit in bits - ) + return " ".join(f'"{bit}"' if not bit.startswith('"') else bit for bit in bits) def quote_default_value(self, value: str) -> str: if any( @@ -680,9 +675,7 @@ def supports_strict(self) -> bool: try: table_name = f"t{secrets.token_hex(16)}" with self.conn: - self.conn.execute( - f"create table {table_name} (name text) strict" - ) + self.conn.execute(f"create table {table_name} (name text) strict") self.conn.execute(f"drop table {table_name}") return True except Exception: @@ -907,9 +900,7 @@ def sort_key(p): if fk.other_column != "rowid" and not any( c for c in self[fk.other_table].columns if c.name == fk.other_column ): - raise AlterError( - f"No such column: {fk.other_table}.{fk.other_column}" - ) + raise AlterError(f"No such column: {fk.other_table}.{fk.other_column}") column_defs = [] # ensure pk is a tuple @@ -1592,9 +1583,7 @@ def indexes(self) -> list[Index]: for row in self.db.execute_returning_dicts(sql): index_name = row["name"] index_name_quoted = ( - f'"{index_name}"' - if not index_name.startswith('"') - else index_name + f'"{index_name}"' if not index_name.startswith('"') else index_name ) column_sql = f"PRAGMA index_info({index_name_quoted})" columns = [] @@ -1616,9 +1605,7 @@ def xindexes(self) -> list[XIndex]: for row in self.db.execute_returning_dicts(sql): index_name = row["name"] index_name_quoted = ( - f'"{index_name}"' - if not index_name.startswith('"') - else index_name + f'"{index_name}"' if not index_name.startswith('"') else index_name ) column_sql = f"PRAGMA index_xinfo({index_name_quoted})" index_columns = [] @@ -1971,15 +1958,11 @@ def transform_sql( sqls.append(copy_sql) # Drop (or keep) the old table if keep_table: - sqls.append( - f"ALTER TABLE [{self.name}] RENAME TO [{keep_table}];" - ) + sqls.append(f"ALTER TABLE [{self.name}] RENAME TO [{keep_table}];") else: sqls.append(f"DROP TABLE [{self.name}];") # Rename the new one - sqls.append( - f"ALTER TABLE [{new_table_name}] RENAME TO [{self.name}];" - ) + sqls.append(f"ALTER TABLE [{new_table_name}] RENAME TO [{self.name}];") # Re-add existing indexes for index in self.indexes: if index.origin != "pk": @@ -2152,9 +2135,7 @@ def create_index( suffix = None created_index_name = None while True: - created_index_name = ( - f"{index_name}_{suffix}" if suffix else index_name - ) + created_index_name = f"{index_name}_{suffix}" if suffix else index_name sql = ( textwrap.dedent( """ @@ -2510,9 +2491,7 @@ def populate_fts(self, columns: Iterable[str]) -> "Table": """ ) .strip() - .format( - table=self.name, columns=", ".join(f"[{c}]" for c in columns) - ) + .format(table=self.name, columns=", ".join(f"[{c}]" for c in columns)) ) self.db.executescript(sql) return self @@ -3677,9 +3656,9 @@ def truncate(value): most_common_results = None least_common_results = None if num_distinct == 1: - value = db.execute( - f"select [{column}] from [{table}] limit 1" - ).fetchone()[0] + value = db.execute(f"select [{column}] from [{table}] limit 1").fetchone()[ + 0 + ] most_common_results = [(truncate(value), total_rows)] elif num_distinct != total_rows: if most_common: diff --git a/sqlite_utils/utils.py b/sqlite_utils/utils.py index 2f2aaaa42..106b5ae1f 100644 --- a/sqlite_utils/utils.py +++ b/sqlite_utils/utils.py @@ -9,7 +9,7 @@ import os import sys from . import recipes -from typing import Dict, cast, BinaryIO, Optional, Tuple, Type +from typing import cast, BinaryIO, Optional from collections.abc import Iterable import click @@ -226,9 +226,7 @@ def _extra_key_strategy( yield row elif not extras_key: extras = row.pop(None) # type: ignore - raise RowError( - f"Row {row} contained these extra values: {extras}" - ) + raise RowError(f"Row {row} contained these extra values: {extras}") else: row[extras_key] = row.pop(None) # type: ignore yield row diff --git a/tests/test_cli.py b/tests/test_cli.py index 7b548524a..ace75c375 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -845,14 +845,14 @@ def test_query_json_binary(db_path): "data": { "$base64": True, "encoded": ( - "eJzt0c1xAyEMBeC7q1ABHleR3HxNAQrIjmb4M0gelx+RTY7p4N2WBYT0vmufUknH" - "8kq5lz5pqRFXsTOl3pYkE/NJnHXoStruJEVjc0mOCyTqq/ZMJnXEZW1Js2ZvRm5U+" - "DPKk9hRWqjyvTFx0YfzhT6MpGmN2lR1fzxjyfVMD9dFrS+bnkleMpMam/ZGXgrX1I" - "/K+5Au3S/9lNQRh0k4Gq/RUz8GiKfsQm+7JLsJ6fTo5JhVG00ZU76kZZkxePx49uI" - "jnpNoJyYlWUsoaSl/CcVATje/Kxu13RANnrHweaH3V5Jh4jvGyKCnxJLiXPKhmW3f" - "iCnG7Jql7RR3UvFo8jJ4z039dtOkTFmWzL1be9lt8A5II471m6vXy+l0BR/4wAc+8" - "IEPfOADH/jABz7wgQ984AMf+MAHPvCBD3zgAx/4wAc+8IEPfOADH/jABz7wgQ984A" - "Mf+MAHPvCBD3zgAx/4wAc+8IEPfOADH/jABz7wgQ984PuP7xubBoN9" + "eJzt0c1xAyEMBeC7q1ABHleR3HxNAQrIjmb4M0gelx+RTY7p4N2WBYT0vmufUknH" + "8kq5lz5pqRFXsTOl3pYkE/NJnHXoStruJEVjc0mOCyTqq/ZMJnXEZW1Js2ZvRm5U+" + "DPKk9hRWqjyvTFx0YfzhT6MpGmN2lR1fzxjyfVMD9dFrS+bnkleMpMam/ZGXgrX1I" + "/K+5Au3S/9lNQRh0k4Gq/RUz8GiKfsQm+7JLsJ6fTo5JhVG00ZU76kZZkxePx49uI" + "jnpNoJyYlWUsoaSl/CcVATje/Kxu13RANnrHweaH3V5Jh4jvGyKCnxJLiXPKhmW3f" + "iCnG7Jql7RR3UvFo8jJ4z039dtOkTFmWzL1be9lt8A5II471m6vXy+l0BR/4wAc+8" + "IEPfOADH/jABz7wgQ984AMf+MAHPvCBD3zgAx/4wAc+8IEPfOADH/jABz7wgQ984A" + "Mf+MAHPvCBD3zgAx/4wAc+8IEPfOADH/jABz7wgQ984PuP7xubBoN9" ), }, } @@ -1171,12 +1171,12 @@ def test_upsert_alter(db_path, tmpdir): # Not null: ( ["name", "text", "--not-null", "name"], - ("CREATE TABLE [t] (\n" " [name] TEXT NOT NULL\n" ")"), + ("CREATE TABLE [t] (\n [name] TEXT NOT NULL\n)"), ), # Default: ( ["age", "integer", "--default", "age", "3"], - ("CREATE TABLE [t] (\n" " [age] INTEGER DEFAULT '3'\n" ")"), + ("CREATE TABLE [t] (\n [age] INTEGER DEFAULT '3'\n)"), ), # Compound primary key ( @@ -2052,7 +2052,7 @@ def test_triggers(tmpdir, extra_args, expected): ), ( ["dogs"], - ("CREATE TABLE [dogs] (\n" " [id] INTEGER,\n" " [name] TEXT\n" ")\n"), + ("CREATE TABLE [dogs] (\n [id] INTEGER,\n [name] TEXT\n)\n"), ), ( ["chickens", "dogs"], @@ -2328,7 +2328,7 @@ def test_rename_table(tmpdir): ) assert result_error.exit_code == 1 assert result_error.output == ( - 'Error: Table "missing" could not be renamed. ' "no such table: missing\n" + 'Error: Table "missing" could not be renamed. no such table: missing\n' ) # And check --ignore works result_error2 = CliRunner().invoke( diff --git a/tests/test_cli_insert.py b/tests/test_cli_insert.py index 954611395..0700a086f 100644 --- a/tests/test_cli_insert.py +++ b/tests/test_cli_insert.py @@ -140,8 +140,7 @@ def test_insert_multiple_with_compound_primary_key(db_path, tmpdir): def test_insert_not_null_default(db_path, tmpdir): json_path = str(tmpdir / "dogs.json") dogs = [ - {"id": i, "name": f"Cleo {i}", "age": i + 3, "score": 10} - for i in range(1, 21) + {"id": i, "name": f"Cleo {i}", "age": i + 3, "score": 10} for i in range(1, 21) ] with open(json_path, "w") as fp: fp.write(json.dumps(dogs)) diff --git a/tests/test_cli_memory.py b/tests/test_cli_memory.py index 9e3f8ac8c..3cffda525 100644 --- a/tests/test_cli_memory.py +++ b/tests/test_cli_memory.py @@ -118,7 +118,7 @@ def test_memory_json_nl(tmpdir, use_stdin): @pytest.mark.parametrize("use_stdin", (True, False)) def test_memory_csv_encoding(tmpdir, use_stdin): latin1_csv = ( - b"date,name,latitude,longitude\n" b"2020-03-04,S\xe3o Paulo,-23.561,-46.645\n" + b"date,name,latitude,longitude\n2020-03-04,S\xe3o Paulo,-23.561,-46.645\n" ) input = None if use_stdin: diff --git a/tests/test_create.py b/tests/test_create.py index a64a4e459..5e173ae12 100644 --- a/tests/test_create.py +++ b/tests/test_create.py @@ -710,10 +710,7 @@ def test_columns_not_in_first_record_should_not_cause_batch_to_be_too_large(fres records = [ {"c0": "first record"}, # one column in first record -> batch size = 999 # fill out the batch with 99 records with enough columns to exceed THRESHOLD - *[ - {f"c{i}": j for i in range(extra_columns)} - for j in range(batch_size - 1) - ], + *[{f"c{i}": j for i in range(extra_columns)} for j in range(batch_size - 1)], ] try: fresh_db["too_many_columns"].insert_all( @@ -810,7 +807,7 @@ def test_create_index_desc(fresh_db): "select sql from sqlite_master where name='idx_dogs_age_name'" ).fetchone()[0] assert sql == ( - "CREATE INDEX [idx_dogs_age_name]\n" " ON [dogs] ([age] desc, [name])" + "CREATE INDEX [idx_dogs_age_name]\n ON [dogs] ([age] desc, [name])" ) @@ -889,9 +886,7 @@ def test_insert_memoryview(fresh_db): def test_insert_thousands_using_generator(fresh_db): - fresh_db["test"].insert_all( - {"i": i, "word": f"word_{i}"} for i in range(10000) - ) + fresh_db["test"].insert_all({"i": i, "word": f"word_{i}"} for i in range(10000)) assert [{"name": "i", "type": "INTEGER"}, {"name": "word", "type": "TEXT"}] == [ {"name": col.name, "type": col.type} for col in fresh_db["test"].columns ] @@ -1228,7 +1223,7 @@ def test_create_replace(fresh_db): fresh_db["t"].create({"id": int}) # This should not fresh_db["t"].create({"name": str}, replace=True) - assert fresh_db["t"].schema == ("CREATE TABLE [t] (\n" " [name] TEXT\n" ")") + assert fresh_db["t"].schema == ("CREATE TABLE [t] (\n [name] TEXT\n)") @pytest.mark.parametrize( @@ -1352,7 +1347,7 @@ def test_insert_upsert_strict(fresh_db, method_name, strict): def test_create_table_strict(fresh_db, strict): table = fresh_db.create_table("t", {"id": int, "f": float}, strict=strict) assert table.strict == strict or not fresh_db.supports_strict - expected_schema = "CREATE TABLE [t] (\n" " [id] INTEGER,\n" " [f] FLOAT\n" ")" + expected_schema = "CREATE TABLE [t] (\n [id] INTEGER,\n [f] FLOAT\n)" if strict and not fresh_db.supports_strict: return if strict: diff --git a/tests/test_extract.py b/tests/test_extract.py index fb80e87f3..adbb03860 100644 --- a/tests/test_extract.py +++ b/tests/test_extract.py @@ -31,8 +31,7 @@ def test_extract_single_column(fresh_db, table, fk_column): + ")" ) assert fresh_db[expected_table].schema == ( - f"CREATE TABLE [{expected_table}] (\n" - + " [id] INTEGER PRIMARY KEY,\n" + f"CREATE TABLE [{expected_table}] (\n [id] INTEGER PRIMARY KEY,\n" " [species] TEXT\n" ")" ) diff --git a/tests/test_extracts.py b/tests/test_extracts.py index f36f6e27a..e30b0d668 100644 --- a/tests/test_extracts.py +++ b/tests/test_extracts.py @@ -25,7 +25,7 @@ def test_extracts(fresh_db, kwargs, expected_table, use_table_factory): {"id": 2, "species_id": "Oak"}, {"id": 3, "species_id": "Palm"}, ], - **insert_kwargs + **insert_kwargs, ) # Should now have two tables: Trees and Species assert {expected_table, "Trees"} == set(fresh_db.table_names())