Fix(#317): document Cursor.__len__ page-only semantics, add count_all() - #964
Fix(#317): document Cursor.__len__ page-only semantics, add count_all()#964xemishra wants to merge 1 commit into
Conversation
|
Hi @xemishra! Thank you for your pull request and welcome to our community. Action RequiredIn 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. ProcessIn 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 If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
Problem
Cursor.__len__()(used implicitly whenever you calllen()on theresult of any list/edge call, e.g.
account.get_campaigns(),account.get_ads(),account.get_insights()) only returns the numberof objects currently buffered in
self._queue, i.e. the size of themost recently loaded page (default 25, max 100 per Graph API page),
not the total number of objects the query actually matches.
Because
Cursorpaginates lazily (only fetching more pages when youiterate), calling
len(cursor)right after a query silently returns atruncated count with no exception or warning:
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 geteverything" checks will quietly lose data in production.
Fix
__len__documenting that it reflectsonly the currently loaded page(s), and left its behavior unchanged
to avoid breaking existing code that may depend on current semantics.
Cursor.count_all()method that fully paginates throughall remaining results via
load_next_page()and returns an accuratetotal count, for callers who need a reliable count instead of
len(cursor)or the summary-dependenttotal().Testing
count_all()returns the same count aslen(list(cursor))for a multi-page edge (e.g.
get_campaignson an account withlen(cursor)andtotal()isunchanged (no breaking changes).
Related issues
Fixes #317