Prevent KeyError when accessing content.value for next cursor
Possible fix for a bug where client.get_tweet_by_id() throws a KeyError when the tweet has replies.
Summary by Sourcery
Bug Fixes:
- Adjust reply_next_cursor lookup to use content.value instead of content.itemContent.value to avoid KeyError for tweets with replies
Summary by CodeRabbit
-
Bug Fixes
- Fixed an issue where additional replies in long tweet threads sometimes failed to load.
- Corrected pagination handling to ensure subsequent pages of replies load consistently.
- Improves reliability when expanding and navigating through reply chains.
- No changes to the public API or user-facing settings.
Reviewer's guide (collapsed on small PRs)
Reviewer's Guide
Refactor cursor extraction in get_tweet_by_id to use content.value instead of content.itemContent.value, preventing KeyError when handling tweets with replies.
Class diagram for updated get_tweet_by_id method in Client
classDiagram
class Client {
+async get_tweet_by_id(tweet_id)
-_get_more_replies(tweet_id, reply_next_cursor)
}
Client --> "calls" _get_more_replies
%% Highlight change in reply_next_cursor extraction
class get_tweet_by_id {
-reply_next_cursor = entries[-1]['content']['itemContent']['value']
+reply_next_cursor = entries[-1]['content']['value']
}
File-Level Changes
| Change | Details | Files |
|---|---|---|
| Simplified reply cursor parsing to avoid missing key errors |
|
twikit/client/client.py |
Tips and commands
Interacting with Sourcery
-
Trigger a new review: Comment
@sourcery-ai reviewon the pull request. - Continue discussions: Reply directly to Sourcery's review comments.
-
Generate a GitHub issue from a review comment: Ask Sourcery to create an
issue from a review comment by replying to it. You can also reply to a
review comment with
@sourcery-ai issueto create an issue from it. -
Generate a pull request title: Write
@sourcery-aianywhere in the pull request title to generate a title at any time. You can also comment@sourcery-ai titleon the pull request to (re-)generate the title at any time. -
Generate a pull request summary: Write
@sourcery-ai summaryanywhere in the pull request body to generate a PR summary at any time exactly where you want it. You can also comment@sourcery-ai summaryon the pull request to (re-)generate the summary at any time. -
Generate reviewer's guide: Comment
@sourcery-ai guideon the pull request to (re-)generate the reviewer's guide at any time. -
Resolve all Sourcery comments: Comment
@sourcery-ai resolveon the pull request to resolve all Sourcery comments. Useful if you've already addressed all the comments and don't want to see them anymore. -
Dismiss all Sourcery reviews: Comment
@sourcery-ai dismisson the pull request to dismiss all existing Sourcery reviews. Especially useful if you want to start fresh with a new review - don't forget to comment@sourcery-ai reviewto trigger a new review!
Customizing Your Experience
Access your dashboard to:
- Enable or disable review features such as the Sourcery-generated pull request summary, the reviewer's guide, and others.
- Change the review language.
- Add, remove or edit custom review instructions.
- Adjust other review settings.
Getting Help
- Contact our support team for questions or feedback.
- Visit our documentation for detailed guides and information.
- Keep in touch with the Sourcery team by following us on X/Twitter, LinkedIn or GitHub.
Walkthrough
Updated get_tweet_by_id to extract the next-replies cursor from entries[-1]['content']['value'] instead of entries[-1]['content']['itemContent']['value']. The pagination flow and call to _get_more_replies remain unchanged. No public interfaces were modified.
Changes
| Cohort / File(s) | Summary of Changes |
|---|---|
Replies pagination cursor fixtwikit/client/client.py |
Adjusted cursor extraction in get_tweet_by_id to read from content.value rather than content.itemContent.value; pagination and control flow unchanged; no signature changes. |
Sequence Diagram(s)
sequenceDiagram
participant Client
participant TwitterAPI as Twitter API
Client->>TwitterAPI: fetch tweet by ID
TwitterAPI-->>Client: response with entries
Client->>Client: parse entries[-1].content.value as cursor
alt more replies available
Client->>TwitterAPI: _get_more_replies(cursor)
TwitterAPI-->>Client: additional replies
end
Estimated code review effort
🎯 2 (Simple) | ⏱️ ~7 minutes
Poem
I twitch my nose at threads that sprawl,
Hop to the cursor, heed the call.
From content.value, paths align,
Replies now queue in tidy line.
Thump-thump—pagination’s right,
A rabbit’s fix by moonlit night. 🐇✨
✨ Finishing Touches
- [ ] 📝 Generate Docstrings
🧪 Generate unit tests
- [ ] Create PR with unit tests
- [ ] Post copyable unit tests in a comment
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.
🪧 Tips
Chat
There are 3 ways to chat with CodeRabbit:
- Review comments: Directly reply to a review comment made by CodeRabbit. Example:
-
I pushed a fix in commit <commit_id>, please review it. -
Open a follow-up GitHub issue for this discussion.
-
- Files and specific lines of code (under the "Files changed" tab): Tag
@coderabbitaiin a new review comment at the desired location with your query. - PR comments: Tag
@coderabbitaiin a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:-
@coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase. -
@coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
-
Support
Need help? Create a ticket on our support page for assistance with any issues or questions.
CodeRabbit Commands (Invoked using PR/Issue comments)
Type @coderabbitai help to get the list of available commands.
Other keywords and placeholders
- Add
@coderabbitai ignoreanywhere in the PR description to prevent this PR from being reviewed. - Add
@coderabbitai summaryto generate the high-level summary at a specific location in the PR description. - Add
@coderabbitaianywhere in the PR title to generate the title automatically.
CodeRabbit Configuration File (.coderabbit.yaml)
- You can programmatically configure CodeRabbit by adding a
.coderabbit.yamlfile to the root of your repository. - Please see the configuration documentation for more information.
- If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation:
# yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json
Status, Documentation and Community
- Visit our Status Page to check the current availability of CodeRabbit.
- Visit our Documentation for detailed information on how to use CodeRabbit.
- Join our Discord Community to get help, request features, and share feedback.
- Follow us on X/Twitter for updates and announcements.
@T3los Thank you for your work!
There's a same pattern at https://github.com/d60/twikit/blob/3b18105ba0adb25ab78ba81d5df78825674f000d/twikit/client/client.py#L1526
May you also fix this? I have tested the change locally and it works well.