pt_BR numeric CNPJ roots never contain repeated digits
- 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
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:
0
TrueExpected 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.
Source: joke2k/faker