joke2k / joke2k/faker

pt_BR numeric CNPJ roots never contain repeated digits

Open Beginner friendly
#2,445 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
19.4k
Forks
2.1k
PR merge metrics
No merged PRs in 30d

Description

* Faker version: 40.36.0 and current `master` (`f33e820`)
* OS: Windows 11, Python 3.14.6

The `pt_BR` company provider builds the eight-digit numeric CNPJ root with `random_sample(range(10), 8)`. Because sampling is without replacement, every generated root has eight distinct digits. Valid numeric CNPJ roots can contain repeated digits; for example, Receita Federal's documented numeric example uses root `18781203`, which repeats both `1` and `8`.

This does not make the generated checksum invalid, but it unnecessarily restricts Faker to `10P8 = 1,814,400` roots instead of `10^8 = 100,000,000` possible eight-digit numeric roots—about 1.8% of the expected space.

Official reference: https://www.gov.br/receitafederal/pt-br/centrais-de-conteudo/publicacoes/documentos-tecnicos/sped/documentos-tecnicos-e-financeira/leiautes/anexo-ii-leiautes-gerais.pdf

### Steps to reproduce

```python
from faker import Faker

fake = Faker("pt_BR")
roots = [fake.company_id()[:8] for _ in range(10_000)]

print(sum(len(set(root)) < 8 for root in roots))
print(all(len(set(root)) == 8 for root in roots))
```

Current output:

```text
0
True
```

### Expected behavior

Each position in a numeric CNPJ root should be sampled independently, allowing repeated digits. The provider can use `random_choices(range(10), length=8)`, matching the with-replacement behavior already used by the alphanumeric branch.

### Actual behavior

`random_sample(range(10), 8)` guarantees that all eight root digits are unique, excluding the overwhelming majority of otherwise valid numeric roots.

### AI assistance disclosure

This report was prepared with assistance from **OpenAI Codex (GPT-5)**. It was used to inspect the current implementation and its git history, search for duplicate issues and pull requests, reproduce the behavior on current `master`, calculate the reachable output space, and draft this report. The reproduction and source references were independently verified against the checked-out repository and the linked Receita Federal documentation.

Contributor guide

Open the contributing guide

Research direction

Start at the pt_BR company provider entry point used by company_id(), and reproduce the behavior with the 10,000-root example in the issue. Replace the numeric root's without-replacement sampling with independent digit sampling, then verify that generated roots can contain repeated digits while company IDs remain valid.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
78/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.