Skip to content

ruff_python_dto_check: add class_with_decorator matcher - #128

Merged
AdaWorldAPI merged 2 commits into
mainfrom
claude/mistaber-ruff-harvest-9gtxd4
Oct 30, 2026
Merged

AdaWorldAPI merged 2 commits into
mainfrom
claude/mistaber-ruff-harvest-9gtxd4

Conversation

@AdaWorldAPI

Copy link
Copy Markdown
Owner

Summary

ruff_python_dto_check matched only top-level decorated functions. Python code that models data as decorated classes, such as @dataclass, attrs or pydantic, was invisible to it. Run on Mistaber's HLL compiler (BrainyBlaze/mistaber, mistaber/dsl/compiler/), it matched nothing, although that package defines its AST as 21 @dataclass classes.

This adds a second matcher kind, class_with_decorator. It matches top-level classes with the same decorator selector used for functions and emits these paths:

  • class.name and class.decorators
  • class.bases: base classes and keywords, as raw source text
  • class.fields: {name, annotation, default} for each annotated assignment in the class body
  • class.methods: names of the functions defined in the class body
  • class.body.source and class.range.line_start / class.range.line_end
  • the existing decorator.raw, decorator.args[i] and decorator.kwargs.<k>

Classes nested inside other classes or functions are not matched, the same top-level policy as for functions. The class name is stored in the existing function_name field so that consumers of the bundle format keep working.

Changes to existing behaviour:

  • The function matcher now applies only function_with_decorator rules. Before, it applied every rule regardless of kind, so a class rule would also have matched decorated functions.
  • The decorator selector moves to matcher/decorator.rs and is shared by both matchers. The decorator emit paths are also shared.
  • matcher::harvest_with_config merges function and class bundles in source order, and the CLI uses it.
  • compute_ast_hash now finds the top-level statement by its start line instead of by function name. Class bundles therefore hash their own body; looking them up by function name would have given every class the empty-body hash. Function hashes are unchanged, and the golden tests pass unmodified.

The JSON schema, the README scope section and a new examples/dataclass.config.json are updated to match.

Test Plan

  • cargo test -p ruff_python_dto_check: all suites pass, including the new tests/class_matcher_test.rs (6 tests). The fixture has decorated, undecorated, differently decorated and nested classes, plus a decorated function whose decorator name matches the class rule. The tests assert:
    • exactly which classes match;
    • the exact fields, bases, methods and decorator.kwargs values;
    • that a function rule matches only the function;
    • source order when both kinds of rule are present;
    • the unknown-kind error message;
    • that classes get distinct, non-empty AST hashes.
  • cargo clippy -p ruff_python_dto_check --all-targets -- -D warnings and cargo fmt -p ruff_python_dto_check --check are clean on the pinned toolchain.
  • Disable runs, each turning tests red:
    • the function matcher's kind filter removed: 3 tests fail;
    • class bodies excluded from the AST hash: 1 test fails.
  • On Mistaber's compiler package, ruff-py-dto harvest --config examples/dataclass.config.json finds exactly the 21 @dataclass classes that Python's own ast module counts. None of them has the empty-body hash.

Generated by Claude Code

Data-shaped Python (dataclasses, attrs, pydantic) lives in decorated classes,
which the harvester could not see: it matched only top-level decorated
functions. The new kind matches top-level decorated classes with the same
decorator selector and emits class.name, class.bases, class.decorators,
class.fields (annotated assignments), class.methods, class.body.source and
class.range.*, plus the existing decorator.* paths.

The function matcher now applies only function rules (it previously ran
every rule regardless of kind), the decorator selector is shared by both
matchers, harvest_with_config merges both in source order, and the AST hash
locates its statement by start line so class bundles hash their own body
instead of an empty one. Existing function hashes and golden outputs are
unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XAqKMMCqUkM2QW7zqLC2NJ
@coderabbitai

coderabbitai Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Important

  • 🔍 Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 146070b4-aec7-4021-80fe-d90b920d02a8
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@AdaWorldAPI
AdaWorldAPI marked this pull request as ready for review October 30, 2026 14:56
@AdaWorldAPI
AdaWorldAPI merged commit 4c3acd4 into main Oct 30, 2026
36 of 37 checks passed
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-30T14:57:34.762836Z 721ca37 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants