Skip to content

Commit

Permalink
Prevent duplicated rule exception when importing rules and filters
Browse files Browse the repository at this point in the history
  • Loading branch information
npeshkov committed Dec 13, 2024
1 parent 9f6aa67 commit 9935c21
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 1 deletion.
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)

## [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
1 change: 1 addition & 0 deletions tests/rules/imported/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Package for testing discovery of imported rules and filters."""
6 changes: 6 additions & 0 deletions tests/rules/imported/imported.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"""Imported rules and filters."""

from tests.rules.imported.original import (
rule_filter_to_be_imported, # noqa: F401
rule_to_be_imported, # noqa: F401
)
14 changes: 14 additions & 0 deletions tests/rules/imported/original.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"""Rules and filters to be imported."""


from dbt_score import Model, RuleViolation, rule, rule_filter


@rule
def rule_to_be_imported(model: Model) -> RuleViolation | None:
"""An example rule."""


@rule_filter
def rule_filter_to_be_imported(model: Model) -> bool: # type: ignore[empty-body]
"""An example filter."""
6 changes: 6 additions & 0 deletions tests/test_rule_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
def test_rule_catalog_terminal(capsys, default_config):
"""Test rule catalog with the terminal formatter."""
default_config.overload({"rule_namespaces": ["tests.rules"]})
default_config.overload(
{"disabled_rules": ["tests.rules.imported.original.rule_to_be_imported"]}
)
display_catalog(default_config, "Doc for tests.rules", "terminal")
stdout = capsys.readouterr().out
assert (
Expand All @@ -23,6 +26,9 @@ def test_rule_catalog_terminal(capsys, default_config):
def test_rule_catalog_markdown(capsys, default_config):
"""Test rule catalog with the markdown formatter."""
default_config.overload({"rule_namespaces": ["tests.rules"]})
default_config.overload(
{"disabled_rules": ["tests.rules.imported.original.rule_to_be_imported"]}
)
display_catalog(default_config, "Doc for tests.rules", "markdown")
stdout = capsys.readouterr().out
assert (
Expand Down
7 changes: 6 additions & 1 deletion tests/test_rule_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ def test_rule_registry_discovery(default_config):
r._load("tests.rules")
assert sorted(r._rules.keys()) == [
"tests.rules.example.rule_test_example",
"tests.rules.imported.original.rule_to_be_imported",
"tests.rules.nested.example.rule_test_nested_example",
]
assert list(r._rule_filters.keys()) == ["tests.rules.example.skip_model1"]
assert list(r._rule_filters.keys()) == [
"tests.rules.example.skip_model1",
"tests.rules.imported.original.rule_filter_to_be_imported",
]


def test_disabled_rule_registry_discovery():
Expand All @@ -26,6 +30,7 @@ def test_disabled_rule_registry_discovery():
r._load("tests.rules")
assert sorted(r._rules.keys()) == [
"tests.rules.example.rule_test_example",
"tests.rules.imported.original.rule_to_be_imported",
]


Expand Down

0 comments on commit 9935c21

Please sign in to comment.