Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent duplicated rule exception when importing rules and filters #87

Merged
merged 5 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to
## [Unreleased]

- Documenting support for python 3.13. (#86)
- Ignore imported rules and filters when building the rule registry. (#85)
npeshkov marked this conversation as resolved.
Show resolved Hide resolved

## [0.8.0] - 2024-11-12

Expand Down
3 changes: 3 additions & 0 deletions src/dbt_score/rule_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ def _load(self, namespace_name: str) -> None:
module = importlib.import_module(module_name)
for obj_name in dir(module):
obj = module.__dict__[obj_name]
# Skip adding objects imported from other modules
if type(obj) is type and module.__name__ != obj.__module__:
continue
if type(obj) is type and issubclass(obj, Rule) and obj is not Rule:
self._add_rule(obj)
if (
Expand Down
4 changes: 2 additions & 2 deletions tests/resources/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ severity=4
[tool.dbt-score.rules."tests.conftest.rule_with_config"]
model_name="model2"

[tool.dbt-score.rules."tests.rules.example.rule_test_example"]
[tool.dbt-score.rules."tests.rules.rules.rule_test_example"]
severity=4
rule_filter_names=["tests.rules.example.skip_model1"]
rule_filter_names=["tests.rules.rule_filters.skip_model1"]
14 changes: 0 additions & 14 deletions tests/rules/example.py

This file was deleted.

15 changes: 15 additions & 0 deletions tests/rules/rule_filters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"""Example rule filters."""

from dbt_score import Model, rule_filter


@rule_filter
def skip_model1(model: Model) -> bool:
"""An example filter."""
return model.name != "model1"


@rule_filter
def skip_schemaX(model: Model) -> bool:
"""An example filter."""
return model.schema != "X"
10 changes: 10 additions & 0 deletions tests/rules/rules.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"""Example rules."""

from dbt_score import Model, RuleViolation, rule

from tests.rules.rule_filters import skip_schemaX


@rule(rule_filters={skip_schemaX()})
def rule_test_example(model: Model) -> RuleViolation | None:
"""An example rule."""
6 changes: 3 additions & 3 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def test_load_valid_toml_file(valid_config_path):
assert config.disabled_rules == ["foo.foo", "tests.bar"]
assert config.rules_config["foo.bar"].severity == Severity.CRITICAL
assert (
config.rules_config["tests.rules.example.rule_test_example"].severity
config.rules_config["tests.rules.rules.rule_test_example"].severity
== Severity.CRITICAL
)
assert config.badge_config.third.threshold == 6.5
Expand All @@ -28,8 +28,8 @@ def test_load_valid_toml_file(valid_config_path):
assert config.fail_project_under == 7.5
assert config.fail_any_item_under == 6.9
assert config.rules_config[
"tests.rules.example.rule_test_example"
].rule_filter_names == ["tests.rules.example.skip_model1"]
"tests.rules.rules.rule_test_example"
].rule_filter_names == ["tests.rules.rule_filters.skip_model1"]


def test_load_invalid_toml_file(caplog, invalid_config_path):
Expand Down
20 changes: 10 additions & 10 deletions tests/test_rule_catalog.py
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The ordering of objects has changed, because files were renamed:

  • example used to be before nested,
  • now rules and rule_filters come after nested.

Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ def test_rule_catalog_terminal(capsys, default_config):
stdout = capsys.readouterr().out
assert (
stdout
== """\x1B[1mtests.rules.example.rule_test_example\x1B[0m:
== """\x1B[1mtests.rules.nested.example.rule_test_nested_example\x1B[0m:
An example rule.

\x1B[1mtests.rules.nested.example.rule_test_nested_example\x1B[0m:
\x1B[1mtests.rules.rules.rule_test_example\x1B[0m:
An example rule.

"""
Expand All @@ -29,41 +29,41 @@ def test_rule_catalog_markdown(capsys, default_config):
stdout
== """# Doc for tests.rules

## `rule_test_example`
## `rule_test_nested_example`

An example rule.

??? quote "Source code"
```python
@rule()
def rule_test_example(model: Model) -> RuleViolation | None:
@rule
def rule_test_nested_example(model: Model) -> RuleViolation | None:
\"""An example rule.\"""

```

### Default configuration

```toml title="pyproject.toml"
[tool.dbt-score.rules."tests.rules.example.rule_test_example"]
[tool.dbt-score.rules."tests.rules.nested.example.rule_test_nested_example"]
severity = 2
```

## `rule_test_nested_example`
## `rule_test_example`

An example rule.

??? quote "Source code"
```python
@rule
def rule_test_nested_example(model: Model) -> RuleViolation | None:
@rule(rule_filters={skip_schemaX()})
def rule_test_example(model: Model) -> RuleViolation | None:
\"""An example rule.\"""

```

### Default configuration

```toml title="pyproject.toml"
[tool.dbt-score.rules."tests.rules.nested.example.rule_test_nested_example"]
[tool.dbt-score.rules."tests.rules.rules.rule_test_example"]
severity = 2
```

Expand Down
17 changes: 9 additions & 8 deletions tests/test_rule_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ def test_rule_registry_discovery(default_config):
r = RuleRegistry(default_config)
r._load("tests.rules")
assert sorted(r._rules.keys()) == [
"tests.rules.example.rule_test_example",
"tests.rules.nested.example.rule_test_nested_example",
"tests.rules.rules.rule_test_example",
]
assert list(r._rule_filters.keys()) == [
"tests.rules.rule_filters.skip_model1",
"tests.rules.rule_filters.skip_schemaX",
]
assert list(r._rule_filters.keys()) == ["tests.rules.example.skip_model1"]


def test_disabled_rule_registry_discovery():
Expand All @@ -25,7 +28,7 @@ def test_disabled_rule_registry_discovery():
r = RuleRegistry(config)
r._load("tests.rules")
assert sorted(r._rules.keys()) == [
"tests.rules.example.rule_test_example",
"tests.rules.rules.rule_test_example",
]


Expand All @@ -35,9 +38,7 @@ def test_configured_rule_registry_discovery(valid_config_path):
config._load_toml_file(str(valid_config_path))
r = RuleRegistry(config)
r._load("tests.rules")
assert (
r.rules["tests.rules.example.rule_test_example"].severity == Severity.CRITICAL
)
assert r.rules["tests.rules.rules.rule_test_example"].severity == Severity.CRITICAL


def test_rule_registry_no_duplicates(default_config):
Expand All @@ -63,5 +64,5 @@ def test_rule_registry_rule_filters(valid_config_path, model1, model2):
r._load("tests.rules")
r._load_filters_into_rules()

assert not r.rules["tests.rules.example.rule_test_example"].should_evaluate(model1)
assert r.rules["tests.rules.example.rule_test_example"].should_evaluate(model2)
assert not r.rules["tests.rules.rules.rule_test_example"].should_evaluate(model1)
assert r.rules["tests.rules.rules.rule_test_example"].should_evaluate(model2)
Loading