Skip to main content

sqlfluff-plugin-legibility

CI Codecov PyPI Python

A small SQLFluff plugin for keeping comments scarce and intentional.

Ordinary SQL and Jinja comments are reported. Projects may explicitly allow traceable or marked comments. Machine directives remain allowed. Consecutive physical comments are always reported.

It adds two non-fixing rules.

Rule Name Behavior
Legibility_L001 legibility.no_unmatched_comments Reports comments unless explicitly allowed.
Legibility_L002 legibility.no_stacked_comments Reports consecutive physical comments.

SQLFluff's built-in LT05 handles line length. This plugin does not duplicate it.

Quick start

Add the plugin to a SQL project managed with uv:

uv add --dev sqlfluff-plugin-legibility

Projects not using uv can install the same package with pip:

pip install sqlfluff-plugin-legibility

SQLFluff discovers the installed plugin automatically. Configure the rules in an existing .sqlfluff file. Choose the dialect that matches your project; the plugin does not set one.

 [sqlfluff]
 dialect = ansi
+warnings = Legibility_L001, LT05
+max_line_length = 80

Legibility_L002 is omitted from warnings, so stacked comments are errors.

Run SQLFluff:

uv run sqlfluff lint path/to/query.sql

Codes listed in warnings are reported but do not themselves make lint fail. Remove a code from warnings to enforce it as a failure. To limit linting to these rules, add the following.

 [sqlfluff]
+rules = Legibility_L001, Legibility_L002, LT05

Behavior

Comment Result
Ordinary SQL or Jinja comment Reported
Traceable comment Reported until comment_matchers allows it
Additional matched comment in the same statement Reported
Marked comment Reported until a prefix or suffix identifier allows it
Consecutive physical comments Reported by Legibility_L002
Built-in machine directive Allowed
Other machine directive Reported until directive_matchers allows it

In the examples below, - is discouraged and + is recommended.

Keep lines readable

LT05 reports lines longer than the configured limit. Wrap long selections so each column is easy to scan.

-SELECT customer_id, customer_name, billing_account_id, billing_account_status FROM customers;
+SELECT
+    customer_id,
+    customer_name,
+    billing_account_id,
+    billing_account_status
+FROM customers;

Remove ordinary comments

No comment configuration is required. By default, each ordinary SQL or Jinja comment reports Legibility_L001.

--- Explain the join.
 SELECT customers.id
 FROM customers;
-SELECT customers.id /* Keep this stable. */
+SELECT customers.id
 FROM customers;
-{# Explain the model. #}
 SELECT customers.id
 FROM customers;

The rule never edits, moves, or deletes a comment.

Allow one traceable comment

Use a regex when a ticket, decision record, or other traceable identifier makes a comment useful.

 [sqlfluff:rules:legibility.no_unmatched_comments]
+comment_matchers = \b(ENG|OPS)-[0-9]+\b

If a comment contains information the SQL cannot express, connect it to a durable record:

--- Provider retries must remain ordered.
+-- ENG-481: Provider retries must remain ordered.
 SELECT retry_id
 FROM retries;

Only one regex-matched physical comment is allowed per top-level statement. The limit resets for each new statement. A comment after a semicolon belongs to the statement on that line; a comment on the next line belongs to the next statement.

Reject stacked comments

Legibility_L002 reports every physical comment after the first in a consecutive stack. Matchers, identifiers, and machine directives do not bypass this rule.

 -- ENG-481: Provider retries must remain ordered.
--- ENG-481: Do not reorder these retries.
 SELECT retry_id
 FROM retries;

A blank line between comments ends the stack. One multiline block comment is one physical comment.

Allow a marked comment

Prefix and suffix identifiers allow individual comments. Start without identifiers and add only those required by a repository policy.

 [sqlfluff:rules:legibility.no_unmatched_comments]
+comment_prefix_identifiers = APPROVED
+comment_suffix_identifiers = @approved

The prefix must begin the comment.

--- The provider requires this ordering.
+-- APPROVED: The provider requires this ordering.
 SELECT retry_id FROM retries;

The suffix must end the comment.

--- The provider requires this ordering.
+-- The provider requires this ordering. @approved
 SELECT retry_id FROM retries;

Identifiers are case-insensitive and require a word boundary. APPROVEDLY does not match APPROVED.

Machine directives are exempt

The rule recognizes common directives without configuration:

-- sqlfluff:dialect:postgres
SELECT customer_id FROM customers;
-- depends_on: {{ ref('upstream_model') }}
SELECT customer_id FROM customers;
SELECT /*+ INDEX(customers customers_pk) */ customer_id
FROM customers; -- noqa: LT05

Projects can allow another directive syntax:

 [sqlfluff:rules:legibility.no_unmatched_comments]
+directive_matchers = ^generated:
-- generated: do not edit
SELECT customer_id FROM customers;

Configuration reference

Setting Default Purpose
comment_matchers Empty Comma-separated, case-insensitive regular expressions; allows one matched comment per top-level statement
comment_prefix_identifiers Empty Comma-separated identifiers allowed at the start of a comment
comment_suffix_identifiers Empty Comma-separated identifiers allowed at the end of a comment
directive_matchers Empty Comma-separated, case-insensitive regular expressions for additional machine directives

Severity

SQLFluff treats enabled rules as errors unless their codes appear in warnings. Agent sessions and CI must leave Legibility_L002 out of warnings. Developers may add it to warnings for non-blocking local feedback.

 [sqlfluff]
-warnings = Legibility_L001, LT05
+warnings = Legibility_L001, Legibility_L002, LT05

Agent policy

Agents should not add or update comments. They may remove or move comments. They must treat Legibility_L002 as an error and must not add a marker or directive to bypass Legibility_L001.

License

MIT

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sqlfluff_plugin_legibility-0.1.0.tar.gz (12.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sqlfluff_plugin_legibility-0.1.0-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file sqlfluff_plugin_legibility-0.1.0.tar.gz.

File metadata

  • Download URL: sqlfluff_plugin_legibility-0.1.0.tar.gz
  • Upload date:
  • Size: 12.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.29 {"installer":{"name":"uv","version":"0.11.29","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for sqlfluff_plugin_legibility-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d7536a3b7e5b33e3024c88044eaf5a6157522348252d033fb703c4c6da97d6b2
MD5 68c430773d7ffa7a64c0126acd9eb59b
BLAKE2b-256 a0044ae787e0db3521654729da44b40aa18d04d48c60839c96b78a1b716344cd

See more details on using hashes here.

File details

Details for the file sqlfluff_plugin_legibility-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: sqlfluff_plugin_legibility-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 9.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.29 {"installer":{"name":"uv","version":"0.11.29","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for sqlfluff_plugin_legibility-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1a1729c2d0fbbea1c0827da9eb99a85295a3ed36b6aae5964b7767e21d3b1a10
MD5 496cce14f71c65ef49a14b37e40615e3
BLAKE2b-256 f202b3227dde1f0f821e7c833fd3058eab16ce7c928d5978d726398aca22c703

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page