Skip to content

Commit 9935c21

Browse files
committed
Prevent duplicated rule exception when importing rules and filters
1 parent 9f6aa67 commit 9935c21

File tree

7 files changed

+37
-1
lines changed

7 files changed

+37
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to
99
## [Unreleased]
1010

1111
- Documenting support for python 3.13. (#86)
12+
- Ignore imported rules and filters when building the rule registry. (#85)
1213

1314
## [0.8.0] - 2024-11-12
1415

src/dbt_score/rule_registry.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ def _load(self, namespace_name: str) -> None:
6262
module = importlib.import_module(module_name)
6363
for obj_name in dir(module):
6464
obj = module.__dict__[obj_name]
65+
# skip adding objects imported from other modules
66+
if type(obj) is type and module.__name__ != obj.__module__:
67+
continue
6568
if type(obj) is type and issubclass(obj, Rule) and obj is not Rule:
6669
self._add_rule(obj)
6770
if (

tests/rules/imported/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""Package for testing discovery of imported rules and filters."""

tests/rules/imported/imported.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"""Imported rules and filters."""
2+
3+
from tests.rules.imported.original import (
4+
rule_filter_to_be_imported, # noqa: F401
5+
rule_to_be_imported, # noqa: F401
6+
)

tests/rules/imported/original.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""Rules and filters to be imported."""
2+
3+
4+
from dbt_score import Model, RuleViolation, rule, rule_filter
5+
6+
7+
@rule
8+
def rule_to_be_imported(model: Model) -> RuleViolation | None:
9+
"""An example rule."""
10+
11+
12+
@rule_filter
13+
def rule_filter_to_be_imported(model: Model) -> bool: # type: ignore[empty-body]
14+
"""An example filter."""

tests/test_rule_catalog.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
def test_rule_catalog_terminal(capsys, default_config):
77
"""Test rule catalog with the terminal formatter."""
88
default_config.overload({"rule_namespaces": ["tests.rules"]})
9+
default_config.overload(
10+
{"disabled_rules": ["tests.rules.imported.original.rule_to_be_imported"]}
11+
)
912
display_catalog(default_config, "Doc for tests.rules", "terminal")
1013
stdout = capsys.readouterr().out
1114
assert (
@@ -23,6 +26,9 @@ def test_rule_catalog_terminal(capsys, default_config):
2326
def test_rule_catalog_markdown(capsys, default_config):
2427
"""Test rule catalog with the markdown formatter."""
2528
default_config.overload({"rule_namespaces": ["tests.rules"]})
29+
default_config.overload(
30+
{"disabled_rules": ["tests.rules.imported.original.rule_to_be_imported"]}
31+
)
2632
display_catalog(default_config, "Doc for tests.rules", "markdown")
2733
stdout = capsys.readouterr().out
2834
assert (

tests/test_rule_registry.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,13 @@ def test_rule_registry_discovery(default_config):
1313
r._load("tests.rules")
1414
assert sorted(r._rules.keys()) == [
1515
"tests.rules.example.rule_test_example",
16+
"tests.rules.imported.original.rule_to_be_imported",
1617
"tests.rules.nested.example.rule_test_nested_example",
1718
]
18-
assert list(r._rule_filters.keys()) == ["tests.rules.example.skip_model1"]
19+
assert list(r._rule_filters.keys()) == [
20+
"tests.rules.example.skip_model1",
21+
"tests.rules.imported.original.rule_filter_to_be_imported",
22+
]
1923

2024

2125
def test_disabled_rule_registry_discovery():
@@ -26,6 +30,7 @@ def test_disabled_rule_registry_discovery():
2630
r._load("tests.rules")
2731
assert sorted(r._rules.keys()) == [
2832
"tests.rules.example.rule_test_example",
33+
"tests.rules.imported.original.rule_to_be_imported",
2934
]
3035

3136

0 commit comments

Comments
 (0)