Skip to content

Commit

Permalink
Simplified error format to "did you mean" format.
Browse files Browse the repository at this point in the history
  • Loading branch information
imaurer committed Mar 25, 2024
1 parent 64ca172 commit 7d3f367
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 34 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ Annotated types as specified by [PEP 593](https://peps.python.org/pep-0593/).

#### Changed
- Renamed OnDisk to OnDiskValidator
- Renamed InMemory to MemoryValidator
- Renamed InMemory to InMemoryValidator
- Refactored InMemoryValidator and OnDiskValidator to use FuzzValidator
- Refactored Person to use FuzzValidator
- Renamed Regex to RegexValidator
- Changed error message to more common "did you mean" message format

#### Removed
- abstract.py module and AbstractType class, simplified by FuzzValidator
Expand Down
2 changes: 1 addition & 1 deletion src/fuzztypes/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "0.0.2"
__version__ = "0.1.0"

# logging
import logging
Expand Down
9 changes: 0 additions & 9 deletions src/fuzztypes/match.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@ def rank_value(self) -> Tuple[Tuple[float, int], Any]:
def __lt__(self, other: "Match"):
return self.rank_value < other.rank_value

def __str__(self):
if self.is_alias:
return f"{self.key} => {self.entity.value} [{self.score:.1f}]"
else:
return f"{self.entity.value} [{self.score:.1f}]"


class MatchResult(BaseModel):
matches: List[Match] = Field(default_factory=list)
Expand All @@ -43,9 +37,6 @@ def __len__(self):
def __getitem__(self, item):
return self.matches[item]

def __str__(self):
return ", ".join(map(str, self.matches))

@property
def entity(self):
return self.choice is not None and self.choice.entity
Expand Down
8 changes: 5 additions & 3 deletions src/fuzztypes/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,13 @@ def __getitem__(self, key: str) -> Optional[NamedEntity]:
if self.notfound_mode == "none":
return None

msg = "key ({key}) could not be resolved"
msg = '"{key}" could not be resolved'
ctx: Dict[str, Any] = dict(key=key)
if match_list:
ctx["near"] = [str(m) for m in match_list]
msg += f", closest non-matches = {match_list}"
near = [f'"{match.entity.value}"' for match in match_list.matches]
if len(near) > 1:
near[-1] = "or " + near[-1]
msg += f", did you mean {', '.join(near)}?"
raise PydanticCustomError("key_not_found", msg, ctx)

def prepare(self):
Expand Down
8 changes: 4 additions & 4 deletions tests/in_memory/test_in_memory_alias.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ def test_duplicate_records():
assert A["b"].value == "a"
assert False, "Didn't raise exception!"
except KeyError as e:
assert str(e) == (
"'Key Error: b [key (b) could not be resolved, "
"closest non-matches = b => c [100.0], b => a ["
"100.0], b => d [100.0]]'"
msg = str(e.args[0])
assert (
msg == "Key Error: b "
'["b" could not be resolved, did you mean "c", "a", or "d"?]'
)

A = InMemoryValidator(source, tiebreaker_mode="lesser")
Expand Down
14 changes: 2 additions & 12 deletions tests/in_memory/test_in_memory_fuzz.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,20 +93,10 @@ def test_min_score():
except ValidationError as e:
assert e.errors(include_url=False) == [
{
"ctx": {
"key": "B K L",
"near": [
"('matches', [Match(key='a b c', entity=NamedEntity("
"value='A B C', label=None, meta=None, "
"priority=None, aliases=[]), is_alias=False, "
"score=40.0, term=None)])",
"('choice', None)",
],
},
"ctx": {"key": "B K L"},
"input": "B K L",
"loc": ("strict",),
"msg": "key (B K L) could not be resolved, closest "
"non-matches = A B C [40.0]",
"msg": '"B K L" could not be resolved, did you mean "A B C"?',
"type": "key_not_found",
}
]
Expand Down
8 changes: 4 additions & 4 deletions tests/on_disk/test_on_disk_alias.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ def test_duplicate_records():
assert A["b"].value == "a"
assert False, "Didn't raise exception!"
except KeyError as e:
assert str(e) == (
"'Key Error: b [key (b) could not be resolved, "
"closest non-matches = b => c [100.0], b => a ["
"100.0], b => d [100.0]]'"
assert (
str(e)
== '\'Key Error: b '
'["b" could not be resolved, did you mean "c", "a", or "d"?]\''
)

A = OnDiskValidator("DupeRec", source, tiebreaker_mode="lesser")
Expand Down

0 comments on commit 7d3f367

Please sign in to comment.