leval
Limited evaluator
What it is and what it does
Leval is a Python expression evaluator designed to be safer and more flexible than ast.literal_eval() while remaining sandboxed. It parses expressions into an AST and walks it in pure Python, never using getattr or subscripting outside your control. You define which functions and variables are available, and can enforce depth and time limits on evaluation.
The package offers two APIs: a simple_eval() function for straightforward cases (arithmetic, comparisons, function calls with your registered functions), and an advanced API with subclassable Evaluator and Universe classes for custom evaluation rules. Security depends entirely on what you register—any function you provide can be called with attacker-controlled arguments, and operators invoke dunder methods on your values, so avoid registering functions with side effects or objects with dangerous comparisons.
Use it for:
- Evaluate user-supplied mathematical or logical expressions in web forms or configuration files without executing arbitrary code.
- Build a formula engine for applications where users define calculations with controlled variables and functions.
- Implement conditional rule evaluation in data pipelines where expressions reference row values but cannot access the filesystem or network.
- Parse and evaluate templated expressions in configuration systems with bounded complexity and execution time.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Leval evaluates limited expressions safely by parsing them into an AST and walking it in Python, supporting depth and time limits alongside optional variable and function mappings.
Yes. Leval is a focused, well-maintained tool with zero dependencies, active development, and no known vulnerabilities. It solves a real security problem—safe dynamic expression evaluation—better than ast.literal_eval() for cases where you need variables and functions. Install it if you need to evaluate user or config expressions safely; skip it if you only need literal Python values.
Install
leval on PyPI
pip
pip install levaluv
uv add levalpoetry
poetry add levalInstalling leval
Before you install
Very low friction: pure Python wheel with no runtime dependencies, and actively maintained with a release on 2026-08-13.
License in practice
MIT license permits commercial and private use with minimal restrictions, requiring only attribution.
Quickstart
from leval.simple import simple_eval
assert simple_eval('1 + 2') == 3
assert simple_eval('x < -80 or x > 125 or x == 85', values={'x': 85})
assert simple_eval('abs(x) > 80', values={'x': -85}, functions={'abs': abs})
Requires Python 3.8 or later.
Verify before relying
- Real-world performance characteristics when evaluating deeply nested or complex expressions.
- Whether max_time enforcement is sufficient for your threat model, given it is cooperative rather than preemptive.
Package facts
| License | MIT (permissive) |
| Python support | supports the current Python release (>=3.8) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | none |
| Maintenance | actively maintained — 1 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 181,868/month — #10,112 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: leval-1.4.0-py3-none-any.whl
Tags
More Software Development packages
Provides backported and experimental type hints…
permissive · top 100 on PyPI
numpyNumPy provides an N-dimensional array object…
permissive · top 100 on PyPI
fastapiFastAPI is a Python web framework for building…
permissive · top 100 on PyPI
annotated-docProvides a way to document function parameters,…
permissive · top 100 on PyPI
typerTyper builds command-line applications from…
permissive · top 1,000 on PyPI
distlibDistlib provides low-level packaging utilities…
permissive · top 1,000 on PyPI
astevalAsteval is a safe Python expression and…
permissive · top 5,000 on PyPI
simpleevalEvaluates mathematical and logical expressions…
permissive · top 5,000 on PyPI
pure-evalSafely evaluate Python AST nodes without…
permissive · top 1,000 on PyPI
py-expression-evalParses and evaluates mathematical expressions…
permissive · top 15,000 on PyPI
cel-pythonParses and evaluates Google Common Expression…
unclear · top 5,000 on PyPI
common-expression-languageEvaluates Google's Common Expression Language…
permissive · top 15,000 on PyPI
tag-expressionsParses and evaluates logical tag expressions…
copyleft · top 15,000 on PyPI
autoevalsAutoevals provides automatic evaluation methods…
unclear · top 5,000 on PyPI
trampolineEnables recursive functions to bypass Python's…
permissive · top 5,000 on PyPI
bddlBDDL is a domain-specific language for defining…
unclear · top 15,000 on PyPI