Skip to content

Commit 4b52749

Browse files
authored
Merge pull request #721 from cordada/task/improve-rut
rut: Improve type annotation; Add method to validate DV
2 parents 05e1f30 + 6321d3a commit 4b52749

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

src/cl_sii/rut/__init__.py

+20-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
1111
"""
1212

13+
from __future__ import annotations
14+
1315
import itertools
1416
import random
1517
import re
@@ -50,7 +52,7 @@ class Rut:
5052
5153
"""
5254

53-
def __init__(self, value: str, validate_dv: bool = False) -> None:
55+
def __init__(self, value: str | Rut, validate_dv: bool = False) -> None:
5456
"""
5557
Constructor.
5658
@@ -79,8 +81,7 @@ def __init__(self, value: str, validate_dv: bool = False) -> None:
7981
self._dv = match_groups['dv']
8082

8183
if validate_dv:
82-
if Rut.calc_dv(self._digits) != self._dv:
83-
raise ValueError("RUT's \"digito verificador\" is incorrect.", value)
84+
self.validate_dv(raise_exception=True)
8485

8586
############################################################################
8687
# properties
@@ -137,6 +138,22 @@ def __hash__(self) -> int:
137138
# Objects are hashable so they can be used in hashable collections.
138139
return hash(self.canonical)
139140

141+
############################################################################
142+
# custom methods
143+
############################################################################
144+
145+
def validate_dv(self, raise_exception: bool = False) -> bool:
146+
"""
147+
Whether the "digito verificador" of the RUT is correct.
148+
149+
:param raise_exception: Whether to raise an exception if validation fails.
150+
:raises ValueError:
151+
"""
152+
is_valid = self.calc_dv(self._digits) == self._dv
153+
if not is_valid and raise_exception:
154+
raise ValueError("RUT's \"digito verificador\" is incorrect.", self.canonical)
155+
return is_valid
156+
140157
############################################################################
141158
# class methods
142159
############################################################################

src/tests/test_rut.py

+19
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,25 @@ def test__hash__(self) -> None:
261261
rut_hash = hash(self.valid_rut_instance.canonical)
262262
self.assertEqual(self.valid_rut_instance.__hash__(), rut_hash)
263263

264+
############################################################################
265+
# custom methods
266+
############################################################################
267+
268+
def test_validate_dv(self) -> None:
269+
self.assertIs(self.valid_rut_instance.validate_dv(), True)
270+
self.assertIs(self.invalid_rut_instance.validate_dv(), False)
271+
272+
def test_validate_dv_raises_exception(self) -> None:
273+
try:
274+
self.valid_rut_instance.validate_dv(raise_exception=True)
275+
except ValueError as exc:
276+
self.fail(f'{exc.__class__.__name__} raised')
277+
278+
with self.assertRaisesRegex(
279+
ValueError, r'''('RUT\\'s "digito verificador" is incorrect.', '6824160-0')'''
280+
):
281+
self.invalid_rut_instance.validate_dv(raise_exception=True)
282+
264283
############################################################################
265284
# class methods
266285
############################################################################

0 commit comments

Comments
 (0)