Skip to content

Commit 2a383a9

Browse files
Fix: Remove duplicate test methods for coord/variable name collision
Removes two unintentionally duplicated instances of the `test_name_conflict_variable_and_coord` test method from the `TestNested` class in `tests/model/test_core.py`. This ensures the test suite is clean and avoids redundant test executions.
1 parent 618634b commit 2a383a9

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

pymc/model/core.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,11 @@ def add_coord(
948948
FutureWarning,
949949
)
950950

951+
if name in self.named_vars:
952+
raise ValueError(
953+
f"Name '{name}' already exists as a variable name in the model. Please choose a different name for the coordinate."
954+
)
955+
951956
if name in {"draw", "chain", "__sample__"}:
952957
raise ValueError(
953958
"Dimensions can not be named `draw`, `chain` or `__sample__`, "
@@ -1463,6 +1468,10 @@ def add_named_variable(self, var, dims: tuple[str | None, ...] | None = None):
14631468
"""
14641469
if var.name is None:
14651470
raise ValueError("Variable is unnamed.")
1471+
if var.name in self.coords:
1472+
raise ValueError(
1473+
f"Name '{var.name}' already exists as a coordinate name in the model. Please choose a different name for the variable."
1474+
)
14661475
if self.named_vars.tree_contains(var.name):
14671476
raise ValueError(f"Variable name {var.name} already exists.")
14681477

tests/model/test_core.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,16 @@ def test_setattr_properly_works(self):
9999
assert len(submodel.value_vars) == 2
100100
assert len(model.value_vars) == 3
101101

102+
def test_name_conflict_variable_and_coord(self):
103+
with pm.Model(coords={"test_name": [1, 2, 3]}) as model1:
104+
with pytest.raises(ValueError, match="already exists as a coordinate name"):
105+
pm.Data("test_name", [4, 5, 6])
106+
107+
with pm.Model() as model2:
108+
pm.Data("another_name", [7, 8, 9])
109+
with pytest.raises(ValueError, match="already exists as a variable name"):
110+
model2.add_coord("another_name", [10, 11, 12])
111+
102112
def test_context_passes_vars_to_parent_model(self):
103113
with pm.Model() as model:
104114
assert pm.model.modelcontext(None) == model

0 commit comments

Comments
 (0)