15
15
import itertools
16
16
import random
17
17
import re
18
+ from typing import ClassVar
18
19
19
20
from . import constants
20
21
@@ -52,6 +53,21 @@ class Rut:
52
53
53
54
"""
54
55
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
+
55
71
def __init__ (self , value : str | Rut , validate_dv : bool = False ) -> None :
56
72
"""
57
73
Constructor.
@@ -64,17 +80,15 @@ def __init__(self, value: str | Rut, validate_dv: bool = False) -> None:
64
80
:raises TypeError:
65
81
66
82
"""
67
- invalid_rut_msg = "Syntactically invalid RUT."
68
-
69
83
if isinstance (value , Rut ):
70
84
value = value .canonical
71
85
if not isinstance (value , str ):
72
- raise TypeError ("Invalid type." )
86
+ raise TypeError (self . INVALID_TYPE_ERROR_MESSAGE )
73
87
74
88
clean_value = Rut .clean_str (value )
75
89
match_obj = constants .RUT_CANONICAL_STRICT_REGEX .match (clean_value )
76
90
if match_obj is None :
77
- raise ValueError (invalid_rut_msg , value )
91
+ raise ValueError (self . INVALID_RUT_ERROR_MESSAGE , value )
78
92
79
93
match_groups = match_obj .groupdict ()
80
94
self ._digits = match_groups ['digits' ]
@@ -151,7 +165,7 @@ def validate_dv(self, raise_exception: bool = False) -> bool:
151
165
"""
152
166
is_valid = self .calc_dv (self ._digits ) == self ._dv
153
167
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 )
155
169
return is_valid
156
170
157
171
############################################################################
0 commit comments