skillfed

leval

Limited evaluator

leval v1.4.0 181.9K downloads/30d#10,112 on PyPI3
Permissive license MIT Active released

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 leval

uv

uv add leval

poetry

poetry add leval

Installing 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

safe expression evaluationlimited ast evaluatordynamic expression parsersandboxed math evaluationcontrolled formula evaluation
expression-evaluationsandboxing

More Software Development packages