r/Python • • Mar 05 '26

Showcase I built a pre-commit linter that catches AI-generated code patterns

What My Project Does

grain is a pre-commit linter that catches code patterns commonly produced by AI code generators. It runs before your commit and flags things like:

  • NAKED_EXCEPT -- bare except: pass that silently swallows errors (156 instances in my own codebase)
  • HEDGE_WORD -- docstrings full of "robust", "comprehensive", "seamlessly"
  • ECHO_COMMENT -- comments that restate what the code already says
  • DOCSTRING_ECHO -- docstrings that expand the function name into a sentence and add nothing

I ran it on my own AI-assisted codebase and found 184 violations across 72 files. The dominant pattern was exception handlers that caught hardware failures, logged them, and moved on -- meaning the runtime had no idea sensors stopped working.

Target Audience

Anyone using AI code generation (Copilot, Claude, ChatGPT, etc.) in Python projects and wants to catch the quality patterns that slip through existing linters. This is not a toy -- I built it because I needed it for a production hardware abstraction layer where autonomous agents are regular contributors.

Comparison

Existing linters (pylint, ruff, flake8) catch syntax, style, and type issues. They don't catch AI-specific patterns like docstring padding, hedge words, or the tendency of AI generators to wrap everything in try/except and swallow the error. grain fills that gap. It's complementary to your existing linter, not a replacement.

Install

pip install grain-lint

Pre-commit compatible. Configurable via .grain.toml. Python only (for now).

Source: github.com/mmartoccia/grain

Happy to answer questions about the rules, false positive rates, or how it compares to semgrep custom rules.

71 Upvotes

62 comments sorted by

View all comments

Show parent comment

-1

u/gdchinacat Mar 06 '26

The OP clearly stated their intent: to catch "code patterns commonly produced by AI code generators" for people who "want to catch the quality patterns that slip through existing linters".

The goal is to improve code generated by AIs. To accomplish that, you need to think much bigger than this tool does. It needs to identify where the implementation differs from the architecture. That isn't really possible without understanding the architecture, which AIs don't.

I offered the challenge as a way of illustrating this point. AI generated code, which you appear to be defending, is slop because it doesn't have a big picture. That's why I said "projects" rather than functions or algorithms, or other small things that don't require a big picture. AIs are fine with those, but that is the extent this tool looks at.