Repository navigation
refactor: Melhorias nas funções de validação de CNPJ #754
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
cb01cc9
8819c79
ba0a8ea
903a5a7
18e5d49
836f6b0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,10 @@ | ||
| import random | ||
| import re | ||
| from itertools import chain | ||
| from random import choices, randint | ||
| from string import ascii_uppercase, digits | ||
|
|
||
| CNPJ_RE = re.compile(r"^[A-Z0-9]{12}[0-9]{2}") | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🩺 Stability & Availability | 🟠 Major | ⚡ Quick win Require a full 14-character match.
Proposed fix-CNPJ_RE = re.compile(r"^[A-Z0-9]{12}[0-9]{2}")
+CNPJ_RE = re.compile(r"[A-Z0-9]{12}[0-9]{2}")Use 🤖 Prompt for AI Agents |
||
|
|
||
|
niltonpimentel02 marked this conversation as resolved.
|
||
| # FORMATTING | ||
| ############ | ||
|
|
||
|
|
@@ -31,7 +34,7 @@ def sieve(dirty: str) -> str: | |
| backward compatibility. | ||
| """ | ||
|
|
||
|
niltonpimentel02 marked this conversation as resolved.
|
||
| return "".join(filter(lambda char: char not in "./-", dirty)) | ||
| return re.sub(r"[\.\/\-]", "", dirty) | ||
|
|
||
|
|
||
| def remove_symbols(dirty: str) -> str: | ||
|
niltonpimentel02 marked this conversation as resolved.
|
||
|
|
@@ -84,13 +87,9 @@ def display(cnpj: str) -> str | None: | |
| backward compatibility. | ||
| """ | ||
|
|
||
| if ( | ||
| len(cnpj) != 14 | ||
| or not _is_alphanumeric(cnpj[:12]) | ||
| or not cnpj[12:].isdigit() | ||
|
niltonpimentel02 marked this conversation as resolved.
|
||
| or len(set(cnpj)) == 1 | ||
| ): | ||
| if not re.search(CNPJ_RE, cnpj): | ||
| return None | ||
|
|
||
| return "{}.{}.{}/{}-{}".format( | ||
| cnpj[:2], cnpj[2:5], cnpj[5:8], cnpj[8:12], cnpj[12:] | ||
| ) | ||
|
|
@@ -130,27 +129,6 @@ def format_cnpj(cnpj: str) -> str | None: | |
| ############ | ||
|
|
||
|
|
||
| def _is_alphanumeric(cnpj: str) -> bool: | ||
| """ | ||
| Checks whether all characters are digits or uppercase letters. | ||
|
|
||
| Args: | ||
| cnpj (str): The CNPJ string to be validated. | ||
|
|
||
| Returns: | ||
| bool: True if all characters are either digits or uppercase letters, | ||
| False otherwise. | ||
|
|
||
| Example: | ||
| >>> _is_alphanumeric("035ABC1400Z142") | ||
| True | ||
| >>> _is_alphanumeric("0011-22200013!") | ||
| False | ||
| """ | ||
|
|
||
| return all(char in (digits + ascii_uppercase) for char in cnpj) | ||
|
|
||
|
|
||
| def validate(cnpj: str) -> bool: | ||
| """ | ||
| Validates a CNPJ (Brazilian Company Registration Number) by comparing its | ||
|
|
@@ -177,14 +155,9 @@ def validate(cnpj: str) -> bool: | |
| This method should not be used in new code and is only provided for | ||
| backward compatibility. | ||
| """ | ||
|
|
||
| if ( | ||
| len(cnpj) != 14 | ||
| or not _is_alphanumeric(cnpj[:12]) | ||
| or not cnpj[12:].isdigit() | ||
| or len(set(cnpj)) == 1 | ||
| ): | ||
| if not re.search(CNPJ_RE, cnpj): | ||
| return False | ||
|
|
||
| return all( | ||
| _hashdigit(cnpj, i + 13) == int(v) for i, v in enumerate(cnpj[12:]) | ||
| ) | ||
|
|
@@ -239,26 +212,12 @@ def generate(branch: int | str = 1, alphanumeric: bool = False) -> str: | |
| >>> generate(branch="AB12", alphanumeric=True) | ||
| "NX9K79E2AB1200" | ||
| """ | ||
| final_branch = str(branch)[:4].zfill(4) | ||
|
niltonpimentel02 marked this conversation as resolved.
|
||
|
|
||
| if alphanumeric: | ||
| branch = str(branch) | ||
| branch = branch[:4] if len(branch) >= 4 else branch.zfill(4) | ||
| branch = ( | ||
| "0001" | ||
| if branch == "0000" or not _is_alphanumeric(branch) | ||
| else branch | ||
| ) | ||
| base = "".join(choices(digits * 3 + ascii_uppercase, k=8)) + branch | ||
|
|
||
| return base + _checksum(base) | ||
|
|
||
| branch = int(branch) | ||
| branch %= 10000 | ||
| branch += int(branch == 0) | ||
| branch = str(branch).zfill(4) | ||
| base = str(randint(0, 99999999)).zfill(8) + branch | ||
| character_choices = digits + ascii_uppercase if alphanumeric else digits | ||
| cnpj_base = "".join(random.sample(character_choices, 8)) + final_branch | ||
|
|
||
| return base + _checksum(base) | ||
| return cnpj_base + _checksum(cnpj_base) | ||
|
|
||
|
|
||
| def _hashdigit(cnpj: str, position: int) -> int: | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.