sqlite-utils/sqlite_utils
feiiiiii5 210c119907
insert --convert: validate the result instead of falling back on falsy
The JSON/CSV branch used 'fn(doc) or doc', so any falsy result was
discarded and the unconverted row inserted. Exit code 0, no warning:

    $ echo '[{"id": 1, "name": "Cleo"}]' | sqlite-utils insert d.db t - --convert '{}'
    $ sqlite-utils 'select * from d.db'      # -> the original row, not {}

The realistic shape of this is a filter:

    --convert 'row if row["ok"] else {}'    # meant to drop the bad rows
    # every row inserted, including the ones meant to be dropped

'--lines' runs a different expression twelve lines up and has no such
fallback, so the same --convert code is rejected there and silently
accepted here:

    --convert 0   JSON input  -> exit 0, rows inserted
    --convert 0   --lines     -> exit 1, Rows must all be dictionaries, got: 0

test_insert_convert_error_messages already asserts that a non-dict
return is rejected (--convert 1); 0, False, [] and "" were the outliers,
purely because they are falsy.

None is the one falsy result that has to keep meaning 'modify doc in
place' - a --convert that is a bare statement returns it, which
test_insert_convert_row_modifying_in_place covers - so the fallback is
now keyed on 'is None' rather than truthiness.

Tests: 7 new cases in tests/test_cli_insert.py, all failing on 6bc1d33.
Suite 1505 passed, 16 skipped. black, flake8, mypy clean. pyright
reports one error and ty two diagnostics, both identical on unmodified
main (tests/test_constructor.py:74 and two unused-type-ignore comments
in db.py/utils.py) and none in the files this touches.
2026-09-29 00:24:06 +08:00
..
__init__.py Support ANY column types for strict tables 2026-08-12 16:44:01 -07:00
__main__.py Support 'python -m sqlite_utils', closes #368 2022-01-08 18:33:22 -08:00
cli.py insert --convert: validate the result instead of falling back on falsy 2026-09-29 00:24:06 +08:00
create_table_parser.py Preserve composite UNIQUE constraints in transforms 2026-08-12 18:46:18 -07:00
db.py Fix for sqlite-utils 4.2 crashing bug (#843) 2026-08-13 16:52:03 -07:00
hookspecs.py Fixes for Ruff>=0.16.0 (#814) 2026-07-25 14:53:12 -07:00
migrations.py Fixes for Ruff>=0.16.0 (#814) 2026-07-25 14:53:12 -07:00
plugins.py Fixes for Ruff>=0.16.0 (#814) 2026-07-25 14:53:12 -07:00
py.typed py.typed file so mypy picks up the types, closes #331 2021-11-14 15:21:04 -08:00
recipes.py Fixes for Ruff>=0.16.0 (#814) 2026-07-25 14:53:12 -07:00
utils.py Fix resource leaks caused by buffered readers 2026-09-22 11:35:41 -07:00