Skip to content

Commit 7fe42ad

Browse files
committed
fix: update exception handling in TypeID tests and improve get_prefix_and_suffix logic to properly handle the case where the input string is empty
1 parent c984444 commit 7fe42ad

2 files changed

Lines changed: 12 additions & 7 deletions

File tree

tests/test_spec.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
from uuid6 import UUID
33

44
from typeid import TypeID
5+
from typeid.errors import TypeIDException
56

67

78
def test_invalid_spec(invalid_spec: list) -> None:
89
for spec in invalid_spec:
9-
with pytest.raises(Exception):
10+
with pytest.raises(TypeIDException):
1011
TypeID.from_string(spec["typeid"])
1112

1213

typeid/typeid.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,18 @@ def from_uuid(suffix: uuid6.UUID, prefix: Optional[str] = None) -> TypeID:
8181

8282
def get_prefix_and_suffix(string: str) -> tuple:
8383
parts = string.rsplit("_", 1)
84+
85+
# When there's no underscore in the string.
8486
if len(parts) == 1:
85-
prefix = None
86-
suffix = parts[0]
87-
elif len(parts) == 2 and parts[0] == "":
87+
if parts[0].strip() == "":
88+
raise InvalidTypeIDStringException(f"Invalid TypeID: {string}")
89+
return None, parts[0]
90+
91+
# When there is an underscore, unpack prefix and suffix.
92+
prefix, suffix = parts
93+
if prefix.strip() == "" or suffix.strip() == "":
8894
raise InvalidTypeIDStringException(f"Invalid TypeID: {string}")
89-
else:
90-
prefix, suffix = parts
91-
95+
9296
return prefix, suffix
9397

9498

0 commit comments

Comments
 (0)