Skip to main content

Evaluate all if-statement predicates in a Python file

Project description

ifeval

Evaluate all if-statement predicates in a Python file and simplify the code by removing unreachable branches.

This tool parses a Python source file, evaluates constant if conditions (if f(x), if x is None, etc.), and rewrites the file to include only the branches that would execute.


Installation

Clone the repository and install with pip:

git clone https://gitlab.com/knvvv/ifeval.git
cd ifeval
pip install .

Usage

Dry run (default)

To preview changes without modifying the file:

ifeval path/to/your_script.py

It will print the diff showing what would be changed.

Apply changes

To actually rewrite the file in-place, add the --no-dry flag:

ifeval path/to/your_script.py --no-dry

Example

Given the following code:

print("Hello")
if True:
    print("Keep this")
else:
    print("Remove this")

ifeval simplifies it to:

print("Hello")
print("Keep this")

⚠️ A word of caution ⚠️

This package executes all the code from files being analyzed. Do not run it on untrusted or potentially malicious code, as it may lead to arbitrary code execution.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ifeval-0.0.1.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ifeval-0.0.1-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file ifeval-0.0.1.tar.gz.

File metadata

  • Download URL: ifeval-0.0.1.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.7

File hashes

Hashes for ifeval-0.0.1.tar.gz
Algorithm Hash digest
SHA256 b2462f25eea7b51223af660029204e2a2b2049e445ec7d8129014fd66b405fcc
MD5 e13cbe870e588505f61c44a456ca83dd
BLAKE2b-256 cbbf771596e2500d8812a58cea37db1066a776925719afee2f5155b52b1a4db1

See more details on using hashes here.

File details

Details for the file ifeval-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: ifeval-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 4.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.7

File hashes

Hashes for ifeval-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 26574e39f175b0457e1813d8a926703fa62267daa8d58360785217a5a2f129c2
MD5 caa9687be68a711666a33ae32d28ed85
BLAKE2b-256 79c9fe488f5dbc0dd7358d3030ff6ade74ec3665f4e7815def03a70430214540

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page