Skip to content
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
2 changes: 1 addition & 1 deletion README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
17 changes: 14 additions & 3 deletions brutils/cpf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:])
Expand Down Expand Up @@ -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:]))
Expand All @@ -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,
Expand Down
17 changes: 17 additions & 0 deletions tests/test_cpf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
Loading