Skip to content

fix(cvm): match holdings CNPJ with or without punctuation#23

Merged
robertoecf merged 1 commit into
mainfrom
fix/cvm-holdings-cnpj-normalize
Jun 16, 2026
Merged

fix(cvm): match holdings CNPJ with or without punctuation#23
robertoecf merged 1 commit into
mainfrom
fix/cvm-holdings-cnpj-normalize

Conversation

@robertoecf

Copy link
Copy Markdown
Owner

Bug

findata cvm holdings <cnpj> returns "No holdings" when the CNPJ is passed as bare digits (22187946000141), even though the --help text 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() vs row[...].strip()). CVM stores CNPJs punctuated, so a bare-digit argument never matched anything.

cnpj_norm = cnpj.strip()                 # "22187946000141"
...
row_cnpj = (...).strip()                  # "22.187.946/0001-41"  → never equal

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

  • New regression test test_holdings_bare_digit_cnpj_matches_punctuated: bare-digit query returns the same rows as the punctuated query (fails on main, passes here).
  • Live check against real 2025-12 CDA data (Verde FIC): 22187946000141 and 22.187.946/0001-41 now return identical results.
  • ruff format/check, mypy src/findata, full pytest suite 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.

`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>
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@coderabbitai

coderabbitai Bot commented Jun 16, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@robertoecf, we couldn't start this review because you've reached your PR review rate limit.

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 @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

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 configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 4bb84fd6-d81f-4516-bc1a-c2c19c56681d

📥 Commits

Reviewing files that changed from the base of the PR and between 2c541bf and 7fdde42.

📒 Files selected for processing (3)
  • CHANGELOG.md
  • src/findata/sources/cvm/holdings.py
  • tests/test_cvm_funds.py
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/cvm-holdings-cnpj-normalize

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines 148 to +150
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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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)

Comment on lines +163 to 165
row_cnpj = _digits(row.get("CNPJ_FUNDO_CLASSE") or row.get("CNPJ_FUNDO"))
if row_cnpj != cnpj_norm:
continue

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
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

@robertoecf robertoecf merged commit 47c4d71 into main Jun 16, 2026
7 checks passed
@robertoecf robertoecf deleted the fix/cvm-holdings-cnpj-normalize branch June 16, 2026 23:59
robertoecf added a commit that referenced this pull request Jun 26, 2026
`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>
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.

1 participant