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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b2462f25eea7b51223af660029204e2a2b2049e445ec7d8129014fd66b405fcc
|
|
| MD5 |
e13cbe870e588505f61c44a456ca83dd
|
|
| BLAKE2b-256 |
cbbf771596e2500d8812a58cea37db1066a776925719afee2f5155b52b1a4db1
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
26574e39f175b0457e1813d8a926703fa62267daa8d58360785217a5a2f129c2
|
|
| MD5 |
caa9687be68a711666a33ae32d28ed85
|
|
| BLAKE2b-256 |
79c9fe488f5dbc0dd7358d3030ff6ade74ec3665f4e7815def03a70430214540
|