fix(cvm): match holdings CNPJ with or without punctuation#23
Conversation
`findata cvm holdings` compared the CNPJ argument to the value stored in the CDA files as raw strings. CVM stores CNPJs punctuated (`22.187.946/0001-41`), so a user passing bare digits (`22187946000141`) — the exact form the `--help` text advertises — got "No holdings". Normalize both sides to digits-only before comparing. Output still carries the canonical punctuated form from CVM. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
|
Warning Review limit reached
More reviews will be available in 52 minutes and 37 seconds. Learn how PR review limits work. Your organization has run out of usage credits. Purchase more credits in the billing tab to continue. ⌛ How to resolve this issue?After more reviews become available, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available. Please see our Fair Usage Limits Policy for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (3)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Code Review
This pull request resolves an issue where the cvm holdings command ignored bare-digit CNPJs by normalizing both the input and the CSV row CNPJs to digits before comparison. A regression test was also added. The review feedback highlights a performance bottleneck caused by running regex-based normalization on every CSV row. It suggests pre-computing both the bare and punctuated CNPJ forms beforehand to allow fast string comparisons and avoid unnecessary network requests for invalid inputs.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| ym = f"{year}{month:02d}" | ||
| raw = await get_bytes(CDA_URL.format(ym=ym), cache_ttl=86400) | ||
| cnpj_norm = cnpj.strip() | ||
| cnpj_norm = _digits(cnpj) |
There was a problem hiding this comment.
We can optimize this function by validating the CNPJ and pre-computing both its bare and punctuated forms before downloading the large ZIP file. This avoids unnecessary network requests for invalid inputs and allows us to perform fast string comparisons in the hot loop instead of running regex normalization on every single row.
cnpj_bare = _digits(cnpj)
if not cnpj_bare:
return []
cnpj_punctuated = (
f"{cnpj_bare[:2]}.{cnpj_bare[2:5]}.{cnpj_bare[5:8]}/{cnpj_bare[8:12]}-{cnpj_bare[12:]}"
if len(cnpj_bare) == 14
else cnpj_bare
)
ym = f"{year}{month:02d}"
raw = await get_bytes(CDA_URL.format(ym=ym), cache_ttl=86400)| row_cnpj = _digits(row.get("CNPJ_FUNDO_CLASSE") or row.get("CNPJ_FUNDO")) | ||
| if row_cnpj != cnpj_norm: | ||
| continue |
There was a problem hiding this comment.
Instead of running the regex-based _digits normalization on every single row of the CSV (which can contain hundreds of thousands of rows and becomes a major CPU bottleneck), we can perform a direct string comparison against the pre-computed bare and punctuated CNPJ forms.
| row_cnpj = _digits(row.get("CNPJ_FUNDO_CLASSE") or row.get("CNPJ_FUNDO")) | |
| if row_cnpj != cnpj_norm: | |
| continue | |
| row_cnpj = (row.get("CNPJ_FUNDO_CLASSE") or row.get("CNPJ_FUNDO") or "").strip() | |
| if row_cnpj != cnpj_punctuated and row_cnpj != cnpj_bare: | |
| continue |
`findata cvm holdings` compared the CNPJ argument to the value stored in the CDA files as raw strings. CVM stores CNPJs punctuated (`22.187.946/0001-41`), so a user passing bare digits (`22187946000141`) — the exact form the `--help` text advertises — got "No holdings". Normalize both sides to digits-only before comparing. Output still carries the canonical punctuated form from CVM. Co-authored-by: Roberto <robertoecf@users.noreply.github.com> Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
Bug
findata cvm holdings <cnpj>returns "No holdings" when the CNPJ is passed as bare digits (22187946000141), even though the--helptext says "Fund CNPJ (with or without punctuation)". Only the punctuated form (22.187.946/0001-41) worked.Root cause
The CDA reader compared the CNPJ argument to the value stored in the CSV as raw strings (
cnpj.strip()vsrow[...].strip()). CVM stores CNPJs punctuated, so a bare-digit argument never matched anything.Fix
Normalize both sides to digits-only before comparing, via a small
_digits()helper. Output still carries the canonical punctuated form exactly as CVM provides it.Verification
test_holdings_bare_digit_cnpj_matches_punctuated: bare-digit query returns the same rows as the punctuated query (fails onmain, passes here).22187946000141and22.187.946/0001-41now return identical results.ruff format/check,mypy src/findata, fullpytestsuite all green.Note
Companion PR fixes a second, independent defect in the same reader (BLC_2 rows silently dropped). The two touch the same
CHANGELOG.md/test anchor, so whichever merges second will hit a trivial both-added conflict — happy to rebase this one once the other lands.