Skip to main content

Detect and auto-fix Yoda conditions in Python source files

Project description

yodacheck

AST-based linter that detects and auto-fixes Yoda conditions in Python source files.

A Yoda condition is a comparison where the literal is on the left side:

# Yoda — flagged
if 5 == x: ...
if None == obj: ...
if True == flag: ...

# Normal — not flagged
if x == 5: ...
if obj is None: ...
if flag is True: ...

Features

  • Detects all Yoda patterns (==, !=, <, <=, >, >=, is, is not)
  • Smart fixes: None ==is None, True/False ==is True/False, all else → swap
  • Auto-fix with timestamped .bak backup before writing
  • Chained comparisons (1 < x < 3) flagged but not auto-fixed (ambiguous)
  • in / not in with literal on left intentionally ignored (normal membership syntax)
  • Output formats: pretty (default), plain, json
  • Color auto-detected (TTY), forced via --color / --no-color
  • Default excludes: .venv, __pycache__, .git, build, dist

Installation

pip install .

After install the yodacheck command is available globally. Without installing, use python yodacheck.py directly.

Usage

# Scan file or directory
yodacheck path/to/file.py
yodacheck src/

# Multiple paths
yodacheck src/ tests/ setup.py

# Show N context lines per issue
yodacheck src/ --context 2

# Auto-fix (creates .bak backup first)
yodacheck src/ --fix

# Exclude paths (glob or dir name, repeatable)
yodacheck . --exclude "migrations/*" --exclude vendor/

# Output format
yodacheck src/ --format plain
yodacheck src/ --format json

# Color
yodacheck src/ --color
yodacheck src/ --no-color

Output

3 issues · 1 file

  src/auth.py
  42:3   None == token          →  token is None        [is None]
  67:3   0 == status_code       →  status_code == 0
  99:3   1 < retry_count < 5    ⚠ chained, no fix

With --context 2:

  42:3   None == token  →  token is None  [is None]
         40 │ 
         41 │ def validate(token):
         42 │ if None == token:
               ^
         43 │     raise ValueError
         44 │ 

Fix behavior

--fix applies all fixable replacements in one pass per file. Chained comparisons are skipped.

yodacheck src/ --fix
# fixed src/auth.py  (auth.py.bak.20260504T150000Z)

JSON output

yodacheck src/ --format json
[
  {
    "path": "src/auth.py",
    "lineno": 42,
    "col": 3,
    "expr": "None == token",
    "tag": "is None",
    "replacement": "token is None",
    "chained": false
  }
]

tag values: "is None", "is not None", "bool", "" (plain swap), "" with chained: true.

Test file

examples/example.py — runnable collection of every detectable pattern:

yodacheck examples/example.py --color
yodacheck examples/example.py --color --context 1

Running tests

python -m unittest discover -s tests -v

Requirements

Python 3.8+. No external dependencies.

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

yodacheck-0.1.0.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

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

yodacheck-0.1.0-py3-none-any.whl (3.7 kB view details)

Uploaded Python 3

File details

Details for the file yodacheck-0.1.0.tar.gz.

File metadata

  • Download URL: yodacheck-0.1.0.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for yodacheck-0.1.0.tar.gz
Algorithm Hash digest
SHA256 8238f80bab367bb894e9dd8cd03e87a0645a3dcc358d5e89d6c15107baab42c6
MD5 ab7be989456afa6524b429da23fba899
BLAKE2b-256 6cf8636bd6534b6037e732d9b510986d336e6c0e1523214c5698200907a95fcb

See more details on using hashes here.

Provenance

The following attestation bundles were made for yodacheck-0.1.0.tar.gz:

Publisher: release.yml on SytxLabs/YodaCheck

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file yodacheck-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: yodacheck-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 3.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for yodacheck-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a13a4e475b00f075011014b8824f37b1960615d7112bdff20ca72a828a0a6183
MD5 28e30741a89b6c1b48a0651be0d1cc96
BLAKE2b-256 22bf5f6b75ce0f7ed433d8f347acd6fdf68690aa1dc57b08abe476a5d4509f0d

See more details on using hashes here.

Provenance

The following attestation bundles were made for yodacheck-0.1.0-py3-none-any.whl:

Publisher: release.yml on SytxLabs/YodaCheck

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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