Skip to content

feat(cnpj): add alphanumeric CNPJ support (RFB Nota Técnica 49/2024)#730

Closed
gugacyber wants to merge 1 commit into
brazilian-utils:mainfrom
gugacyber:feat/cnpj-alfanumerico
Closed

feat(cnpj): add alphanumeric CNPJ support (RFB Nota Técnica 49/2024)#730
gugacyber wants to merge 1 commit into
brazilian-utils:mainfrom
gugacyber:feat/cnpj-alfanumerico

Conversation

@gugacyber
Copy link
Copy Markdown

Summary

Closes #685

Implements support for the new alphanumeric CNPJ format introduced by the Receita Federal do Brasil, effective July 2026 (Nota Técnica Conjunta COCAD/SUARA/RFB nº 49/2024).

Background

Starting July 2026, the RFB will issue CNPJs where the first 12 characters (base + branch) can contain uppercase letters A–Z in addition to digits 0–9. The two check digits (DVs) remain numeric. The validation algorithm is unchanged but character values are computed as ord(char) - 48 instead of int(char).

Changes

File Change
brutils/cnpj.py Full alphanumeric support
tests/test_cnpj.py 10 new tests (20 total, all passing)

New helper: _char_value(char)

Converts a character to its RFB numeric value (ord(char) - 48):

  • '0''9' → 0–9
  • 'A''Z' → 17–42

Updated: _hashdigit()

Uses _char_value() instead of int() — fully backward compatible.

Updated: validate() / is_valid()

Accept 14-character strings containing [0-9A-Z] with numeric DVs.

Updated: generate()

New alphanumeric=False parameter. Defaults to False for backward compatibility.

from brutils.cnpj import is_valid, format_cnpj, generate

# Canonical RFB example (Nota Técnica 49/2024, Q14)
is_valid('12ABC34501DE35')        # True
format_cnpj('12ABC34501DE35')     # '12.ABC.345/01DE-35'

# Backward compatible
is_valid('03560714000142')        # True
format_cnpj('03560714000142')     # '03.560.714/0001-42'

# Generation
generate()                        # numeric (existing behavior)
generate(alphanumeric=True)       # alphanumeric

Tests

20 passed in 0.72s (10 existing + 10 new)

New test cases:

  • Canonical RFB example validates correctly
  • Numeric CNPJs remain valid (backward compat)
  • Lowercase letters rejected
  • Wrong checksum rejected
  • All-identical chars rejected
  • Alphanumeric formatting
  • 50 generated alphanumeric CNPJs all pass validation

The Receita Federal will start issuing alphanumeric CNPJs in July 2026,
per Nota Técnica Conjunta COCAD/SUARA/RFB nº 49/2024.

Changes:
- _char_value(): new helper converting chars via ord(c)-48 (RFB rule)
- _is_valid_format(): centralised format check supporting [0-9A-Z]
- _hashdigit(): uses _char_value() instead of int() — works for both
- validate() / is_valid(): accept 14-char alphanumeric CNPJs
- display() / format_cnpj(): format alphanumeric CNPJs correctly
- generate(): new alphanumeric=False parameter for backward compat
- All existing tests pass (10/10); 10 new tests added (20/20 total)

Closes brazilian-utils#685
@gugacyber gugacyber requested review from a team as code owners May 20, 2026 02:49
@codecov
Copy link
Copy Markdown

codecov Bot commented May 20, 2026

Codecov Report

❌ Patch coverage is 92.00000% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 98.84%. Comparing base (5dc9585) to head (b31c4d3).

Files with missing lines Patch % Lines
brutils/cnpj.py 92.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #730      +/-   ##
==========================================
- Coverage   99.08%   98.84%   -0.24%     
==========================================
  Files          26       26              
  Lines         765      782      +17     
==========================================
+ Hits          758      773      +15     
- Misses          7        9       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@niltonpimentel02
Copy link
Copy Markdown
Member

olá! agradecemos pela sua contribuição, porém, já tem uma pessoa responsável por essa issue e com um pr aberto pra revisão.. por esse motivo esse pr vai ser fechado sem revisão.

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.

Suporte ao novo CNPJ Alfanumérico

2 participants