Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion sqlmesh/core/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -2685,7 +2685,25 @@ def show_linter_violations(
self, violations: t.List[RuleViolation], model: Model, is_error: bool = False
) -> None:
severity = "errors" if is_error else "warnings"
violations_msg = "\n".join(f" - {violation}" for violation in violations)

# Sort violations by line, then alphabetically the name of the violation
# Violations with no range go first
sorted_violations = sorted(
violations,
key=lambda v: (
v.violation_range.start.line if v.violation_range else -1,
v.rule.name.lower(),
),
)
violations_text = [
(
f" - Line {v.violation_range.start.line + 1}: {v.rule.name} - {v.violation_msg}"
if v.violation_range
else f" - {v.rule.name}: {v.violation_msg}"
)
for v in sorted_violations
]
violations_msg = "\n".join(violations_text)
msg = f"Linter {severity} for {model._path}:\n{violations_msg}"

if is_error:
Expand Down
5 changes: 3 additions & 2 deletions sqlmesh/core/linter/definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,10 @@ def check_model(self, model: Model, context: GenericContext) -> t.List[RuleViola

for rule in self._underlying.values():
violation = rule(context).check_model(model)

if isinstance(violation, RuleViolation):
violation = [violation]
if violation:
violations.append(violation)
violations.extend(violation)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks wrong. Are we adding the same violation twice? Haven't we added it on line 112 already?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No it turns the RuleViolation into a t.List[RulationViolation] before adding it. and if it's None it doesn't get extended.


return violations

Expand Down
4 changes: 3 additions & 1 deletion sqlmesh/core/linter/rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ def __init__(self, context: GenericContext):
self.context = context

@abc.abstractmethod
def check_model(self, model: Model) -> t.Optional[RuleViolation]:
def check_model(
self, model: Model
) -> t.Optional[t.Union[RuleViolation, t.List[RuleViolation]]]:
"""The evaluation function that'll check for a violation of this rule."""

@property
Expand Down
2 changes: 1 addition & 1 deletion tests/core/test_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -1952,7 +1952,7 @@ def assert_cached_violations_exist(cache: OptimizedQueryCache, model: Model):
ctx.plan(environment="dev", auto_apply=True, no_prompts=True)

assert (
"""noselectstar: Query should not contain SELECT * on its outer most projections"""
"""noselectstar - Query should not contain SELECT * on its outer most projections"""
in mock_logger.call_args[0][0]
)

Expand Down