Format with Black, remove unused imports, fix implicitly concatenated string

This commit is contained in:
Hugo van Kemenade 2024-11-24 11:53:00 +02:00
commit 9c9fdc3790
11 changed files with 95 additions and 112 deletions

View file

@ -127,19 +127,21 @@
} }
], ],
"source": [ "source": [
"db[\"creatures\"].insert_all([{\n", "db[\"creatures\"].insert_all(\n",
" \"name\": \"Cleo\",\n", " [\n",
" \"species\": \"dog\",\n", " {\"name\": \"Cleo\", \"species\": \"dog\", \"age\": 6},\n",
" \"age\": 6\n", " {\n",
"}, {\n", " \"name\": \"Lila\",\n",
" \"name\": \"Lila\",\n", " \"species\": \"chicken\",\n",
" \"species\": \"chicken\",\n", " \"age\": 0.8,\n",
" \"age\": 0.8,\n", " },\n",
"}, {\n", " {\n",
" \"name\": \"Bants\",\n", " \"name\": \"Bants\",\n",
" \"species\": \"chicken\",\n", " \"species\": \"chicken\",\n",
" \"age\": 0.8,\n", " \"age\": 0.8,\n",
"}])" " },\n",
" ]\n",
")"
] ]
}, },
{ {
@ -341,7 +343,9 @@
} }
], ],
"source": [ "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": [ "source": [
"db[\"creatures\"].insert_all([{\n", "db[\"creatures\"].insert_all(\n",
" \"id\": 1,\n", " [\n",
" \"name\": \"Cleo\",\n", " {\"id\": 1, \"name\": \"Cleo\", \"species\": \"dog\", \"age\": 6},\n",
" \"species\": \"dog\",\n", " {\n",
" \"age\": 6\n", " \"id\": 2,\n",
"}, {\n", " \"name\": \"Lila\",\n",
" \"id\": 2,\n", " \"species\": \"chicken\",\n",
" \"name\": \"Lila\",\n", " \"age\": 0.8,\n",
" \"species\": \"chicken\",\n", " },\n",
" \"age\": 0.8,\n", " {\n",
"}, {\n", " \"id\": 3,\n",
" \"id\": 3,\n", " \"name\": \"Bants\",\n",
" \"name\": \"Bants\",\n", " \"species\": \"chicken\",\n",
" \"species\": \"chicken\",\n", " \"age\": 0.8,\n",
" \"age\": 0.8,\n", " },\n",
"}], pk=\"id\")" " ],\n",
" pk=\"id\",\n",
")"
] ]
}, },
{ {
@ -575,17 +581,23 @@
} }
], ],
"source": [ "source": [
"table.insert_all([{\n", "table.insert_all(\n",
" \"id\": 4,\n", " [\n",
" \"name\": \"Azi\",\n", " {\n",
" \"species\": \"chicken\",\n", " \"id\": 4,\n",
" \"age\": 0.8,\n", " \"name\": \"Azi\",\n",
"}, {\n", " \"species\": \"chicken\",\n",
" \"id\": 5,\n", " \"age\": 0.8,\n",
" \"name\": \"Snowy\",\n", " },\n",
" \"species\": \"chicken\",\n", " {\n",
" \"age\": 0.9,\n", " \"id\": 5,\n",
"}], pk=\"id\")" " \"name\": \"Snowy\",\n",
" \"species\": \"chicken\",\n",
" \"age\": 0.9,\n",
" },\n",
" ],\n",
" pk=\"id\",\n",
")"
] ]
}, },
{ {
@ -1006,7 +1018,9 @@
} }
], ],
"source": [ "source": [
"list(db.query(\"\"\"\n", "list(\n",
" db.query(\n",
" \"\"\"\n",
" select\n", " select\n",
" creatures.id,\n", " creatures.id,\n",
" creatures.name,\n", " creatures.name,\n",
@ -1015,7 +1029,9 @@
" species.species\n", " species.species\n",
" from creatures\n", " from creatures\n",
" join species on creatures.species_id = species.id\n", " join species on creatures.species_id = species.id\n",
"\"\"\"))" "\"\"\"\n",
" )\n",
")"
] ]
}, },
{ {

View file

@ -1,5 +1,4 @@
from setuptools import setup, find_packages from setuptools import setup, find_packages
import io
import os import os
VERSION = "3.38" VERSION = "3.38"

View file

@ -2137,9 +2137,7 @@ def search(
table_columns = table_obj.columns_dict table_columns = table_obj.columns_dict
for c in column: for c in column:
if c not in table_columns: if c not in table_columns:
raise click.ClickException( raise click.ClickException(f"Table '{dbtable}' has no column '{c}")
f"Table '{dbtable}' has no column '{c}"
)
sql = table_obj.search_sql(columns=column, order_by=order, limit=limit) sql = table_obj.search_sql(columns=column, order_by=order, limit=limit)
if show_sql: if show_sql:
click.echo(sql) click.echo(sql)

View file

@ -28,11 +28,8 @@ from typing import (
cast, cast,
Any, Any,
Callable, Callable,
Dict,
Union, Union,
Optional, Optional,
List,
Tuple,
) )
from collections.abc import Generator, Iterable from collections.abc import Generator, Iterable
import uuid import uuid
@ -590,9 +587,7 @@ class Database:
query += '"' query += '"'
bits = _quote_fts_re.split(query) bits = _quote_fts_re.split(query)
bits = [b for b in bits if b and b != '""'] bits = [b for b in bits if b and b != '""']
return " ".join( return " ".join(f'"{bit}"' if not bit.startswith('"') else bit for bit in bits)
f'"{bit}"' if not bit.startswith('"') else bit for bit in bits
)
def quote_default_value(self, value: str) -> str: def quote_default_value(self, value: str) -> str:
if any( if any(
@ -680,9 +675,7 @@ class Database:
try: try:
table_name = f"t{secrets.token_hex(16)}" table_name = f"t{secrets.token_hex(16)}"
with self.conn: with self.conn:
self.conn.execute( self.conn.execute(f"create table {table_name} (name text) strict")
f"create table {table_name} (name text) strict"
)
self.conn.execute(f"drop table {table_name}") self.conn.execute(f"drop table {table_name}")
return True return True
except Exception: except Exception:
@ -907,9 +900,7 @@ class Database:
if fk.other_column != "rowid" and not any( if fk.other_column != "rowid" and not any(
c for c in self[fk.other_table].columns if c.name == fk.other_column c for c in self[fk.other_table].columns if c.name == fk.other_column
): ):
raise AlterError( raise AlterError(f"No such column: {fk.other_table}.{fk.other_column}")
f"No such column: {fk.other_table}.{fk.other_column}"
)
column_defs = [] column_defs = []
# ensure pk is a tuple # ensure pk is a tuple
@ -1592,9 +1583,7 @@ class Table(Queryable):
for row in self.db.execute_returning_dicts(sql): for row in self.db.execute_returning_dicts(sql):
index_name = row["name"] index_name = row["name"]
index_name_quoted = ( index_name_quoted = (
f'"{index_name}"' f'"{index_name}"' if not index_name.startswith('"') else index_name
if not index_name.startswith('"')
else index_name
) )
column_sql = f"PRAGMA index_info({index_name_quoted})" column_sql = f"PRAGMA index_info({index_name_quoted})"
columns = [] columns = []
@ -1616,9 +1605,7 @@ class Table(Queryable):
for row in self.db.execute_returning_dicts(sql): for row in self.db.execute_returning_dicts(sql):
index_name = row["name"] index_name = row["name"]
index_name_quoted = ( index_name_quoted = (
f'"{index_name}"' f'"{index_name}"' if not index_name.startswith('"') else index_name
if not index_name.startswith('"')
else index_name
) )
column_sql = f"PRAGMA index_xinfo({index_name_quoted})" column_sql = f"PRAGMA index_xinfo({index_name_quoted})"
index_columns = [] index_columns = []
@ -1971,15 +1958,11 @@ class Table(Queryable):
sqls.append(copy_sql) sqls.append(copy_sql)
# Drop (or keep) the old table # Drop (or keep) the old table
if keep_table: if keep_table:
sqls.append( sqls.append(f"ALTER TABLE [{self.name}] RENAME TO [{keep_table}];")
f"ALTER TABLE [{self.name}] RENAME TO [{keep_table}];"
)
else: else:
sqls.append(f"DROP TABLE [{self.name}];") sqls.append(f"DROP TABLE [{self.name}];")
# Rename the new one # Rename the new one
sqls.append( sqls.append(f"ALTER TABLE [{new_table_name}] RENAME TO [{self.name}];")
f"ALTER TABLE [{new_table_name}] RENAME TO [{self.name}];"
)
# Re-add existing indexes # Re-add existing indexes
for index in self.indexes: for index in self.indexes:
if index.origin != "pk": if index.origin != "pk":
@ -2152,9 +2135,7 @@ class Table(Queryable):
suffix = None suffix = None
created_index_name = None created_index_name = None
while True: while True:
created_index_name = ( created_index_name = f"{index_name}_{suffix}" if suffix else index_name
f"{index_name}_{suffix}" if suffix else index_name
)
sql = ( sql = (
textwrap.dedent( textwrap.dedent(
""" """
@ -2510,9 +2491,7 @@ class Table(Queryable):
""" """
) )
.strip() .strip()
.format( .format(table=self.name, columns=", ".join(f"[{c}]" for c in columns))
table=self.name, columns=", ".join(f"[{c}]" for c in columns)
)
) )
self.db.executescript(sql) self.db.executescript(sql)
return self return self
@ -3677,9 +3656,9 @@ class Table(Queryable):
most_common_results = None most_common_results = None
least_common_results = None least_common_results = None
if num_distinct == 1: if num_distinct == 1:
value = db.execute( value = db.execute(f"select [{column}] from [{table}] limit 1").fetchone()[
f"select [{column}] from [{table}] limit 1" 0
).fetchone()[0] ]
most_common_results = [(truncate(value), total_rows)] most_common_results = [(truncate(value), total_rows)]
elif num_distinct != total_rows: elif num_distinct != total_rows:
if most_common: if most_common:

View file

@ -9,7 +9,7 @@ import json
import os import os
import sys import sys
from . import recipes from . import recipes
from typing import Dict, cast, BinaryIO, Optional, Tuple, Type from typing import cast, BinaryIO, Optional
from collections.abc import Iterable from collections.abc import Iterable
import click import click
@ -226,9 +226,7 @@ def _extra_key_strategy(
yield row yield row
elif not extras_key: elif not extras_key:
extras = row.pop(None) # type: ignore extras = row.pop(None) # type: ignore
raise RowError( raise RowError(f"Row {row} contained these extra values: {extras}")
f"Row {row} contained these extra values: {extras}"
)
else: else:
row[extras_key] = row.pop(None) # type: ignore row[extras_key] = row.pop(None) # type: ignore
yield row yield row

View file

@ -845,14 +845,14 @@ def test_query_json_binary(db_path):
"data": { "data": {
"$base64": True, "$base64": True,
"encoded": ( "encoded": (
"eJzt0c1xAyEMBeC7q1ABHleR3HxNAQrIjmb4M0gelx+RTY7p4N2WBYT0vmufUknH" "eJzt0c1xAyEMBeC7q1ABHleR3HxNAQrIjmb4M0gelx+RTY7p4N2WBYT0vmufUknH"
"8kq5lz5pqRFXsTOl3pYkE/NJnHXoStruJEVjc0mOCyTqq/ZMJnXEZW1Js2ZvRm5U+" "8kq5lz5pqRFXsTOl3pYkE/NJnHXoStruJEVjc0mOCyTqq/ZMJnXEZW1Js2ZvRm5U+"
"DPKk9hRWqjyvTFx0YfzhT6MpGmN2lR1fzxjyfVMD9dFrS+bnkleMpMam/ZGXgrX1I" "DPKk9hRWqjyvTFx0YfzhT6MpGmN2lR1fzxjyfVMD9dFrS+bnkleMpMam/ZGXgrX1I"
"/K+5Au3S/9lNQRh0k4Gq/RUz8GiKfsQm+7JLsJ6fTo5JhVG00ZU76kZZkxePx49uI" "/K+5Au3S/9lNQRh0k4Gq/RUz8GiKfsQm+7JLsJ6fTo5JhVG00ZU76kZZkxePx49uI"
"jnpNoJyYlWUsoaSl/CcVATje/Kxu13RANnrHweaH3V5Jh4jvGyKCnxJLiXPKhmW3f" "jnpNoJyYlWUsoaSl/CcVATje/Kxu13RANnrHweaH3V5Jh4jvGyKCnxJLiXPKhmW3f"
"iCnG7Jql7RR3UvFo8jJ4z039dtOkTFmWzL1be9lt8A5II471m6vXy+l0BR/4wAc+8" "iCnG7Jql7RR3UvFo8jJ4z039dtOkTFmWzL1be9lt8A5II471m6vXy+l0BR/4wAc+8"
"IEPfOADH/jABz7wgQ984AMf+MAHPvCBD3zgAx/4wAc+8IEPfOADH/jABz7wgQ984A" "IEPfOADH/jABz7wgQ984AMf+MAHPvCBD3zgAx/4wAc+8IEPfOADH/jABz7wgQ984A"
"Mf+MAHPvCBD3zgAx/4wAc+8IEPfOADH/jABz7wgQ984PuP7xubBoN9" "Mf+MAHPvCBD3zgAx/4wAc+8IEPfOADH/jABz7wgQ984PuP7xubBoN9"
), ),
}, },
} }
@ -1171,12 +1171,12 @@ def test_upsert_alter(db_path, tmpdir):
# Not null: # Not null:
( (
["name", "text", "--not-null", "name"], ["name", "text", "--not-null", "name"],
("CREATE TABLE [t] (\n" " [name] TEXT NOT NULL\n" ")"), ("CREATE TABLE [t] (\n [name] TEXT NOT NULL\n)"),
), ),
# Default: # Default:
( (
["age", "integer", "--default", "age", "3"], ["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 # Compound primary key
( (
@ -2052,7 +2052,7 @@ def test_triggers(tmpdir, extra_args, expected):
), ),
( (
["dogs"], ["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"], ["chickens", "dogs"],
@ -2328,7 +2328,7 @@ def test_rename_table(tmpdir):
) )
assert result_error.exit_code == 1 assert result_error.exit_code == 1
assert result_error.output == ( 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 # And check --ignore works
result_error2 = CliRunner().invoke( result_error2 = CliRunner().invoke(

View file

@ -140,8 +140,7 @@ def test_insert_multiple_with_compound_primary_key(db_path, tmpdir):
def test_insert_not_null_default(db_path, tmpdir): def test_insert_not_null_default(db_path, tmpdir):
json_path = str(tmpdir / "dogs.json") json_path = str(tmpdir / "dogs.json")
dogs = [ dogs = [
{"id": i, "name": f"Cleo {i}", "age": i + 3, "score": 10} {"id": i, "name": f"Cleo {i}", "age": i + 3, "score": 10} for i in range(1, 21)
for i in range(1, 21)
] ]
with open(json_path, "w") as fp: with open(json_path, "w") as fp:
fp.write(json.dumps(dogs)) fp.write(json.dumps(dogs))

View file

@ -118,7 +118,7 @@ def test_memory_json_nl(tmpdir, use_stdin):
@pytest.mark.parametrize("use_stdin", (True, False)) @pytest.mark.parametrize("use_stdin", (True, False))
def test_memory_csv_encoding(tmpdir, use_stdin): def test_memory_csv_encoding(tmpdir, use_stdin):
latin1_csv = ( 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 input = None
if use_stdin: if use_stdin:

View file

@ -710,10 +710,7 @@ def test_columns_not_in_first_record_should_not_cause_batch_to_be_too_large(fres
records = [ records = [
{"c0": "first record"}, # one column in first record -> batch size = 999 {"c0": "first record"}, # one column in first record -> batch size = 999
# fill out the batch with 99 records with enough columns to exceed THRESHOLD # 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: try:
fresh_db["too_many_columns"].insert_all( 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'" "select sql from sqlite_master where name='idx_dogs_age_name'"
).fetchone()[0] ).fetchone()[0]
assert sql == ( 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): def test_insert_thousands_using_generator(fresh_db):
fresh_db["test"].insert_all( fresh_db["test"].insert_all({"i": i, "word": f"word_{i}"} for i in range(10000))
{"i": i, "word": f"word_{i}"} for i in range(10000)
)
assert [{"name": "i", "type": "INTEGER"}, {"name": "word", "type": "TEXT"}] == [ assert [{"name": "i", "type": "INTEGER"}, {"name": "word", "type": "TEXT"}] == [
{"name": col.name, "type": col.type} for col in fresh_db["test"].columns {"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}) fresh_db["t"].create({"id": int})
# This should not # This should not
fresh_db["t"].create({"name": str}, replace=True) 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( @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): def test_create_table_strict(fresh_db, strict):
table = fresh_db.create_table("t", {"id": int, "f": float}, strict=strict) table = fresh_db.create_table("t", {"id": int, "f": float}, strict=strict)
assert table.strict == strict or not fresh_db.supports_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: if strict and not fresh_db.supports_strict:
return return
if strict: if strict:

View file

@ -31,8 +31,7 @@ def test_extract_single_column(fresh_db, table, fk_column):
+ ")" + ")"
) )
assert fresh_db[expected_table].schema == ( assert fresh_db[expected_table].schema == (
f"CREATE TABLE [{expected_table}] (\n" f"CREATE TABLE [{expected_table}] (\n [id] INTEGER PRIMARY KEY,\n"
+ " [id] INTEGER PRIMARY KEY,\n"
" [species] TEXT\n" " [species] TEXT\n"
")" ")"
) )

View file

@ -25,7 +25,7 @@ def test_extracts(fresh_db, kwargs, expected_table, use_table_factory):
{"id": 2, "species_id": "Oak"}, {"id": 2, "species_id": "Oak"},
{"id": 3, "species_id": "Palm"}, {"id": 3, "species_id": "Palm"},
], ],
**insert_kwargs **insert_kwargs,
) )
# Should now have two tables: Trees and Species # Should now have two tables: Trees and Species
assert {expected_table, "Trees"} == set(fresh_db.table_names()) assert {expected_table, "Trees"} == set(fresh_db.table_names())