Skip to content

BUG: Report tract_math operation errors instead of a usage error - #144

Open
demianw wants to merge 1 commit into
masterfrom
fix-tract-math-error-reporting
Open

demianw wants to merge 1 commit into
masterfrom
fix-tract-math-error-reporting

Conversation

@demianw

@demianw demianw commented Nov 4, 2026

Copy link
Copy Markdown
Owner

What was wrong

scripts/cli_tract_math.py caught every TypeError raised while running an operation and reported it as a usage error, so bugs inside operations looked like wrong command lines. Reproduction on master (NumPy 2.5.4), where tract_bhattacharyya_coefficient has an internal TypeError (fixed separately in #143):

$ tract_math a.trk tract_bhattacharyya_coefficient 2 b.trk
Traceback (most recent call last):            # on stdout
  ...
TypeError: `bins` must be an integer, a string, or an array
usage: ...
tract_math: error:

Error: Wrong number of parameters for the operation

Also print(sys.stderr, "Error reading file ", ...) printed <_io.TextIOWrapper name='<stderr>' ...> Error reading file ... to stdout instead of writing to stderr.

The change

  • cli_tract_math.main catches only TractMathWrongArgumentsError (already a TypeError subclass) and reports just its message through parser.error, without a traceback, since it is a usage error. Any other exception propagates with its normal traceback and message. The now unused traceback import is removed.
  • Genuine wrong parameter counts still give a usage error: too few parameters were already rejected by the decorator, but too many parameters only failed as the TypeError of the operation call itself (e.g. tract_math t.trk count extra out.txt → count() takes 2 positional arguments but 3 were given). The decorator now checks the arguments with inspect.signature(func).bind(...) before calling the operation and raises TractMathWrongArgumentsError on a mismatch.
  • print(..., file=sys.stderr) for the file reading error.

After the change, the command above shows the TypeError traceback from NumPy (exit status 1), while tract_math t.trk tract_volume (missing parameter) prints only tract_math: error: Wrong number of arguments for the operation... (exit status 2).

Behaviour change: errors inside operations are no longer reported as "Wrong number of parameters"; usage errors no longer print a traceback.

Testing

  • New tests in tests/test_tract_math.py (pytest-console-scripts): missing and extra parameters give a usage error without a traceback; a TypeError raised inside an operation propagates out of main() instead of becoming a usage error; the file reading error goes to stderr. All four fail on master and pass here.
  • python -m pytest -q -p no:cacheprovider tract_querier: 46 passed, 1 skipped (pre-existing unconditional skip in test_query_rewrite.py).

Merge order

Touches lines next to #138 (--force in scripts/cli_tract_math.py and decorator.py, which raises its overwrite message through TractMathWrongArgumentsError — with this PR that message is shown without a traceback) and #139 (decorator.py); textual conflicts only. Both #106 and #143 append tests to tests/test_tract_math.py. Independent of #143 otherwise.

Closes #106

🤖 Generated with Claude Code

cli_tract_math caught every TypeError raised while running an
operation and reported it as "Wrong number of parameters for the
operation", hiding genuine bugs inside operations. Catch only
TractMathWrongArgumentsError, and print just its message (no
traceback) since it is a usage error.

Too many parameters used to surface as the TypeError of the operation
call itself; the decorator now checks the arguments against the
operation signature and raises TractMathWrongArgumentsError, so wrong
parameter counts still give a usage error.

Also write the "Error reading file" message to stderr:
print(sys.stderr, ...) printed the stream object to stdout.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: tract_math reports every crash as "Wrong number of parameters"

1 participant