Skip to content

Commit dfabdd5

Browse files
committed
get() to raise ValueError instead of returning None with bad params; linter fixes
1 parent 591890a commit dfabdd5

File tree

3 files changed

+11
-7
lines changed

3 files changed

+11
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Unreleased
44

55
- `DiffSync.get_by_uids()` now raises `ObjectNotFound` if any of the provided uids cannot be located.
6+
- `DiffSync.get()` raises `ObjectNotFound` or `ValueError` on failure, instead of returning `None`.
67
- #34 - in diff dicts, change keys `src`/`dst`/`_src`/`_dst` to `-` and `+`
78
- #37 - add `sync_complete` callback, triggered on `sync_from` completion with changes.
89
- #41 - add `summary` API for Diff and DiffElement objects.

diffsync/__init__.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ def _sync_from_diff_element(
549549
log = logger or self._log
550550
object_class = getattr(self, element.type)
551551
try:
552-
obj = self.get(object_class, element.keys)
552+
obj: Optional[DiffSyncModel] = self.get(object_class, element.keys)
553553
except ObjectNotFound:
554554
obj = None
555555
# Get the attributes that actually differ between source and dest
@@ -654,6 +654,7 @@ def get(
654654
identifier: Unique ID of the object to retrieve, or dict of unique identifier keys/values
655655
656656
Raises:
657+
ValueError: if obj is a str and identifier is a dict (can't convert dict into a uid str without a model class)
657658
ObjectNotFound: if the requested object is not present
658659
"""
659660
if isinstance(obj, str):
@@ -671,11 +672,10 @@ def get(
671672
elif object_class:
672673
uid = object_class.create_unique_id(**identifier)
673674
else:
674-
self._log.warning(
675-
f"Tried to look up a {modelname} by identifier {identifier}, "
676-
"but don't know how to convert that to a uid string",
675+
raise ValueError(
676+
f"Invalid args: ({obj}, {identifier}): "
677+
f"either {obj} should be a class/instance or {identifier} should be a str"
677678
)
678-
return None
679679

680680
if uid not in self._data[modelname]:
681681
raise ObjectNotFound(f"{modelname} {uid} not present in {self.name}")

tests/unit/test_diffsync.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,9 @@ def test_diffsync_get_with_generic_model(generic_diffsync, generic_diffsync_mode
8989
# The generic_diffsync_model has an empty identifier/unique-id
9090
assert generic_diffsync.get(DiffSyncModel, "") == generic_diffsync_model
9191
assert generic_diffsync.get(DiffSyncModel.get_type(), "") == generic_diffsync_model
92-
# DiffSync doesn't know how to construct a uid str for a "diffsyncmodel"
93-
assert generic_diffsync.get(DiffSyncModel.get_type(), {}) is None
92+
# DiffSync doesn't know how to construct a uid str for a "diffsyncmodel" (it needs the class or instance, not a str)
93+
with pytest.raises(ValueError):
94+
generic_diffsync.get(DiffSyncModel.get_type(), {})
9495
# Wrong object-type - no match
9596
with pytest.raises(ObjectNotFound):
9697
generic_diffsync.get("", "")
@@ -147,6 +148,7 @@ class BadElementName(DiffSync):
147148

148149

149150
def test_diffsync_subclass_validation_missing_top_level():
151+
# pylint: disable=unused-variable
150152
with pytest.raises(AttributeError) as excinfo:
151153

152154
class MissingTopLevel(DiffSync):
@@ -160,6 +162,7 @@ class MissingTopLevel(DiffSync):
160162

161163

162164
def test_diffsync_subclass_validation_top_level_not_diffsyncmodel():
165+
# pylint: disable=unused-variable
163166
with pytest.raises(AttributeError) as excinfo:
164167

165168
class TopLevelNotDiffSyncModel(DiffSync):

0 commit comments

Comments
 (0)