Skip to content

Commit e082f92

Browse files
Merge pull request #724 from cordada/deploy/v0.37.0
Deploy Release v0.37.0
2 parents 800e5c2 + 9fc64b3 commit e082f92

File tree

7 files changed

+66
-10
lines changed

7 files changed

+66
-10
lines changed

.bumpversion.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[bumpversion]
2-
current_version = 0.36.0
2+
current_version = 0.37.0
33
commit = True
44
tag = False
55
message = chore: Bump version from {current_version} to {new_version}

HISTORY.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# History
22

3+
## 0.37.0 (2024-10-25)
4+
5+
- (PR #721, 2024-10-11) rut: Improve type annotation; Add method to validate DV
6+
- (PR #720, 2024-10-11) chore(deps): Bump django from 4.2.15 to 4.2.16
7+
- (PR #722, 2024-10-25) extras: Pydantic `Rut` type regex is not compliant with JSON Schema
8+
39
## 0.36.0 (2024-10-03)
410

511
- (PR #715, 2024-10-01) chore: Bump actions/checkout from 4.1.7 to 4.2.0 in production-deps group

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ cryptography==43.0.1
2828
# signxml
2929
defusedxml==0.7.1
3030
# via -r requirements.in
31-
django==4.2.15
31+
django==4.2.16
3232
# via
3333
# -r requirements.in
3434
# django-filter

src/cl_sii/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
55
"""
66

7-
__version__ = '0.36.0'
7+
__version__ = '0.37.0'

src/cl_sii/extras/pydantic_types.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

55
from __future__ import annotations
66

7+
import re
78
import sys
8-
from typing import Any
9+
from typing import Any, ClassVar, Pattern
910

1011

1112
if sys.version_info[:2] >= (3, 9):
@@ -74,6 +75,21 @@ class _RutPydanticAnnotation:
7475
b'"78773510-K"'
7576
"""
7677

78+
RUT_CANONICAL_STRICT_REGEX: ClassVar[Pattern] = re.compile(
79+
re.sub(
80+
pattern=r'\?P<\w+>',
81+
repl='',
82+
string=cl_sii.rut.constants.RUT_CANONICAL_STRICT_REGEX.pattern,
83+
)
84+
)
85+
"""
86+
RUT (strict) regex for canonical format, without named groups.
87+
88+
.. warning::
89+
JSON Schema and OpenAPI use the regular expression syntax from
90+
JavaScript (ECMA 262), which does not support Python’s named groups.
91+
"""
92+
7793
@classmethod
7894
def __get_pydantic_core_schema__(
7995
cls, source_type: Any, handler: pydantic.GetCoreSchemaHandler
@@ -83,9 +99,7 @@ def validate_from_str(value: str) -> cl_sii.rut.Rut:
8399

84100
from_str_schema = pydantic_core.core_schema.chain_schema(
85101
[
86-
pydantic_core.core_schema.str_schema(
87-
pattern=cl_sii.rut.constants.RUT_CANONICAL_STRICT_REGEX.pattern
88-
),
102+
pydantic_core.core_schema.str_schema(pattern=cls.RUT_CANONICAL_STRICT_REGEX),
89103
pydantic_core.core_schema.no_info_plain_validator_function(validate_from_str),
90104
]
91105
)

src/cl_sii/rut/__init__.py

Lines changed: 20 additions & 3 deletions
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

Lines changed: 19 additions & 0 deletions
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)