Skip to content

Advanced API

The simple API presents only a fraction of the functionality present within the core SQLFluff library. For more advanced use cases, users can import the Linter() and FluffConfig() classes from sqlfluff.core. As of version 0.4.0 this is considered as experimental only as the internals may change without warning in any future release. If you come to rely on the internals of SQLFluff, please post an issue on GitHub to share what you're up to. This will help shape a more reliable, tidy and well documented public API for use.

Classes

Linter

The interface class to interact with the linter.

Parameters:

ParameterTypeDefaultDescription
config`fluffconfig.FluffConfigNone`None
formatter`FormatterInterfaceNone`None
dialect`strNone`None
rules`list[str]None`None
user_rules`list[type[base.BaseRule]]None`None
exclude_rules`list[str]None`None

Methods

allowed_rule_ref_map

python
allowed_rule_ref_map(
    reference_map,
    disable_noqa_except
) → dict[str, set[str]]

Generate a noqa rule reference map.

Parameters:

ParameterTypeDefaultDescription
reference_mapdict[str, set[str]]
disable_noqa_except`strNone`

Returns:

dict[str, set[str]] — See return type above

fix

python
fix(
    self,
    tree,
    config=None,
    fname=None,
    templated_file=None
) → tuple[segments.base.BaseSegment, list[SQLBaseError]]

Return the fixed tree and violations from lintfix when we're fixing.

Parameters:

ParameterTypeDefaultDescription
self
treesegments.base.BaseSegment
config`fluffconfig.FluffConfigNone`None
fname`strNone`None
templated_file`ForwardRef('TemplatedFile')None`None

Returns:

tuple[segments.base.BaseSegment, list[SQLBaseError]] — See return type above

get_rulepack

python
get_rulepack(
    self,
    config=None
) → base.RulePack

Get hold of a set of rules.

Parameters:

ParameterTypeDefaultDescription
self
config`fluffconfig.FluffConfigNone`None

Returns:

base.RulePack — See return type above

lint

python
lint(
    self,
    tree,
    config=None,
    fname=None,
    templated_file=None
) → list[SQLBaseError]

Return just the violations from lintfix when we're only linting.

Parameters:

ParameterTypeDefaultDescription
self
treesegments.base.BaseSegment
config`fluffconfig.FluffConfigNone`None
fname`strNone`None
templated_file`ForwardRef('TemplatedFile')None`None

Returns:

list[SQLBaseError] — See return type above

lint_fix_parsed

python
lint_fix_parsed(
    tree,
    config,
    rule_pack,
    fix=False,
    fname=None,
    templated_file=None,
    formatter=None
) → tuple[segments.base.BaseSegment, list[SQLBaseError], noqa.IgnoreMask | None, list[tuple[str, str, float]]]

Lint and optionally fix a tree object.

Parameters:

ParameterTypeDefaultDescription
treesegments.base.BaseSegment
configfluffconfig.FluffConfig
rule_packbase.RulePack
fixboolFalse
fname`strNone`None
templated_file`ForwardRef('TemplatedFile')None`None
formatter`FormatterInterfaceNone`None

Returns:

tuple[segments.base.BaseSegment, list[SQLBaseError], noqa.IgnoreMask | None, list[tuple[str, str, float]]] — See return type above

lint_parsed

python
lint_parsed(
    parsed,
    rule_pack,
    fix=False,
    formatter=None,
    encoding="utf8"
) → linted_file.LintedFile

Lint a ParsedString and return a LintedFile.

Parameters:

ParameterTypeDefaultDescription
parsedcommon.ParsedString
rule_packbase.RulePack
fixboolFalse
formatter`FormatterInterfaceNone`None
encodingstr"utf8"

Returns:

linted_file.LintedFile — See return type above

lint_path

python
lint_path(
    self,
    path,
    fix=False,
    ignore_non_existent_files=False,
    ignore_files=True,
    processes=None
) → linted_dir.LintedDir

Lint a path.

Parameters:

ParameterTypeDefaultDescription
self
pathstr
fixboolFalse
ignore_non_existent_filesboolFalse
ignore_filesboolTrue
processes`intNone`None

Returns:

linted_dir.LintedDir — See return type above

lint_paths

python
lint_paths(
    self,
    paths,
    fix=False,
    ignore_non_existent_files=False,
    ignore_files=True,
    processes=None,
    apply_fixes=False,
    fixed_file_suffix="",
    fix_even_unparsable=False,
    retain_files=True
) → linting_result.LintingResult

Lint an iterable of paths.

Parameters:

ParameterTypeDefaultDescription
self
pathstuple[str, ...]
fixboolFalse
ignore_non_existent_filesboolFalse
ignore_filesboolTrue
processes`intNone`None
apply_fixesboolFalse
fixed_file_suffixstr""
fix_even_unparsableboolFalse
retain_filesboolTrue

Returns:

linting_result.LintingResult — See return type above

lint_rendered

python
lint_rendered(
    rendered,
    rule_pack,
    fix=False,
    formatter=None
) → linted_file.LintedFile

Take a RenderedFile and return a LintedFile.

Parameters:

ParameterTypeDefaultDescription
renderedcommon.RenderedFile
rule_packbase.RulePack
fixboolFalse
formatter`FormatterInterfaceNone`None

Returns:

linted_file.LintedFile — See return type above

lint_string

python
lint_string(
    self,
    in_str="",
    fname="<string input>",
    fix=False,
    config=None,
    encoding="utf8"
) → linted_file.LintedFile

Lint a string.

Parameters:

ParameterTypeDefaultDescription
self
in_strstr""
fnamestr"<string input>"
fixboolFalse
config`fluffconfig.FluffConfigNone`None
encodingstr"utf8"

Returns:

linted_file.LintedFile — LintedFile: an object representing that linted file.

lint_string_wrapped

python
lint_string_wrapped(
    self,
    string,
    fname="<string input>",
    fix=False,
    stdin_filename=None
) → linting_result.LintingResult

Lint strings directly.

Parameters:

ParameterTypeDefaultDescription
self
stringstr
fnamestr"<string input>"
fixboolFalse
stdin_filename`strNone`None

Returns:

linting_result.LintingResult — See return type above

load_raw_file_and_config

python
load_raw_file_and_config(
    fname,
    root_config
) → tuple[str, fluffconfig.FluffConfig, str]

Load a raw file and the associated config.

Parameters:

ParameterTypeDefaultDescription
fnamestr
root_configfluffconfig.FluffConfig

Returns:

tuple[str, fluffconfig.FluffConfig, str] — See return type above

parse_path

python
parse_path(
    self,
    path,
    parse_statistics=False
) → collections.abc.Iterator[common.ParsedString]

Parse a path of sql files.

NB: This a generator which will yield the result of each file within the path iteratively.

Parameters:

ParameterTypeDefaultDescription
self
pathstr
parse_statisticsboolFalse

Returns:

collections.abc.Iterator[common.ParsedString] — See return type above

parse_rendered

python
parse_rendered(
    rendered,
    parse_statistics=False
) → common.ParsedString

Parse a rendered file.

Parameters:

ParameterTypeDefaultDescription
renderedcommon.RenderedFile
parse_statisticsboolFalse

Returns:

common.ParsedString — See return type above

parse_string

python
parse_string(
    self,
    in_str,
    fname="<string>",
    config=None,
    encoding="utf-8",
    parse_statistics=False
) → common.ParsedString

Parse a string.

Parameters:

ParameterTypeDefaultDescription
self
in_strstr
fnamestr"<string>"
config`fluffconfig.FluffConfigNone`None
encodingstr"utf-8"
parse_statisticsboolFalse

Returns:

common.ParsedString — See return type above

remove_templated_errors

python
remove_templated_errors(
    linting_errors
) → list[SQLBaseError]

Filter a list of lint errors, removing those from the templated slices.

Parameters:

ParameterTypeDefaultDescription
linting_errorslist[SQLBaseError]

Returns:

list[SQLBaseError] — See return type above

render_file

python
render_file(
    self,
    fname,
    root_config
) → common.RenderedFile

Load and render a file with relevant config.

Parameters:

ParameterTypeDefaultDescription
self
fnamestr
root_configfluffconfig.FluffConfig

Returns:

common.RenderedFile — See return type above

render_string

python
render_string(
    self,
    in_str,
    fname,
    config,
    encoding
) → common.RenderedFile

Template the file.

Parameters:

ParameterTypeDefaultDescription
self
in_strstr
fnamestr
configfluffconfig.FluffConfig
encodingstr

Returns:

common.RenderedFile — See return type above

rule_tuples

python
rule_tuples(
    self
) → list[common.RuleTuple]

A simple pass through to access the rule tuples of the rule set.

Parameters:

ParameterTypeDefaultDescription
self

Returns:

list[common.RuleTuple] — See return type above

PyLexer

The Lexer class actually does the lexing step.

Parameters:

ParameterTypeDefaultDescription
config`fluffconfig.FluffConfigNone`None
last_resort_lexer`lexer.StringLexerNone`None
dialect`strNone`None

Methods

elements_to_segments

python
elements_to_segments(
    self,
    elements,
    templated_file
) → tuple[segments.raw.RawSegment, ...]

Convert a tuple of lexed elements into a tuple of segments.

Parameters:

ParameterTypeDefaultDescription
self
elementslist[lexer.TemplateElement]
templated_filebase.TemplatedFile

Returns:

tuple[segments.raw.RawSegment, ...] — See return type above

lex

python
lex(
    self,
    raw
) → tuple[tuple[segments.base.BaseSegment, ...], list[SQLLexError]]

Take a string or TemplatedFile and return segments.

If we fail to match the whole string, then we must have found something that we cannot lex. If that happens we should package it up as unlexable and keep track of the exceptions.

Parameters:

ParameterTypeDefaultDescription
self
raw`strbase.TemplatedFile`

Returns:

tuple[tuple[segments.base.BaseSegment, ...], list[SQLLexError]] — See return type above

lex_match

python
lex_match(
    forward_string,
    lexer_matchers
) → lexer.LexMatch

Iteratively match strings using the selection of submatchers.

Parameters:

ParameterTypeDefaultDescription
forward_stringstr
lexer_matcherslist[lexer.StringLexer]

Returns:

lexer.LexMatch — See return type above

map_template_slices

python
map_template_slices(
    elements,
    template
) → list[lexer.TemplateElement]

Create a tuple of TemplateElement from a tuple of LexedElement.

This adds slices in the templated file to the original lexed elements. We'll need this to work out the position in the source file.

Parameters:

ParameterTypeDefaultDescription
elementslist[lexer.LexedElement]
templatebase.TemplatedFile

Returns:

list[lexer.TemplateElement] — See return type above

violations_from_segments

python
violations_from_segments(
    segments
) → list[SQLLexError]

Generate any lexing errors for any unlexables.

Parameters:

ParameterTypeDefaultDescription
segmentstuple[segments.raw.RawSegment, ...]

Returns:

list[SQLLexError] — See return type above

Parser

Instantiates parsed queries from a sequence of lexed raw segments.

Parameters:

ParameterTypeDefaultDescription
config`fluffconfig.FluffConfigNone`None
dialect`strNone`None

Methods

parse

python
parse(
    self,
    segments,
    fname=None,
    parse_statistics=False
) → ForwardRef('BaseSegment') | None

Parse a series of lexed tokens using the current dialect.

Parameters:

ParameterTypeDefaultDescription
self
segmentscollections.abc.Sequence['BaseSegment']
fname`strNone`None
parse_statisticsboolFalse

Returns:

ForwardRef('BaseSegment') | None — See return type above

Released under the MIT License.