fix: enforce MAX_PRECISION bound in Solver.__init__#31
Merged
Conversation
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>
…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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem.
MAX_PRECISION = 8192is exported fromformulabut used to be purely decorative —Solver.__init__forwarded any value straight to the C++ extension. A typo likeprecision=10000quietly worked or produced surprising output instead of failing cleanly.Fix.
src/formula/formula.py—Solver.__init__now validates0 <= precision <= MAX_PRECISIONand raisesValueErrorwith 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.pycovers: 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.