From 46daef9ddc6d564fcc260a76d1e7284378f3b865 Mon Sep 17 00:00:00 2001 From: Jose Tomas Robles Hahn Date: Thu, 10 Oct 2024 19:33:29 -0300 Subject: [PATCH 1/7] chore(Rut): Improve type annotation of `Rut(value)` `Rut.__init__()` is able to accept values of type `Rut` by converting them to `str`, so a more appropriate type annotation for `value` is `str | Rut` instead of just `str`. --- src/cl_sii/rut/__init__.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/cl_sii/rut/__init__.py b/src/cl_sii/rut/__init__.py index 4b12b74e..ec787a29 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. From 7fc3e8465d02011dde2881c640be943f19a669c5 Mon Sep 17 00:00:00 2001 From: Jose Tomas Robles Hahn Date: Thu, 10 Oct 2024 19:37:43 -0300 Subject: [PATCH 2/7] feat(rut): Add method `validate_dv()` to `Rut` This method is useful when we want to validate the DV of a `Rut` that was instantiated with `validate_dv=False`. --- src/cl_sii/rut/__init__.py | 16 ++++++++++++++++ src/tests/test_rut.py | 19 +++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/cl_sii/rut/__init__.py b/src/cl_sii/rut/__init__.py index ec787a29..a63fdc0a 100644 --- a/src/cl_sii/rut/__init__.py +++ b/src/cl_sii/rut/__init__.py @@ -139,6 +139,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 ############################################################################ From 6321d3a54a420c11bb1a70328423a83c9655edaf Mon Sep 17 00:00:00 2001 From: Jose Tomas Robles Hahn Date: Thu, 10 Oct 2024 19:38:26 -0300 Subject: [PATCH 3/7] chore(rut): Refactor validation of DV in `Rut.__init__()` --- src/cl_sii/rut/__init__.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/cl_sii/rut/__init__.py b/src/cl_sii/rut/__init__.py index a63fdc0a..40345573 100644 --- a/src/cl_sii/rut/__init__.py +++ b/src/cl_sii/rut/__init__.py @@ -81,8 +81,7 @@ def __init__(self, value: str | Rut, 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 From a1676ee79e4deb86bbd129d29db1c6a0dbd090a4 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Fri, 11 Oct 2024 21:17:12 +0000 Subject: [PATCH 4/7] chore(deps): Bump django from 4.2.15 to 4.2.16 Bumps [django](https://github.com/django/django) from 4.2.15 to 4.2.16. - [Commits](https://github.com/django/django/compare/4.2.15...4.2.16) --- updated-dependencies: - dependency-name: django dependency-type: direct:production ... Signed-off-by: dependabot[bot] --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From d885a3cd8ef9d68d82774522eafff812f45b08a2 Mon Sep 17 00:00:00 2001 From: Jose Tomas Robles Hahn Date: Thu, 24 Oct 2024 23:08:58 -0300 Subject: [PATCH 5/7] fix(extras): Pydantic `Rut` type regex is not compliant with JSON Schema The regular expression used to validate RUTs in `_RutPydanticAnnotation` results in Pydantic generating an invalid JSON Schema because the regex uses named groups, which are not supported by the JavaScript regular expression syntax used by JSON Schema (and OpenAPI). --- src/cl_sii/extras/pydantic_types.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) 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), ] ) From d479608360e34fa9970b6d638f5ec676b830cc44 Mon Sep 17 00:00:00 2001 From: svillegas-cdd Date: Fri, 25 Oct 2024 12:02:36 -0300 Subject: [PATCH 6/7] chore: Update history for new version --- HISTORY.md | 6 ++++++ 1 file changed, 6 insertions(+) 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 From 4058c6b6c8cbe332cb1b47710d91fd9dc9547d88 Mon Sep 17 00:00:00 2001 From: svillegas-cdd Date: Fri, 25 Oct 2024 12:02:43 -0300 Subject: [PATCH 7/7] chore: Bump version from 0.36.0 to 0.37.0 --- .bumpversion.cfg | 2 +- src/cl_sii/__init__.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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/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'