-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Upgrade Python version requirements and dependencies - Updated Python version from 3.7 to 3.8 in Makefile and README.md. - Upgraded Pydantic dependency from 1.9.0 to 2.10.3 in requirements.txt and setup.py. - Updated mypy version from 0.790 to 1.13.0 in requirements-test.txt. - Refactored CLABE validation logic in types.py, removing custom error classes and integrating Pydantic's validation features. - Removed unused error handling code and updated tests to reflect changes in validation logic. - Updated GitHub Actions workflow to support Python versions 3.8 through 3.13. - Bumped version to 2.0.0.dev0 in version.py. * Update GitHub Actions workflow to use string format for Python versions * Update GitHub Actions workflow to include Python 3.13 in the testing * Add python version actions (#172) * Add python version actions * Update test.yml * update test deps --------- Co-authored-by: Felipe López <[email protected]> * Upgrade Python version requirements and dependencies - Updated Python version from 3.7 to 3.8 in Makefile and README.md. - Upgraded Pydantic dependency from 1.9.0 to 2.10.3 in requirements.txt and setup.py. - Updated mypy version from 0.790 to 1.13.0 in requirements-test.txt. - Refactored CLABE validation logic in types.py, removing custom error classes and integrating Pydantic's validation features. - Removed unused error handling code and updated tests to reflect changes in validation logic. - Updated GitHub Actions workflow to support Python versions 3.8 through 3.13. - Bumped version to 2.0.0.dev0 in version.py. * Changed regex to pattern in BankConfigRequest model * Update README; bump version to 2.0.0 * Update GitHub Actions workflow to remove Python 3.7 from the testing matrix, supporting versions 3.8 through 3.13. * This change enhances compatibility with Pydantic V2 and streamlines error handling. * Update setup.py to require Python 3.8 and bump version to 2.0.0 * Enhance README with Pydantic v2 usage examples and update CLABE validation logic * Fix code block formatting in README.md for Pydantic example * Removing redundant test case --------- Co-authored-by: gabino <[email protected]> Co-authored-by: Pach <[email protected]> Co-authored-by: Felipe López <[email protected]>
- Loading branch information
1 parent
e8cbcaa
commit 8aea318
Showing
11 changed files
with
155 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,67 @@ | ||
from typing import TYPE_CHECKING, ClassVar | ||
from typing import Any, Dict, Type | ||
|
||
from pydantic.v1.errors import NotDigitError | ||
from pydantic.v1.validators import ( | ||
constr_length_validator, | ||
constr_strip_whitespace, | ||
str_validator, | ||
) | ||
from pydantic import GetCoreSchemaHandler, GetJsonSchemaHandler | ||
from pydantic_core import PydanticCustomError, core_schema | ||
|
||
from .errors import BankCodeValidationError, ClabeControlDigitValidationError | ||
from .validations import BANK_NAMES, BANKS, compute_control_digit | ||
|
||
if TYPE_CHECKING: | ||
from pydantic.v1.typing import CallableGenerator | ||
|
||
|
||
def validate_digits(v: str) -> str: | ||
if not v.isdigit(): | ||
raise NotDigitError | ||
return v | ||
CLABE_LENGTH = 18 | ||
|
||
|
||
class Clabe(str): | ||
""" | ||
Based on: https://es.wikipedia.org/wiki/CLABE | ||
""" | ||
|
||
strip_whitespace: ClassVar[bool] = True | ||
min_length: ClassVar[int] = 18 | ||
max_length: ClassVar[int] = 18 | ||
|
||
def __init__(self, clabe: str): | ||
def __init__(self, clabe: str) -> None: | ||
self.bank_code_abm = clabe[:3] | ||
self.bank_code_banxico = BANKS[clabe[:3]] | ||
self.bank_name = BANK_NAMES[self.bank_code_banxico] | ||
|
||
@property | ||
def bank_code(self) -> str: | ||
return self.bank_code_banxico | ||
|
||
@classmethod | ||
def __get_validators__(cls) -> 'CallableGenerator': | ||
yield str_validator | ||
yield constr_strip_whitespace | ||
yield constr_length_validator | ||
yield validate_digits | ||
yield cls.validate_bank_code_abm | ||
yield cls.validate_control_digit | ||
yield cls | ||
def __get_pydantic_json_schema__( | ||
cls, | ||
schema: core_schema.CoreSchema, | ||
handler: GetJsonSchemaHandler, | ||
) -> Dict[str, Any]: | ||
json_schema = handler(schema) | ||
json_schema.update( | ||
type="string", | ||
pattern="^[0-9]{18}$", | ||
description="CLABE (Clave Bancaria Estandarizada)", | ||
examples=["723010123456789019"], | ||
) | ||
return json_schema | ||
|
||
@classmethod | ||
def validate_bank_code_abm(cls, clabe: str) -> str: | ||
if clabe[:3] not in BANKS.keys(): | ||
raise BankCodeValidationError | ||
return clabe | ||
def __get_pydantic_core_schema__( | ||
cls, | ||
_: Type[Any], | ||
__: GetCoreSchemaHandler, | ||
) -> core_schema.CoreSchema: | ||
return core_schema.no_info_after_validator_function( | ||
cls._validate, | ||
core_schema.str_schema( | ||
min_length=CLABE_LENGTH, | ||
max_length=CLABE_LENGTH, | ||
strip_whitespace=True, | ||
), | ||
) | ||
|
||
@classmethod | ||
def validate_control_digit(cls, clabe: str) -> str: | ||
def _validate(cls, clabe: str) -> 'Clabe': | ||
if not clabe.isdigit(): | ||
raise PydanticCustomError('clabe', 'debe ser numérico') | ||
if clabe[:3] not in BANKS: | ||
raise PydanticCustomError( | ||
'clabe.bank_code', 'código de banco no es válido' | ||
) | ||
if clabe[-1] != compute_control_digit(clabe): | ||
raise ClabeControlDigitValidationError | ||
return clabe | ||
|
||
@property | ||
def bank_code(self): | ||
return self.bank_code_banxico | ||
raise PydanticCustomError( | ||
'clabe.control_digit', 'clabe dígito de control no es válido' | ||
) | ||
return cls(clabe) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
__version__ = '1.3.0' | ||
__version__ = '2.0.0' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,4 @@ pytest-cov==4.1.0 | |
black==22.8.0 | ||
isort==5.11.5 | ||
flake8==5.0.4 | ||
mypy==1.4.1 | ||
mypy==1.4.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
pydantic==1.10.19 | ||
pydantic==2.10.3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters