Python · maintainability
no-repeated-string-literal
python:no-repeated-string-literal Structured string literals repeated across functions should use a module constant.
- Code
- SARJ024
- Default
- error
- Fix
- none
- Languages
- python
Why
Independent copies of SQL, route templates, and identifier-like strings can drift while appearing equivalent.
Fix
Extract the shared value to one named module-level constant and reference it from each function.
Before / after
Executed by this rule’s unit tests.
Before
SQL literal is copied across functions
def load():
return "SELECT id, name, created_at FROM organization"
def refresh():
return "SELECT id, name, created_at FROM organization"
After
Functions reuse one SQL constant
QUERY = "SELECT id, name, created_at FROM organization"
def load():
return QUERY
def refresh():
return QUERY
Limits
- Only structured literals of at least 40 characters repeated across distinct functions are reported.
- Prose, documentation scaffolding, annotations, generated files, and repeated f-string fragments are excluded.