Skip to content

fix(pt_BR): allow repeated digits in numeric CNPJ roots - #2446

Open
liuyaohui666 wants to merge 1 commit into
joke2k:masterfrom
liuyaohui666:fix/pt-br-cnpj-repeated-digits
Open

fix(pt_BR): allow repeated digits in numeric CNPJ roots#2446
liuyaohui666 wants to merge 1 commit into
joke2k:masterfrom
liuyaohui666:fix/pt-br-cnpj-repeated-digits

Conversation

@liuyaohui666

Copy link
Copy Markdown

What does this change

Numeric Brazilian CNPJ generation now samples the eight root digits with replacement. A regression test uses the Receita Federal example 18.781.203/0001-28 to prove that repeated root digits are supported while preserving checksum generation.

What was wrong

_company_id_base(False) used random_sample(range(10), 8), which samples without replacement. That made every generated eight-digit root contain eight distinct digits, although valid numeric CNPJs may repeat digits.

As a result, Faker could generate only 10P8 = 1,814,400 roots out of the 10^8 = 100,000,000 possible numeric roots (about 1.8%).

How this fixes it

Use random_choices(range(10), length=8) so each root position is sampled independently. The alphanumeric path, branch suffix, public API, and checksum calculation remain unchanged.

Fixes #2445

Verification

  • pytest tests/providers/test_company.py -q: 93 passed
  • tox -e py -- tests/providers/test_company.py: 93 passed, then 101 passed in the locale module run
  • Black (line length 120), isort, flake8, mypy, check-manifest, and test-class ordering checks passed
  • git diff --check passed

AI Assistance Disclosure (REQUIRED)

  • No AI tools were used in the creation of this PR.
  • If AI tools were used, disclose how they were used and how you reviewed their output.

OpenAI Codex (GPT-5) assisted with repository/source-history inspection, duplicate searching, the reproducer, the regression test, the one-line implementation, and drafting this PR. I reviewed the diff and ran the listed tests and static checks locally.

Checklist

  • I have read the CONTRIBUTING document.
  • I have read the Coding Style documentation.
  • I have run make lint.

make is unavailable in the Windows development environment. Its Black, isort, flake8, and mypy components were run individually and passed; the complete GitHub Actions matrix is left to verify the repository-native workflow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

pt_BR numeric CNPJ roots never contain repeated digits

1 participant