Skip to content

Commit a5d25ef

Browse files
authored
feat: add context to linter rules (#4247)
1 parent 3c1a44a commit a5d25ef

File tree

4 files changed

+19
-6
lines changed

4 files changed

+19
-6
lines changed

examples/sushi/linter/user.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import typing as t
44

5+
from sqlmesh.core.context import Context
56
from sqlmesh.core.linter.rule import Rule, RuleViolation
67
from sqlmesh.core.model import Model
78

@@ -10,4 +11,6 @@ class NoMissingOwner(Rule):
1011
"""All models should have an owner specified."""
1112

1213
def check_model(self, model: Model) -> t.Optional[RuleViolation]:
14+
assert isinstance(self.context, Context)
15+
assert len(self.context.models) > 10
1316
return self.violation() if not model.owner else None

sqlmesh/core/context.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2537,7 +2537,7 @@ def lint_models(
25372537
# Linter may be `None` if the context is not loaded yet
25382538
if linter := self._linters.get(model.project):
25392539
lint_violation, violations = (
2540-
linter.lint_model(model, console=self.console) or found_error
2540+
linter.lint_model(model, self, console=self.console) or found_error
25412541
)
25422542
if lint_violation:
25432543
found_error = True

sqlmesh/core/linter/definition.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
from sqlmesh.core.linter.rule import Rule, RuleViolation
1212
from sqlmesh.core.console import LinterConsole, get_console
1313

14+
if t.TYPE_CHECKING:
15+
from sqlmesh.core.context import GenericContext
16+
1417

1518
def select_rules(all_rules: RuleSet, rule_names: t.Set[str]) -> RuleSet:
1619
if "all" in rule_names:
@@ -52,7 +55,7 @@ def from_rules(cls, all_rules: RuleSet, config: LinterConfig) -> Linter:
5255
return Linter(config.enabled, all_rules, rules, warn_rules)
5356

5457
def lint_model(
55-
self, model: Model, console: LinterConsole = get_console()
58+
self, model: Model, context: GenericContext, console: LinterConsole = get_console()
5659
) -> t.Tuple[bool, t.List[AnnotatedRuleViolation]]:
5760
if not self.enabled:
5861
return False, []
@@ -62,8 +65,8 @@ def lint_model(
6265
rules = self.rules.difference(ignored_rules)
6366
warn_rules = self.warn_rules.difference(ignored_rules)
6467

65-
error_violations = rules.check_model(model)
66-
warn_violations = warn_rules.check_model(model)
68+
error_violations = rules.check_model(model, context)
69+
warn_violations = warn_rules.check_model(model, context)
6770

6871
all_violations: t.List[AnnotatedRuleViolation] = [
6972
AnnotatedRuleViolation(
@@ -96,11 +99,11 @@ class RuleSet(Mapping[str, type[Rule]]):
9699
def __init__(self, rules: Iterable[type[Rule]] = ()) -> None:
97100
self._underlying = {rule.name: rule for rule in rules}
98101

99-
def check_model(self, model: Model) -> t.List[RuleViolation]:
102+
def check_model(self, model: Model, context: GenericContext) -> t.List[RuleViolation]:
100103
violations = []
101104

102105
for rule in self._underlying.values():
103-
violation = rule().check_model(model)
106+
violation = rule(context).check_model(model)
104107

105108
if violation:
106109
violations.append(violation)

sqlmesh/core/linter/rule.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
import typing as t
1010

1111

12+
if t.TYPE_CHECKING:
13+
from sqlmesh.core.context import GenericContext
14+
15+
1216
class _Rule(abc.ABCMeta):
1317
def __new__(cls: Type[_Rule], clsname: str, bases: t.Tuple, attrs: t.Dict) -> _Rule:
1418
attrs["name"] = clsname.lower()
@@ -20,6 +24,9 @@ class Rule(abc.ABC, metaclass=_Rule):
2024

2125
name = "rule"
2226

27+
def __init__(self, context: GenericContext):
28+
self.context = context
29+
2330
@abc.abstractmethod
2431
def check_model(self, model: Model) -> t.Optional[RuleViolation]:
2532
"""The evaluation function that'll check for a violation of this rule."""

0 commit comments

Comments
 (0)