From 25d2f381b5bf8c8cd77b9c7f56c0fe7070fb4f91 Mon Sep 17 00:00:00 2001 From: Matheus Lobo Camara <171098636+LoboProgrammingg@users.noreply.github.com> Date: Fri, 20 Nov 2026 13:06:09 -0400 Subject: [PATCH] =?UTF-8?q?fix:=20rejeita=20d=C3=ADgitos=20n=C3=A3o=20ASCI?= =?UTF-8?q?I=20na=20valida=C3=A7=C3=A3o=20de=20CEP=20(#803)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit str.isdigit() aceita dígitos Unicode como '²', '0' e '٠', então is_valid_cep('²' * 8) retornava True e format_cep formatava a entrada. --- CHANGELOG.md | 1 + README.md | 2 +- README_EN.md | 2 +- brutils/cep.py | 9 +++++++-- tests/test_cep.py | 5 +++++ 5 files changed, 15 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 74c1db3f..93ecff1b 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_cep` e `format_cep` aceitavam dígitos não ASCII, como `²` [#805](https://github.com/brazilian-utils/python/pull/805) - 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 5ba23fd5..2a1463d3 100644 --- a/README.md +++ b/README.md @@ -329,7 +329,7 @@ Exemplo: Verifica se um CEP (Código de Endereçamento Postal) brasileiro é válido. Para que um CEP seja considerado válido, a entrada deve ser uma string contendo -exatamente 8 dígitos. Esta função não verifica se o CEP é um CEP real, pois +exatamente 8 dígitos de 0 a 9. Esta função não verifica se o CEP é um CEP real, pois valida apenas o formato da string. Argumentos: diff --git a/README_EN.md b/README_EN.md index 0b9167a8..b45cbda1 100644 --- a/README_EN.md +++ b/README_EN.md @@ -330,7 +330,7 @@ Example: ### is_valid_cep Checks if a Brazilian CEP (Postal Code) is valid. -To be considered valid, the input must be a string containing exactly 8 digits. +To be considered valid, the input must be a string containing exactly 8 digits (0-9). This function does not verify if the CEP is a real postal code; it only validates the format of the string. diff --git a/brutils/cep.py b/brutils/cep.py index 46ef975f..6988389f 100644 --- a/brutils/cep.py +++ b/brutils/cep.py @@ -67,7 +67,7 @@ def is_valid(cep: str) -> bool: Checks if a CEP (Postal Code) is valid. To be considered valid, the input must be a string containing exactly 8 - digits. + ASCII digits (0-9). This function does not verify if the CEP is a real postal code; it only validates the format of the string. @@ -89,7 +89,12 @@ def is_valid(cep: str) -> bool: https://en.wikipedia.org/wiki/Código_de_Endereçamento_Postal """ - return isinstance(cep, str) and len(cep) == 8 and cep.isdigit() + return ( + isinstance(cep, str) + and len(cep) == 8 + and cep.isascii() + and cep.isdigit() + ) def generate() -> str: diff --git a/tests/test_cep.py b/tests/test_cep.py index e47b12d3..c8f1afe3 100644 --- a/tests/test_cep.py +++ b/tests/test_cep.py @@ -34,6 +34,11 @@ def test_is_valid(self): # When CEP does not contain only digits, returns False self.assertIs(is_valid("1234567-"), False) + # When CEP contains non-ASCII digits, returns False + self.assertIs(is_valid("²" * 8), False) + self.assertIs(is_valid("01310200"), False) + self.assertIs(is_valid("٠١٣١٠٢٠٠"), False) + # When CEP is valid self.assertIs(is_valid("99999999"), True) self.assertIs(is_valid("88390000"), True)