Skip to content

Commit 6e660a6

Browse files
authored
Merge pull request #871 from cordada/refactor-rut-error-messages
rut: Use class variables for exception messages raised by `Rut`
2 parents c4efb69 + c76b044 commit 6e660a6

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

src/cl_sii/rut/__init__.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import itertools
1616
import random
1717
import re
18+
from typing import ClassVar
1819

1920
from . import constants
2021

@@ -52,6 +53,21 @@ class Rut:
5253
5354
"""
5455

56+
INVALID_TYPE_ERROR_MESSAGE: ClassVar[str] = "Invalid type."
57+
"""
58+
Error message used when the input type is not one of the accepted ones.
59+
"""
60+
61+
INVALID_RUT_ERROR_MESSAGE: ClassVar[str] = "Syntactically invalid RUT."
62+
"""
63+
Error message used when the input is not a syntactically valid RUT.
64+
"""
65+
66+
INVALID_DV_ERROR_MESSAGE: ClassVar[str] = "RUT's \"digito verificador\" is incorrect."
67+
"""
68+
Error message used when the RUT's "digito verificador" is incorrect.
69+
"""
70+
5571
def __init__(self, value: str | Rut, validate_dv: bool = False) -> None:
5672
"""
5773
Constructor.
@@ -64,17 +80,15 @@ def __init__(self, value: str | Rut, validate_dv: bool = False) -> None:
6480
:raises TypeError:
6581
6682
"""
67-
invalid_rut_msg = "Syntactically invalid RUT."
68-
6983
if isinstance(value, Rut):
7084
value = value.canonical
7185
if not isinstance(value, str):
72-
raise TypeError("Invalid type.")
86+
raise TypeError(self.INVALID_TYPE_ERROR_MESSAGE)
7387

7488
clean_value = Rut.clean_str(value)
7589
match_obj = constants.RUT_CANONICAL_STRICT_REGEX.match(clean_value)
7690
if match_obj is None:
77-
raise ValueError(invalid_rut_msg, value)
91+
raise ValueError(self.INVALID_RUT_ERROR_MESSAGE, value)
7892

7993
match_groups = match_obj.groupdict()
8094
self._digits = match_groups['digits']
@@ -151,7 +165,7 @@ def validate_dv(self, raise_exception: bool = False) -> bool:
151165
"""
152166
is_valid = self.calc_dv(self._digits) == self._dv
153167
if not is_valid and raise_exception:
154-
raise ValueError("RUT's \"digito verificador\" is incorrect.", self.canonical)
168+
raise ValueError(self.INVALID_DV_ERROR_MESSAGE, self.canonical)
155169
return is_valid
156170

157171
############################################################################

0 commit comments

Comments
 (0)