Skip to content

Commit d79c01a

Browse files
chore: Raise on primary key dtype mimsatch
1 parent 05b4df9 commit d79c01a

2 files changed

Lines changed: 7 additions & 13 deletions

File tree

diffly/comparison.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from __future__ import annotations
55

66
import datetime as dt
7-
import warnings
87
from collections.abc import Iterable, Mapping, Sequence
98
from functools import cached_property
109
from typing import TYPE_CHECKING, Literal, Self, overload
@@ -165,18 +164,14 @@ def _init_with_validation(
165164
"The columns are not a primary key for the right data frame."
166165
)
167166

168-
# Try joining empty frames to check if the primary key columns are
169-
# compatible. If not, we set the primary key to `None` and emit an
170-
# appropriate warning.
167+
# Check that the primary key dtypes are compatible for joining.
171168
try:
172169
left_schema.to_frame().join(right_schema.to_frame(), on=primary_key)
173170
except pl.exceptions.SchemaError as e:
174-
warnings.warn(
175-
"`primary_key` is set to None as the primary key of the left and "
176-
"right tables have incompatible data types: "
177-
+ str(e).split("\n")[0],
178-
)
179-
primary_key = None
171+
raise PrimaryKeyError(
172+
"Primary key columns have incompatible dtypes between left and "
173+
"right: " + str(e).split("\n")[0]
174+
) from e
180175

181176
# Assign other relevant attributes
182177
schemas = Schemas(left_schema, right_schema)

tests/test_dataframe_comparison.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,12 @@ def test_pk_violation() -> None:
4545

4646

4747
def test_incompatible_primary_key_dtypes() -> None:
48-
with pytest.warns(UserWarning, match=".*datatypes of join keys don't match.*"):
49-
comparison = compare_frames(
48+
with pytest.raises(PrimaryKeyError, match="incompatible dtypes"):
49+
compare_frames(
5050
pl.DataFrame({"key": ["tiger"], "speed_kph": [5.0]}),
5151
pl.DataFrame({"key": [1], "speed_kph": [5.0]}),
5252
primary_key=["key"],
5353
)
54-
comparison.summary()
5554

5655

5756
def test_incomplete_mapping() -> None:

0 commit comments

Comments
 (0)