Skip to main content

AST-based duplicate and structural similarity detector for Python

Project description

pydry

pydry is a small Python CLI for finding exact duplicate functions and structurally similar functions in Python code.

Features

  • Finds exact duplicate functions using AST normalization.
  • Ranks near matches by structural similarity and refactorability.
  • Flags likely abstraction candidates and common risk signals.
  • Emits text output for quick inspection and JSON output for automation.
  • Runs without third-party runtime dependencies.

Installation

python -m pip install pydry-cli

For local development from a checkout:

make venv
source venv/bin/activate
make install
make check

Quick start

Run a compact summary for the current directory:

pydry showcase

Find exact duplicates:

pydry exact ./src --normalize-local-names --normalize-constants

Find near matches:

pydry near ./src --threshold 0.85 --top-k 25

Write a full JSON report:

pydry report ./src --output reports/pydry-report.json

Commands

pydry exact

Find exact duplicate functions after AST normalization.

pydry exact ./src
pydry exact ./src --min-count 3
pydry exact ./src --normalize-local-names --normalize-constants
pydry exact ./src --format json

Useful options:

  • --min-count: minimum group size, default 2.
  • --top-level-only: ignore nested functions and methods.
  • --normalize-local-names: treat local variable renames as equivalent.
  • --normalize-constants: treat many literal value changes as equivalent.
  • --include-canonical: include canonical AST dumps in JSON output.
  • --strict: fail on files that cannot be read or parsed.

pydry near

Rank structurally similar function pairs.

pydry near ./src
pydry near ./src --threshold 0.85 --top-k 25
pydry near ./src --format json --output reports/near.json

Useful options:

  • --threshold: similarity threshold from 0 to 1, default 0.8.
  • --top-k: cap the number of returned pairs.
  • --top-level-only: ignore nested functions and methods.
  • --strict: fail on files that cannot be read or parsed.

pydry abstract

Filter near matches to pairs that look like plausible refactor candidates.

pydry abstract ./src
pydry abstract ./src --threshold 0.86 --format json

pydry report

Generate one JSON document with exact, near, and abstract sections.

pydry report ./src
pydry report ./src --threshold 0.82 --top-k 250 --output reports/pydry-report.json

pydry showcase and pydry simulate

Run a compact terminal summary. Both commands use the same analysis pipeline. With no path, they scan the current directory.

pydry showcase
pydry showcase ./src --top-k 10 --threshold 0.8
pydry showcase ./src --format json
pydry simulate ./src

JSON output

JSON-capable commands return an envelope:

{
  "results": [],
  "diagnostics": {
    "scan_errors_count": 0,
    "scan_error_samples": [],
    "plugin_errors_count": 0,
    "plugin_error_samples": []
  }
}

Near and abstract entries include:

  • similarity_score
  • refactorability_score
  • pattern_labels
  • shared_structure_summary
  • key_differences
  • risk_flags
  • suggested_refactor_kind
  • evidence

Python API

The CLI is the primary interface, but the core functions are importable:

from pathlib import Path

from pydry.engine import exact_groups, near_matches

groups = exact_groups(
    Path("src"),
    normalize_local_names=True,
    normalize_constants=True,
)
rows = near_matches(Path("src"), threshold=0.85, top_k=25)

Limitations

  • Similarity is heuristic. It does not prove semantic equivalence.
  • Cross-file import resolution is not attempted.
  • Generated abstraction templates are suggestions, not executable patches.

Development

make check
make coverage
make check-dist

The package supports Python 3.11 and newer.

License

pydry is released under the MIT License. See LICENSE.

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

pydry_cli-0.0.3.tar.gz (33.4 kB view details)

Uploaded Source

Built Distribution

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

pydry_cli-0.0.3-py3-none-any.whl (20.8 kB view details)

Uploaded Python 3

File details

Details for the file pydry_cli-0.0.3.tar.gz.

File metadata

  • Download URL: pydry_cli-0.0.3.tar.gz
  • Upload date:
  • Size: 33.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for pydry_cli-0.0.3.tar.gz
Algorithm Hash digest
SHA256 58fbb8eecbe33c0188eccae27cf8bcc43744460877d3840faf0944c36bd87d44
MD5 447cdb761966153708e66e08d8f4764b
BLAKE2b-256 d45995b2729ad26ecedb133e87abc094e6e0fd4b78107c8d411b5cd5a9119a97

See more details on using hashes here.

Provenance

The following attestation bundles were made for pydry_cli-0.0.3.tar.gz:

Publisher: publish.yml on hesreallyhim/pydry

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

File details

Details for the file pydry_cli-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: pydry_cli-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 20.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for pydry_cli-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 f63439aeb0694d392ccc6556078f4633b524aaad40759df587085be90e920206
MD5 8fba5e3407a676da4f675d3f0d78429b
BLAKE2b-256 80197e8f023c4e1a624a5a31618475ba9078fb44259ae28311633004ee3e5208

See more details on using hashes here.

Provenance

The following attestation bundles were made for pydry_cli-0.0.3-py3-none-any.whl:

Publisher: publish.yml on hesreallyhim/pydry

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