squabble.lint module

linting engine

class squabble.lint.LintIssue[source]

Bases: squabble.lint._LintIssue

class squabble.lint.Severity[source]

Bases: enum.Enum

Enumeration describing the relative severity of a LintIssue.

By themselves, these values don’t mean much, but are meant to convey the likely hood that a detected issue is truly problematic. For example, a syntax error in a migration would be CRITICAL, but perhaps a naming inconsistency would be LOW.

LOW = 'LOW'
MEDIUM = 'MEDIUM'
HIGH = 'HIGH'
CRITICAL = 'CRITICAL'
squabble.lint.check_file(config, name, contents)[source]

Return a list of lint issues from using config to lint name.

class squabble.lint.Session(rules, sql_text, file_name)[source]

Bases: object

A run of the linter using a given set of rules over a single file. This class exists mainly to hold the list of issues returned by the enabled rules.

report_issue(issue)[source]
lint()[source]

Run the linter on SQL given in constructor, returning a list of LintIssue discovered.

class squabble.lint.Context(session)[source]

Bases: object

Contains the node tag callback hooks enabled at or below the parent_node passed to the call to traverse.

>>> import pglast
>>> ast = pglast.Node(pglast.parse_sql('''
...   CREATE TABLE foo (id INTEGER PRIMARY KEY);
... '''))
>>> ctx = Context(session=...)
>>>
>>> def create_stmt(child_ctx, node):
...     print('create stmt')
...     child_ctx.register('ColumnDef', lambda _c, _n: print('from child'))
...
>>> ctx.register('CreateStmt', create_stmt)
>>> ctx.register('ColumnDef', lambda _c, _n: print('from root'))
>>> ctx.traverse(ast)
create stmt
from child
from root
traverse(parent_node)[source]

Recursively walk down the AST starting at parent_node.

For every node, call any callback functions registered for that particular node tag.

register_exit(fn)[source]

Register fn to be called when the current node is finished being traversed.

register(node_tag, fn)[source]

Register fn to be called whenever node_tag node is visited.

>>> session = ...
>>> ctx = Context(session)
>>> ctx.register('CreateStmt', lambda ctx, node: ...)
report_issue(issue)[source]
report(message, node=None, severity=None)[source]

Convenience wrapper to create and report a lint issue.