Skip to content

fix: enforce MAX_PRECISION bound in Solver.__init__#31

Merged
hozblok merged 3 commits into
masterfrom
fix/19-enforce-max-precision
May 19, 2026
Merged

fix: enforce MAX_PRECISION bound in Solver.__init__#31
hozblok merged 3 commits into
masterfrom
fix/19-enforce-max-precision

Conversation

@hozblok

@hozblok hozblok commented May 11, 2026

Copy link
Copy Markdown
Owner

Problem. MAX_PRECISION = 8192 is exported from formula but used to be purely decorative — Solver.__init__ forwarded any value straight to the C++ extension. A typo like precision=10000 quietly worked or produced surprising output instead of failing cleanly.

Fix. src/formula/formula.pySolver.__init__ now validates 0 <= precision <= MAX_PRECISION and raises ValueError with a clear message naming both the bound and the offending value. The previously-pylint-disabled "useless super delegation" override now does meaningful work (so PR #20 / the cleanup batch will keep it instead of deleting it).

Test. New file tests/test_solver_precision_bounds.py covers: precision == MAX_PRECISION accepted; precision == 0 accepted (existing "all digits" sentinel); negative precision rejected; MAX_PRECISION + 1 rejected; constant pinned to 8192. Full suite 321/321.

MAX_PRECISION = 8192 was exported from the package but used to be
purely decorative — Solver forwarded any value to the C++ extension.
A typo like precision=10000 quietly worked or produced surprising
output instead of failing cleanly.

Solver.__init__ now validates `0 <= precision <= MAX_PRECISION` and
raises ValueError with a clear message naming both the bound and the
offending value. The previously-pylint-disabled "useless super
delegation" override now does meaningful work.

Regression test in tests/test_solver_precision_bounds.py covers:
- precision == MAX_PRECISION accepted
- precision == 0 accepted (existing sentinel)
- negative precision rejected
- MAX_PRECISION + 1 rejected
- MAX_PRECISION constant pinned to 8192

See ai/improvements_2026-05-09.md item #19.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@hozblok hozblok marked this pull request as ready for review May 19, 2026 21:32
…rkaround)

The previous deferred `from formula import MAX_PRECISION` inside
Solver.__init__ existed because formula/__init__.py defined
MAX_PRECISION *after* `from .formula import Solver, Number`. Importing
MAX_PRECISION at formula.py module-top would have triggered an
ImportError against the partially-initialized package, so the lookup
had to be deferred to call time.

Move the constant to formula.py — the only module that uses it — and
re-export it from __init__.py. Removes the per-call sys.modules lookup,
makes the dependency visible to static analyzers and IDEs, and
eliminates the circular relationship.

`from formula import MAX_PRECISION` and `formula.MAX_PRECISION` keep
working (re-exported); the regression test in
tests/test_solver_precision_bounds.py imports it that way and still
passes unchanged.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@hozblok hozblok merged commit ceefe45 into master May 19, 2026
5 checks passed
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.

1 participant