diff --git a/CHANGELOG.md b/CHANGELOG.md index 74c1db3..84e8deb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- `is_valid_cpf` aceitava dígitos de outros alfabetos, como `٩`, e levantava `ValueError` com algarismos sobrescritos, como `²` [#809](https://github.com/brazilian-utils/python/issues/809) - Cálculo do dígito verificador em `is_valid_legal_process` e `generate_legal_process`, que rejeitava números de processo reais e gerava números inválidos [#785](https://github.com/brazilian-utils/python/pull/785) ## [2.5.0] - 2026-06-30 diff --git a/README.md b/README.md index 5ba23fd..f2b7458 100644 --- a/README.md +++ b/README.md @@ -130,7 +130,7 @@ ela apenas valida o formato da string. Argumentos: -- cpf (str): O CPF a ser validado, uma string de 11 dígitos +- cpf (str): O CPF a ser validado, uma string de 11 dígitos de 0 a 9 Retorna: diff --git a/README_EN.md b/README_EN.md index 0b9167a..e5934a8 100644 --- a/README_EN.md +++ b/README_EN.md @@ -130,7 +130,7 @@ validates the format of the string. Args: -- cpf (str): The CPF to be validated, a 11-digit string +- cpf (str): The CPF to be validated, an 11-digit string of ASCII digits (0-9) Returns: diff --git a/brutils/cpf.py b/brutils/cpf.py index c2e418a..2385b2e 100644 --- a/brutils/cpf.py +++ b/brutils/cpf.py @@ -71,7 +71,12 @@ def display(cpf: str) -> str: backward compatibility. """ - if not cpf.isdigit() or len(cpf) != 11 or len(set(cpf)) == 1: + if ( + not cpf.isascii() + or not cpf.isdigit() + or len(cpf) != 11 + or len(set(cpf)) == 1 + ): return None return "{}.{}.{}-{}".format(cpf[:3], cpf[3:6], cpf[6:9], cpf[9:]) @@ -133,7 +138,12 @@ def validate(cpf: str) -> bool: backward compatibility. """ - if not cpf.isdigit() or len(cpf) != 11 or len(set(cpf)) == 1: + if ( + not cpf.isascii() + or not cpf.isdigit() + or len(cpf) != 11 + or len(set(cpf)) == 1 + ): return False return all(_hashdigit(cpf, i + 10) == int(v) for i, v in enumerate(cpf[9:])) @@ -148,7 +158,8 @@ def is_valid(cpf: str) -> bool: validates the format of the string. Args: - cpf (str): The CPF to be validated, a 11-digit string + cpf (str): The CPF to be validated, an 11-digit string of + ASCII digits (0-9) Returns: bool: True if the checksum digits match the base number, diff --git a/tests/test_cpf.py b/tests/test_cpf.py index 84f49e7..ef24d75 100644 --- a/tests/test_cpf.py +++ b/tests/test_cpf.py @@ -31,11 +31,19 @@ def test_display(self): self.assertIsNone(display("0000000000a")) self.assertIsNone(display("000000000000")) + # When CPF contains non-ASCII digits, returns None + self.assertIsNone(display("52٩٩8224725")) + self.assertIsNone(display("0000000001¹")) + def test_validate(self): self.assertIs(validate("52513127765"), True) self.assertIs(validate("52599927765"), True) self.assertIs(validate("00000000000"), False) + # When CPF contains non-ASCII digits, returns False + self.assertIs(validate("525٩٩٩27765"), False) + self.assertIs(validate("5251312776" + "⁵"), False) + def test_is_valid(self): # When cpf is not string, returns False self.assertIs(is_valid(1), False) @@ -63,6 +71,15 @@ def test_is_valid(self): # False self.assertIs(is_valid("11144477732"), False) + # When CPF contains digits from other alphabets, returns False + self.assertIs(is_valid("52٩٩8224725"), False) + self.assertIs(is_valid("11144477735"), False) + + # When CPF contains superscript digits, returns False instead of + # raising ValueError + self.assertIs(is_valid("1114447773" + "⁵"), False) + self.assertIs(is_valid("²" * 11), False) + # When cpf is valid self.assertIs(is_valid("11144477735"), True) self.assertIs(is_valid("11111111200"), True)