Principle:Wandb Weave Decorator Style Enforcement
| Knowledge Sources | |
|---|---|
| Domains | Code_Quality, Static_Analysis |
| Last Updated | 2026-02-14 10:53 GMT |
Overview
Principle of enforcing consistent decorator syntax across a codebase via CST-based static analysis and auto-fix.
Description
Decorator Style Enforcement addresses visual inconsistency when a decorator accepts optional arguments. In Python, `@decorator` and `@decorator()` are functionally equivalent when no arguments are passed, but mixing the two forms creates inconsistent style. This principle uses a lint rule to detect the verbose form (with empty parentheses) and automatically rewrites it to the concise form (without parentheses). In Weave's case, this ensures `@weave.op` is always used instead of `@weave.op()`, reducing visual noise and maintaining a uniform coding style.
Usage
Apply this principle when a project has decorators that support both called (`@deco()`) and uncalled (`@deco`) forms and wants to enforce a single canonical style. This is particularly useful for decorator-heavy APIs like Weave's `@weave.op`.
Theoretical Basis
The mechanism uses a CST Decorator visitor:
- Visit: Each `Decorator` node in the syntax tree is visited.
- Match: The visitor checks if the decorator is a `Call` node with zero arguments whose function matches the target pattern (e.g., `weave.op`).
- Simplify: If matched, the `Call` wrapper is stripped, leaving only the attribute reference as the decorator.
Pseudo-code Logic:
# Abstract algorithm (NOT real implementation)
for decorator_node in cst.walk(source_tree):
if is_call_with_no_args(decorator_node) and matches_target(decorator_node):
simplified = strip_call_syntax(decorator_node)
report(decorator_node, message, simplified)