Skip to content

Fix test to pass pytest -Werror #623

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions sqlite_utils/cli.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import base64
import click
from click_default_group import DefaultGroup # type: ignore
from datetime import datetime
from datetime import datetime, timezone
import hashlib
import pathlib
from runpy import run_module
Expand Down Expand Up @@ -865,7 +865,7 @@ def inner(fn):
required=True,
),
click.argument("table"),
click.argument("file", type=click.File("rb"), required=True),
click.argument("file", type=click.File("rb", lazy=True), required=True),
click.option(
"--pk",
help="Columns to use as the primary key, e.g. id",
Expand Down Expand Up @@ -1949,12 +1949,16 @@ def memory(
fp = file_path.open("rb")
rows, format_used = rows_from_file(fp, format=format, encoding=encoding)
tracker = None
if format_used in (Format.CSV, Format.TSV) and not no_detect_types:
tracker = TypeTracker()
rows = tracker.wrap(rows)
if flatten:
rows = (_flatten(row) for row in rows)
db[file_table].insert_all(rows, alter=True)
try:
if format_used in (Format.CSV, Format.TSV) and not no_detect_types:
tracker = TypeTracker()
rows = tracker.wrap(rows)
if flatten:
rows = (_flatten(row) for row in rows)
db[file_table].insert_all(rows, alter=True)
except UnicodeDecodeError as e:
fp.close()
raise e
if tracker is not None:
db[file_table].transform(types=tracker.types)
# Add convenient t / t1 / t2 views
Expand Down Expand Up @@ -3196,8 +3200,12 @@ def __init__(self, exception, path):
"ctime": lambda p: p.stat().st_ctime,
"mtime_int": lambda p: int(p.stat().st_mtime),
"ctime_int": lambda p: int(p.stat().st_ctime),
"mtime_iso": lambda p: datetime.utcfromtimestamp(p.stat().st_mtime).isoformat(),
"ctime_iso": lambda p: datetime.utcfromtimestamp(p.stat().st_ctime).isoformat(),
"mtime_iso": lambda p: datetime.fromtimestamp(p.stat().st_mtime, timezone.utc)
.isoformat()
.rstrip("+00:00"),
"ctime_iso": lambda p: datetime.fromtimestamp(p.stat().st_mtime, timezone.utc)
.isoformat()
.rstrip("+00:00"),
"size": lambda p: p.stat().st_size,
"stem": lambda p: p.stem,
"suffix": lambda p: p.suffix,
Expand Down
8 changes: 7 additions & 1 deletion sqlite_utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ def _extra_key_strategy(
reader: Iterable[dict],
ignore_extras: Optional[bool] = False,
extras_key: Optional[str] = None,
buffer: Optional[io.TextIOWrapper] = None,
) -> Iterable[dict]:
# Logic for handling CSV rows with more values than there are headings
for row in reader:
Expand All @@ -231,6 +232,8 @@ def _extra_key_strategy(
else:
row[extras_key] = row.pop(None) # type: ignore
yield row
if buffer:
buffer.close()


def rows_from_file(
Expand Down Expand Up @@ -299,7 +302,10 @@ class Format(enum.Enum):
reader = csv.DictReader(decoded_fp, dialect=dialect)
else:
reader = csv.DictReader(decoded_fp)
return _extra_key_strategy(reader, ignore_extras, extras_key), Format.CSV
return (
_extra_key_strategy(reader, ignore_extras, extras_key, decoded_fp),
Format.CSV,
)
elif format == Format.TSV:
rows = rows_from_file(
fp, format=Format.CSV, dialect=csv.excel_tab, encoding=encoding
Expand Down