Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .bumpversion.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 0.36.0
current_version = 0.37.0
commit = True
tag = False
message = chore: Bump version from {current_version} to {new_version}
Expand Down
6 changes: 6 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# History

## 0.37.0 (2024-10-25)

- (PR #721, 2024-10-11) rut: Improve type annotation; Add method to validate DV
- (PR #720, 2024-10-11) chore(deps): Bump django from 4.2.15 to 4.2.16
- (PR #722, 2024-10-25) extras: Pydantic `Rut` type regex is not compliant with JSON Schema

## 0.36.0 (2024-10-03)

- (PR #715, 2024-10-01) chore: Bump actions/checkout from 4.1.7 to 4.2.0 in production-deps group
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ cryptography==43.0.1
# signxml
defusedxml==0.7.1
# via -r requirements.in
django==4.2.15
django==4.2.16
# via
# -r requirements.in
# django-filter
Expand Down
2 changes: 1 addition & 1 deletion src/cl_sii/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

"""

__version__ = '0.36.0'
__version__ = '0.37.0'
22 changes: 18 additions & 4 deletions src/cl_sii/extras/pydantic_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

from __future__ import annotations

import re
import sys
from typing import Any
from typing import Any, ClassVar, Pattern


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

RUT_CANONICAL_STRICT_REGEX: ClassVar[Pattern] = re.compile(
re.sub(
pattern=r'\?P<\w+>',
repl='',
string=cl_sii.rut.constants.RUT_CANONICAL_STRICT_REGEX.pattern,
)
)
"""
RUT (strict) regex for canonical format, without named groups.

.. warning::
JSON Schema and OpenAPI use the regular expression syntax from
JavaScript (ECMA 262), which does not support Python’s named groups.
"""

@classmethod
def __get_pydantic_core_schema__(
cls, source_type: Any, handler: pydantic.GetCoreSchemaHandler
Expand All @@ -83,9 +99,7 @@ def validate_from_str(value: str) -> cl_sii.rut.Rut:

from_str_schema = pydantic_core.core_schema.chain_schema(
[
pydantic_core.core_schema.str_schema(
pattern=cl_sii.rut.constants.RUT_CANONICAL_STRICT_REGEX.pattern
),
pydantic_core.core_schema.str_schema(pattern=cls.RUT_CANONICAL_STRICT_REGEX),
pydantic_core.core_schema.no_info_plain_validator_function(validate_from_str),
]
)
Expand Down
23 changes: 20 additions & 3 deletions src/cl_sii/rut/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

"""

from __future__ import annotations

import itertools
import random
import re
Expand Down Expand Up @@ -50,7 +52,7 @@ class Rut:

"""

def __init__(self, value: str, validate_dv: bool = False) -> None:
def __init__(self, value: str | Rut, validate_dv: bool = False) -> None:
"""
Constructor.

Expand Down Expand Up @@ -79,8 +81,7 @@ def __init__(self, value: str, validate_dv: bool = False) -> None:
self._dv = match_groups['dv']

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

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

############################################################################
# custom methods
############################################################################

def validate_dv(self, raise_exception: bool = False) -> bool:
"""
Whether the "digito verificador" of the RUT is correct.

:param raise_exception: Whether to raise an exception if validation fails.
:raises ValueError:
"""
is_valid = self.calc_dv(self._digits) == self._dv
if not is_valid and raise_exception:
raise ValueError("RUT's \"digito verificador\" is incorrect.", self.canonical)
return is_valid

############################################################################
# class methods
############################################################################
Expand Down
19 changes: 19 additions & 0 deletions src/tests/test_rut.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,25 @@ def test__hash__(self) -> None:
rut_hash = hash(self.valid_rut_instance.canonical)
self.assertEqual(self.valid_rut_instance.__hash__(), rut_hash)

############################################################################
# custom methods
############################################################################

def test_validate_dv(self) -> None:
self.assertIs(self.valid_rut_instance.validate_dv(), True)
self.assertIs(self.invalid_rut_instance.validate_dv(), False)

def test_validate_dv_raises_exception(self) -> None:
try:
self.valid_rut_instance.validate_dv(raise_exception=True)
except ValueError as exc:
self.fail(f'{exc.__class__.__name__} raised')

with self.assertRaisesRegex(
ValueError, r'''('RUT\\'s "digito verificador" is incorrect.', '6824160-0')'''
):
self.invalid_rut_instance.validate_dv(raise_exception=True)

############################################################################
# class methods
############################################################################
Expand Down