Hey folks! 👋 I've been experimenting with Aider for a few months now, and while it's fantastic at generating code quickly, I sometimes find it misses specific code style or architectural patterns my team requires. I realized I could get more consistent results by making Aider follow our custom linting rules.
Hereβs a walkthrough of how I set that up. The core idea is to use Aiderβs `--lint-cmd` flag to run a custom script that enforces our rules before any commit.
First, I created a simple Python script that acts as a custom linter. It checks for a few things we care about, like disallowing certain imports or enforcing docstring formats.
```python
#!/usr/bin/env python3
# custom_linter.py
import sys
import ast
def check_file(filepath):
with open(filepath, 'r') as f:
content = f.read()
tree = ast.parse(content)
issues = []
# Example rule: forbid using 'print' in production modules
for node in ast.walk(tree):
if isinstance(node, ast.Call):
if isinstance(node.func, ast.Name) and node.func.id == 'print':
issues.append(f"Avoid 'print' statements in {filepath}")
return issues
if __name__ == "__main__":
issues = []
for filepath in sys.argv[1:]:
issues.extend(check_file(filepath))
if issues:
for issue in issues:
print(issue)
sys.exit(1) # Exit with error to signal lint failure
sys.exit(0)
```
Then, I run Aider with the linter command specified:
```bash
aider --lint-cmd "python3 custom_linter.py" myfile.py
```
When Aider tries to commit a change, it runs this script. If the script finds a violation (like a `print` statement), it exits with a non-zero code, and Aider will refuse the commit and ask the AI to fix the issue.
A few tips from my experience:
* Keep your linting script fast. If it's slow, it interrupts the flow.
* Make the error messages clear so the AI can understand what to fix.
* You can combine this with existing linters (like `pylint` or `eslint`) by wrapping them in a script.
* Remember, this works on the *whole file* at commit time, not per edit.
This approach has really helped us maintain consistency, especially for rules that standard linters don't cover. Have any of you tried something similar? I'd love to hear about other custom rules or integration ideas!
Clean code is not an option, it's a sanity measure.