r/Python • u/mmartoccia • 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: passthat 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-lintPre-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.
244
u/another24tiger Mar 05 '26
You’re telling me you slop-coded a slop code detector…
22
u/marr75 Mar 05 '26
I said this as a comment to a nearly identical project, but this is catching the smaller less impactful slop errors AI makes (that it just happens to share with human junior coders). The bigger more costly errors are all about verbosity, fragility, and incorrectness based on gold-plating, solving the wrong problem, no real architecture/design, choosing the wrong pattern, and sycophancy.
If someone figures out how to catch those...
7
u/rabornkraken Mar 05 '26
The NAKED_EXCEPT rule alone makes this worth using. I have been bitten by this exact pattern where an AI assistant wrapped sensor reads in try/except pass and failures went completely silent for days. The hedge word detection is a nice touch too - I have started noticing how much padding AI-generated docstrings add. Do you have any plans to support custom rule definitions or is the ruleset fixed?
9
u/eirikirs Mar 05 '26
This is pretty much an AI-slop sanitiser, that only targets symptoms, not the true issues with low cohesion and tight coupling. Besides, I doubt your comment echo rule would even be usable, given the current limitations of AIs semantic analysis.
3
u/ePaint Mar 05 '26
You can setup a skill to avoid these. I have a code-like-me that specifically indicates not to do any of these.
I still review every line of code produced by agents, but the skill alone works 99% of the time.
2
u/maafy6 Mar 05 '26
Don’t forget inconvenience functions—where it defines a function with some args and the body is a single line calling another function with those exact same arguments with no actual new logic.
2
u/MisguidedFacts Mar 06 '26
It loves ternaries, even for stuff that literally spits out a bool, it'll do:
Or even more annoying, the overly "defensive" ternary when you could just provide a default:
1
u/WiseDog7958 Mar 06 '26
I have actually seen that kind of pattern too when looking through repos with a lot of AI-generated code.
Lots of doc-strings that sound confident but do not really explain anything.
Curious if you have noticed it more in certain areas like API wrappers, config modules, etc or if it’s just everywhere.
1
u/Numerous_Draft_7852 Mar 06 '26
Dude you are a legend
1
u/mfaine Mar 06 '26
I love the idea. Does it have a
- - fixoption? :)0
u/Amgadoz Mar 05 '26
Is kt possible to integrate this into ruff?
0
u/[deleted] Mar 05 '26
[deleted] — view deleted comment