Skip to content

Improve parser error message for missing units in equations#1787

Open
has9sayed wants to merge 6 commits into
brian-team:masterfrom
has9sayed:parse
Open

Improve parser error message for missing units in equations#1787
has9sayed wants to merge 6 commits into
brian-team:masterfrom
has9sayed:parse

Conversation

@has9sayed
Copy link
Copy Markdown
Contributor

A more explicit error message when a user forgets to specify the units in an equation. It intercepts the generic Expected end of text error from pyparsing and now suggests checking for an omitted unit or missing colon (:), improving the debugging experience for syntax errors.

 A more explicit error message when a user forgets to specify the units in an equation. It intercepts the generic `Expected end of text` error from pyparsing and now suggests checking for an omitted unit or missing colon (:), improving the debugging experience for syntax errors.
@has9sayed
Copy link
Copy Markdown
Contributor Author

has9sayed commented Mar 11, 2026

Improve Equations parsing by replacing generic pyparsing errors and raw Python tracebacks with targeted diagnostics.

  • exact problem (missing colon, malformed d…/dt, unmatched parentheses, invalid units, etc.) and provides a suggested fix.
  • Extracts inline # comments from equation strings and stores them on SingleEquation.comment so model descriptions retain user annotations for downstream tooling and documentation.
  • Refactors the parsing helpers for clarity and reuse while keeping the public API and existing behavior intact.
from brian2.equations.equations import parse_string_equations, EquationError

try:
    parse_string_equations("dv/dt = -v / (tau : volt")
except EquationError as exc:
    print(exc)

Output -

Syntax error in the expression for variable 'v':
  -v / (tau
Unmatched parenthesis — 1 opening '(' but only 0 closing ')'. Add 1 closing paren to balance.

@mstimberg Is this how we wanted?

@mstimberg
Copy link
Copy Markdown
Member

Hi @has9sayed, thanks for the PR. I will need a bit more time to look into this in detail – at a cursory look this looks more complex/comprehensive than what I had in mind, but maybe that's a good thing 😊

@has9sayed
Copy link
Copy Markdown
Contributor Author

Improved diagnostics for missing unit separators in equations.

  • Detect missing : causing line merging and return a clear, targeted hint instead of a generic SyntaxError.
  • Handle empty parse_exception.line by falling back to the raw equation string.
  • Extend tests to cover multiline cases and prevent false positives on valid equations.

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.

2 participants