Skip to content

Commit

Permalink
Get rid of self in regular method
Browse files Browse the repository at this point in the history
  • Loading branch information
jochemvandooren committed Mar 18, 2024
1 parent dce673b commit c14d7a7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
10 changes: 7 additions & 3 deletions src/dbt_score/rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def evaluate(self, model: Model) -> RuleViolation | None:
def rule(
description: str | None = None,
severity: Severity = Severity.MEDIUM,
) -> Callable[[Callable[[Any, Model], RuleViolation | None]], Type[Rule]]:
) -> Callable[[Callable[[Model], RuleViolation | None]], Type[Rule]]:
"""Rule decorator.
The rule decorator creates a rule class (subclass of Rule) and returns it.
Expand All @@ -54,7 +54,7 @@ def rule(
"""

def decorator_rule(
func: Callable[[Any, Model], RuleViolation | None],
func: Callable[[Model], RuleViolation | None],
) -> Type[Rule]:
"""Decorator function."""
if func.__doc__ is None and description is None:
Expand All @@ -65,14 +65,18 @@ def decorator_rule(
func.__doc__.split("\n")[0] if func.__doc__ else None
)

def wrapped_func(self: Rule, *args: Any, **kwargs: Any) -> RuleViolation | None:
"""Wrap func to add `self`."""
return func(*args, **kwargs)

# Create the rule class inheriting from Rule.
rule_class = type(
func.__name__,
(Rule,),
{
"description": rule_description,
"severity": severity,
"evaluate": func,
"evaluate": wrapped_func,
},
)

Expand Down
10 changes: 5 additions & 5 deletions src/dbt_score/rules/example_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def evaluate(self, model: Model) -> RuleViolation | None:


@rule()
def has_owner(self: Rule, model: Model) -> RuleViolation | None:
def has_owner(model: Model) -> RuleViolation | None:
"""A model should have an owner defined."""
if "owner" not in model.meta:
return RuleViolation("Define the owner of the model in the meta section.")
Expand All @@ -33,7 +33,7 @@ def has_owner(self: Rule, model: Model) -> RuleViolation | None:


@rule()
def has_primary_key(self: Rule, model: Model) -> RuleViolation | None:
def has_primary_key(model: Model) -> RuleViolation | None:
"""A model should have a primary key defined, unless it's a view."""
if not model.config.get("materialized") == "picnic_view":
has_pk = False
Expand All @@ -49,7 +49,7 @@ def has_primary_key(self: Rule, model: Model) -> RuleViolation | None:


@rule()
def primary_key_has_uniqueness_test(self: Rule, model: Model) -> RuleViolation | None:
def primary_key_has_uniqueness_test(model: Model) -> RuleViolation | None:
"""Primary key columns should have a uniqueness test defined."""
columns_with_pk = []
if model.config.get("materialized") == "view":
Expand All @@ -66,7 +66,7 @@ def primary_key_has_uniqueness_test(self: Rule, model: Model) -> RuleViolation |


@rule()
def columns_have_description(self: Rule, model: Model) -> RuleViolation | None:
def columns_have_description(model: Model) -> RuleViolation | None:
"""All columns of a model should have a description."""
invalid_column_names = [
column.name for column in model.columns if not column.description
Expand All @@ -81,7 +81,7 @@ def columns_have_description(self: Rule, model: Model) -> RuleViolation | None:


@rule(description="A model should have at least one test defined.")
def has_test(self: Rule, model: Model) -> RuleViolation | None:
def has_test(model: Model) -> RuleViolation | None:
"""A model should have at least one model-level or column-level test defined.
This does not include singular tests, which are tests defined in a separate .sql
Expand Down

0 comments on commit c14d7a7

Please sign in to comment.