Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix attributeError of persona, httpxTimeout tweeter #1957

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

nquang29
Copy link
Collaborator

@nquang29 nquang29 commented Mar 6, 2025

File "/app/utils/social.py", line 118, in add_twitter_to_persona
AttributeError: 'NoneType' object has no attribute 'replace'
 File "/app/utils/social.py", line 72, in upsert_persona_from_twitter_profile
AttributeError: 'NoneType' object has no attribute 'replace'
File "/app/routers/apps.py", line 224, in get_or_create_user_persona
AttributeError: 'NoneType' object has no attribute 'replace'
File "/app/utils/social.py", line 67, in verify_latest_tweet
UnboundLocalError: cannot access local variable 'latest_tweet' where it is not associated with a value
File "/app/utils/social.py", line 24, in get_twitter_profile
    response = await client.get(url, headers=headers)
httpx.ReadTimeout

@nquang29 nquang29 requested a review from beastoin March 6, 2025 17:12
@@ -221,7 +221,7 @@ async def get_or_create_user_persona(uid: str = Depends(auth.get_current_user_ui
persona_data = {
'id': persona_id,
'name': user.get('display_name', 'My Persona'),
'username': increment_username(user.get('display_name', 'MyPersona').replace(' ', '').lower()),
'username': increment_username((user.get('display_name') or 'MyPersona').replace(' ', '').lower()),
Copy link
Collaborator

Choose a reason for hiding this comment

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

this is weird 🌚, please check it carefully / go deeper to increment username

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

this still return 'MyPersona' if display_name is empty. I using this code to handle case display_name is None

Copy link
Collaborator

Choose a reason for hiding this comment

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

so you mean user.get('display_name', 'My Persona') will not return My Persona if the dict user does not have display_name ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

user = {
        'uid': "aaa",
        'email': "bbbb",
        'display_name': None,  
    }

i mean with this case, user.get('display_name', 'My Persona') will not return My Persona

@@ -20,7 +20,7 @@ async def get_twitter_profile(handle: str) -> Dict[str, Any]:
"X-RapidAPI-Host": rapid_api_host
}

async with httpx.AsyncClient() as client:
async with httpx.AsyncClient(timeout=rapid_api_timeout) as client:
Copy link
Collaborator

Choose a reason for hiding this comment

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

what is the default value of timeout ? and, should we use the simple request instead of asyncclient ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

default value is 5s, i noticed that timeout error rarely occur (< 10 / day) so i believe increasing the timeout for this request could help reduce errors
because it just a single request so we dont need to use asyncClient

Copy link
Collaborator

Choose a reason for hiding this comment

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

ok please simplify it, use request, and do hard-code 15s timeout, we dont really need then env for this one.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

i replaced asyncclient with simple request

@@ -69,7 +70,7 @@ async def verify_latest_tweet(username: str, handle: str) -> Dict[str, Any]:

async def upsert_persona_from_twitter_profile(username: str, handle: str, uid: str) -> Dict[str, Any]:
profile = await get_twitter_profile(handle)
profile['avatar'] = profile['avatar'].replace('_normal', '')
profile['avatar'] = (profile.get('avatar') or '').replace('_normal', '')
Copy link
Collaborator

@beastoin beastoin Mar 8, 2025

Choose a reason for hiding this comment

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

why don't you use profile.get('avatar', <default>) ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

if avartar = None -> profile.get('avatar', '') will return None, which can't be used with .replace
so i use (profile.get('avatar') or '') to ensure string always return for using .replace

Copy link
Collaborator

@beastoin beastoin Mar 9, 2025

Choose a reason for hiding this comment

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

weird, please double check it. the bellow code is from my playground :)

>>> a = {}
>>> print(a.get('x'))
None
>>> print(a.get('x', 'default'))
default
>>> print(a.get('x', 'default').replace('d', 'a'))
aefault
>>>

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

i mean this case: a = {'x': None}

> print(a.get('x', 'default').replace('d', 'a'))
Traceback (most recent call last):
  File "/tmp/1741493933617c793c7bde2bf4/code/main.py", line 25, in <module>
    print(a.get('x', 'default').replace('d', 'a'))
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'replace'

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.

2 participants