Skip to content

Fix(#317): document Cursor.__len__ page-only semantics, add count_all() - #964

Open
xemishra wants to merge 1 commit into
facebook:mainfrom
xemishra:main
Open

Fix(#317): document Cursor.__len__ page-only semantics, add count_all()#964
xemishra wants to merge 1 commit into
facebook:mainfrom
xemishra:main

Conversation

@xemishra

@xemishra xemishra commented Aug 6, 2026

Copy link
Copy Markdown

Problem

Cursor.__len__() (used implicitly whenever you call len() on the
result of any list/edge call, e.g. account.get_campaigns(),
account.get_ads(), account.get_insights()) only returns the number
of objects currently buffered in self._queue, i.e. the size of the
most recently loaded page (default 25, max 100 per Graph API page),
not the total number of objects the query actually matches.

Because Cursor paginates lazily (only fetching more pages when you
iterate), calling len(cursor) right after a query silently returns a
truncated count with no exception or warning:

campaigns = account.get_campaigns()
print(len(campaigns))  # e.g. 25, even if there are 3,000 matches

This has been reported before (#317) and is still present in main.
It's a high-impact bug because it fails silently, any user code
that relies on len() for reporting, batching, or "did I get
everything" checks will quietly lose data in production.

Fix

  • Added a clarifying comment on __len__ documenting that it reflects
    only the currently loaded page(s), and left its behavior unchanged
    to avoid breaking existing code that may depend on current semantics.
  • Added a new Cursor.count_all() method that fully paginates through
    all remaining results via load_next_page() and returns an accurate
    total count, for callers who need a reliable count instead of
    len(cursor) or the summary-dependent total().

Testing

  • Verified count_all() returns the same count as len(list(cursor))
    for a multi-page edge (e.g. get_campaigns on an account with

    100 campaigns).

  • Verified existing behavior of len(cursor) and total() is
    unchanged (no breaking changes).

Related issues

Fixes #317

@meta-cla

meta-cla Bot commented Aug 6, 2026

Copy link
Copy Markdown

Hi @xemishra!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@meta-cla meta-cla Bot added the CLA Signed label Aug 6, 2026
@meta-cla

meta-cla Bot commented Aug 6, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Max 100 activities retrieved from account

1 participant