Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion twikit/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import warnings
from functools import partial
from typing import Any, AsyncGenerator, Literal
from typing import Any, AsyncGenerator, Literal, List
from urllib.parse import urlparse

import filetype
Expand Down Expand Up @@ -785,6 +785,45 @@ async def search_tweet(
previous_cursor
)

async def get_user_mentions(
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

issue: The method returns a list of (tweet_id, tweet) tuples, not Tweet objects as the docstring and type hint suggest.

Please update the docstring and type hint to match the actual return type, or modify the method to return a List[Tweet] as documented.

self,
handle: str,
search_count: int = 20
) -> List[Tweet]:
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.

⚠️ Potential issue

Fix return type annotation to match actual implementation.

The method declares it returns List[Tweet] but actually returns List[Tuple[str, Tweet]] (tuples of tweet ID and tweet object).

-) -> List[Tweet]:
+) -> List[Tuple[str, Tweet]]:

You'll also need to import Tuple:

-from typing import Any, AsyncGenerator, List, Literal
+from typing import Any, AsyncGenerator, List, Literal, Tuple
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
) -> List[Tweet]:
# at the top of twikit/client/client.py
-from typing import Any, AsyncGenerator, List, Literal
+from typing import Any, AsyncGenerator, List, Literal, Tuple
# … later in the file, around line 792 …
- ) -> List[Tweet]:
+ ) -> List[Tuple[str, Tweet]]:
🤖 Prompt for AI Agents
In twikit/client/client.py at line 792, the return type annotation is
incorrectly specified as List[Tweet] while the method actually returns
List[Tuple[str, Tweet]]. Update the return type annotation to List[Tuple[str,
Tweet]] to accurately reflect the returned data structure. Additionally, add an
import statement for Tuple from the typing module at the top of the file if it
is not already imported.

"""
Fetches user mentions via search method.

Parameters
----------
handle : :class:`str`
The target user handle (eg: 5mknc5, elonmusk).
search_count : :class:`int`, default=20
The number of latest tweets to retrieve in each request.

Returns
-------
List[:class:`Tweet`]
A list of `Tweet` results.

Examples
--------
>>> mentions = await client.get_user_mentions('5mknc5', search_count=30)
>>> for mention in mentions:
... print(mention)
<Tweet id="...">
<Tweet id="...">
...
...
Comment on lines +810 to +816
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.

🛠️ Refactor suggestion

Update documentation examples to match actual return format.

The examples show the result as <Tweet id="..."> but the method actually returns tuples of (tweet_id, tweet).

-        >>> mentions = await client.get_user_mentions('5mknc5', search_count=30)
-        >>> for mention in mentions:
-        ...     print(mention)
-        <Tweet id="...">
-        <Tweet id="...">
+        >>> mentions = await client.get_user_mentions('5mknc5', search_count=30)
+        >>> for tweet_id, tweet in mentions:
+        ...     print(f"Tweet ID: {tweet_id}, Tweet: {tweet}")
+        Tweet ID: 123456789, Tweet: <Tweet id="123456789">
+        Tweet ID: 123456790, Tweet: <Tweet id="123456790">
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
>>> mentions = await client.get_user_mentions('5mknc5', search_count=30)
>>> for mention in mentions:
... print(mention)
<Tweet id="...">
<Tweet id="...">
...
...
>>> mentions = await client.get_user_mentions('5mknc5', search_count=30)
>>> for tweet_id, tweet in mentions:
... print(f"Tweet ID: {tweet_id}, Tweet: {tweet}")
Tweet ID: 123456789, Tweet: <Tweet id="123456789">
Tweet ID: 123456790, Tweet: <Tweet id="123456790">
🤖 Prompt for AI Agents
In twikit/client/client.py around lines 810 to 816, the documentation example
incorrectly shows the method returning <Tweet id="..."> objects, but it actually
returns tuples of (tweet_id, tweet). Update the example to reflect this by
showing the iteration over mentions unpacking or accessing the tuple elements,
demonstrating the correct return format.


"""
result = await self.search_tweet(f"@{handle}", "Latest", count=search_count)
mentions = [
tweet for tweet in result
if f"@{handle.lower()}" in (tweet.full_text or tweet.text or "").strip().lower()
]
mentions.sort(key=lambda x: int(x.id))
return mentions

async def search_user(
self,
query: str,
Expand Down