Skip to content

Commit

Permalink
Add linter for raw SQL, refs #36
Browse files Browse the repository at this point in the history
  • Loading branch information
atodorov committed Jun 25, 2018
1 parent 1a58ef6 commit d823d08
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions kiwi_lint/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
from .dunder_attributes import DunderClassAttributeChecker
from .list_comprehension import ListComprehensionChecker
from .docstring import DocstringChecker
from .raw_sql import RawSQLChecker


def register(linter):
linter.register_checker(DunderClassAttributeChecker(linter))
linter.register_checker(ListComprehensionChecker(linter))
linter.register_checker(DocstringChecker(linter))
linter.register_checker(RawSQLChecker(linter))
28 changes: 28 additions & 0 deletions kiwi_lint/raw_sql.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Copyright (c) 2018 Alexander Todorov <[email protected]>

# Licensed under the GPL 2.0: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html

import astroid

from pylint import interfaces
from pylint import checkers
from pylint.checkers import utils


class RawSQLChecker(checkers.BaseChecker):
__implements__ = (interfaces.IAstroidChecker,)

name = 'raw-sql-checker'

msgs = {'R4431': ('Avoid using raw SQL',
'avoid-raw-sql',
'Avoid raw SQL, use Django ORM queries instead')}

@utils.check_messages('avoid-raw-sql')
def visit_attribute(self, node):
# looking for .extra(select={}) patterns
if node.attrname == 'extra' and isinstance(node.parent, astroid.Call):
for keyword in node.parent.keywords:
if keyword.arg == 'select':
self.add_message('avoid-raw-sql', node=node)
break

0 comments on commit d823d08

Please sign in to comment.