sqlfluff.core.rules.base
: Base Rule Classes¶
Implements the base rule class.
Rules crawl through the trees returned by the parser and evaluate particular rules.
The intent is that it should be possible for the rules to be expressed as simply as possible, with as much of the complexity abstracted away.
The evaluation function should take enough arguments that it can evaluate the position of the given segment in relation to its neighbors, and that the segment which finally “triggers” the error, should be the one that would be corrected OR if the rule relates to something that is missing, then it should flag on the segment FOLLOWING, the place that the desired element is missing.
- class BaseRule(code: str, description: str, **kwargs: Any)¶
The base class for a rule.
- Parameters:
code (
str
) – The identifier for this rule, used in inclusion or exclusion.description (
str
) – A human readable description of what this rule does. It will be displayed when any violations are found.
- crawl(tree: BaseSegment, dialect: Dialect, fix: bool, templated_file: TemplatedFile | None, ignore_mask: IgnoreMask | None, fname: str | None, config: FluffConfig) Tuple[List[SQLLintError], Tuple[RawSegment, ...], List[LintFix], Dict[str, Any] | None] ¶
Run the rule on a given tree.
- Returns:
A tuple of (vs, raw_stack, fixes, memory)
- static discard_unsafe_fixes(lint_result: LintResult, templated_file: TemplatedFile | None) None ¶
Remove (discard) LintResult fixes if they are “unsafe”.
By removing its fixes, a LintResult will still be reported, but it will be treated as _unfixable_.
- static filter_meta(segments: Sequence[BaseSegment], keep_meta: bool = False) Tuple[BaseSegment, ...] ¶
Filter the segments to non-meta.
Or optionally the opposite if keep_meta is True.
- classmethod get_config_ref() str ¶
Return the config lookup ref for this rule.
If a name is defined, it’s the name - otherwise the code.
The name is a much more understandable reference and so makes config files more readable. For backward compatibility however we also support the rule code for those without names.
- classmethod get_parent_of(segment: BaseSegment, root_segment: BaseSegment) BaseSegment | None ¶
Return the segment immediately containing segment.
NB: This is recursive.
- Parameters:
segment – The segment to look for.
root_segment – Some known parent of the segment we’re looking for (although likely not the direct parent in question).
- static split_comma_separated_string(raw: str | List[str]) List[str] ¶
Converts comma separated string to List, stripping whitespace.
- class LintResult(anchor: BaseSegment | None = None, fixes: List[LintFix] | None = None, memory: Any | None = None, description: str | None = None, source: str | None = None)¶
A class to hold the results of a rule evaluation.
- Parameters:
anchor (
BaseSegment
, optional) – A segment which represents the position of the problem. NB: Each fix will also hold its own reference to position, so this position is mostly for alerting the user to where the problem is.fixes (
list
ofLintFix
, optional) – An array of any fixes which would correct this issue. If not present then it’s assumed that this issue will have to manually fixed.memory (
dict
, optional) – An object which stores any working memory for the rule. The memory returned in any LintResult will be passed as an input to the next segment to be crawled.description (
str
, optional) – A description of the problem identified as part of this result. This will override the description of the rule as what gets reported to the user with the problem if provided.source (
str
, optional) – A string identifier for what generated the result. Within larger libraries like reflow this can be useful for tracking where a result came from.
- class RuleGhost(code, name, description)¶
- code¶
Alias for field number 0
- description¶
Alias for field number 2
- name¶
Alias for field number 1
- class RuleLoggingAdapter(logger, extra=None)¶
A LoggingAdapter for rules which adds the code of the rule to it.
- process(msg: str, kwargs: Any) Tuple[str, Any] ¶
Add the code element to the logging message before emit.
- class RuleManifest(code: str, name: str, description: str, groups: Tuple[str, ...], aliases: Tuple[str, ...], rule_class: Type[BaseRule])¶
Element in the rule register.
- class RuleMetaclass(name: str, bases: List[BaseRule], class_dict: Dict[str, Any])¶
The metaclass for rules.
This metaclass provides provides auto-enrichment of the rule docstring so that examples, groups, aliases and names are added.
The reason we enrich the docstring is so that it can be picked up by autodoc and all be displayed in the sqlfluff docs.
- class RulePack(rules: List[BaseRule], reference_map: Dict[str, Set[str]])¶
A bundle of rules to be applied.
This contains a set of rules, post filtering but also contains the mapping required to interpret any noqa messages found in files.
The reason for this object is that rules are filtered and instantiated into this pack in the main process when running in multi-processing mode so that user defined rules can be used without reference issues.
- reference_map¶
A mapping of rule references to the codes they refer to, e.g. {“my_ref”: {“LT01”, “LT02”}}. The references (i.e. the keys) may be codes, groups, aliases or names. The values of the mapping are sets of rule codes only. This object acts as a lookup to be able to translate selectors (which may contain diverse references) into a consolidated list of rule codes. This mapping contains the full set of rules, rather than just the filtered set present in the rules attribute.
- Type:
dict
- codes() Iterator[str] ¶
Returns an iterator through the codes contained in the pack.
- class RuleSet(name: str, config_info: Dict[str, Dict[str, Any]])¶
Class to define a ruleset.
A rule set is instantiated on module load, but the references to each of its classes are instantiated at runtime. This means that configuration values can be passed to those rules live and be responsive to any changes in configuration from the path that the file is in.
Rules should be fetched using the
get_rulelist()
command which also handles any filtering (i.e. allowlisting and denylisting).New rules should be added to the instance of this class using the
register()
decorator. That decorator registers the class, but also performs basic type and name-convention checks.The code for the rule will be parsed from the name, the description from the docstring. The eval function is assumed that it will be overridden by the subclass, and the parent class raises an error on this function if not overridden.
- get_rulepack(config: FluffConfig) RulePack ¶
Use the config to return the appropriate rules.
We use the config both for allowlisting and denylisting, but also for configuring the rules given the given config.
- register(cls: Type[BaseRule], plugin: PluginSpec | None = None) Type[BaseRule] ¶
Decorate a class with this to add it to the ruleset.
@myruleset.register class Rule_LT01(BaseRule): "Description of rule." def eval(self, **kwargs): return LintResult()
We expect that rules are defined as classes with the name Rule_XXXX where XXXX is of the form LNNN, where L is a letter (literally L for linting by default) and N is a three digit number.
If this receives classes by any other name, then it will raise a
ValueError
.
- rule_reference_map() Dict[str, Set[str]] ¶
Generate a rule reference map for looking up rules.
Generate the master reference map. The priority order is: codes > names > groups > aliases (i.e. if there’s a collision between a name and an alias - we assume the alias is wrong)