diff --git a/.bumpversion.cfg b/.bumpversion.cfg index 52fd7b4f..8ed6bf66 100644 --- a/.bumpversion.cfg +++ b/.bumpversion.cfg @@ -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} diff --git a/HISTORY.md b/HISTORY.md index fd49e5b8..8e554a74 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -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 diff --git a/requirements.txt b/requirements.txt index 138d8f33..18815cbe 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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 diff --git a/src/cl_sii/__init__.py b/src/cl_sii/__init__.py index d3e74ea3..a4e3c5d7 100644 --- a/src/cl_sii/__init__.py +++ b/src/cl_sii/__init__.py @@ -4,4 +4,4 @@ """ -__version__ = '0.36.0' +__version__ = '0.37.0' diff --git a/src/cl_sii/extras/pydantic_types.py b/src/cl_sii/extras/pydantic_types.py index a55a2b03..cd0f3a89 100644 --- a/src/cl_sii/extras/pydantic_types.py +++ b/src/cl_sii/extras/pydantic_types.py @@ -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): @@ -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 @@ -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), ] ) diff --git a/src/cl_sii/rut/__init__.py b/src/cl_sii/rut/__init__.py index 4b12b74e..40345573 100644 --- a/src/cl_sii/rut/__init__.py +++ b/src/cl_sii/rut/__init__.py @@ -10,6 +10,8 @@ """ +from __future__ import annotations + import itertools import random import re @@ -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. @@ -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 @@ -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 ############################################################################ diff --git a/src/tests/test_rut.py b/src/tests/test_rut.py index e7dd2e47..505264d3 100644 --- a/src/tests/test_rut.py +++ b/src/tests/test_rut.py @@ -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 ############################################################################