scripts: csv.py: Fixed issue with exprs not always being typechecked

Kind of a complicated corner case, but this shows up if you try to sort
by fields as numbers and not as strings. In theory this is possible by
creating a hidden sort field with a typed expr:

  $ ./scripts/csv.py test.csv -bi -bfunction -Si=i

But we weren't typechecking sort fields that already exist in the by
fields, since these are usually strings.

This fix is to make sure all exprs are in the typechecked fields, even
if they are already in by fields. There's no real cost to this.

---

Note this version does _not_ typecheck i, and sorts by string:

  $ ./scripts/csv.py test.csv -bi -bfunction -Si

This raises the question, should we always sort by string by default?

I don't think so. It's easy to miss the difference, and a typecheck
error is a lot safer than incorrect sorting.

So this will sort by number, with i as a hidden field:

  $ ./scripts/csv.py test.csv -bfunction -Si

If you want to sort by string with a hidden field, this is still
possible with -l/--label:

  $ ./scripts/csv.py test.csv -bi -lfunction -Si
This commit is contained in:
Christopher Haster
2025-02-28 16:53:47 -06:00
parent 7789714560
commit 1b8733b3de

View File

@@ -1426,8 +1426,16 @@ def compile(fields_, results,
by.append(k)
# make sure sort/hot fields are included
for k, reverse in it.chain(sort or [], hot or []):
# this defaults to typechecking sort/hot fields, which is
# probably safer, if you really want to sort by strings you
# can use --by + --label to create hidden by fields
if k and k not in by and k not in fields:
fields.append(k)
# make sure all expr targets are in fields so they get typechecked
# correctly
for k, _ in exprs:
if k not in fields:
fields.append(k)
# we only really care about the last mod/expr for each field
mods = {k: mod for k, mod in mods}