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

TypeError when awaiting synchronous _create_user method #191

Open
L235 opened this issue Nov 20, 2024 · 0 comments
Open

TypeError when awaiting synchronous _create_user method #191

L235 opened this issue Nov 20, 2024 · 0 comments

Comments

@L235
Copy link

L235 commented Nov 20, 2024

Description

I'm encountering a TypeError in the pydle library when running my IRC bot. The error occurs because the _create_user method, which is synchronous, is being called with await in asynchronous methods like _sync_user and _rename_user. This leads to an exception since you cannot await a non-coroutine function.

Error Details

Here is the traceback of the error:

TypeError: object NoneType can't be used in 'await' expression

This occurs when the bot receives certain IRC messages and attempts to handle user synchronization.

Code Snippets

From client.py:

def _create_user(self, nickname):
    # Servers are NOT users.
    if not nickname or '.' in nickname:
        return

    self.users[nickname] = {
        'nickname': nickname,
        'username': None,
        'realname': None,
        'hostname': None
    }

async def _sync_user(self, nick, metadata):
    # Create user in database.
    if nick not in self.users:
        await self._create_user(nick)  # <-- This causes the TypeError
        if nick not in self.users:
            return
    self.users[nick].update(metadata)

async def _rename_user(self, user, new):
    if user in self.users:
        self.users[new] = self.users[user]
        self.users[new]['nickname'] = new
        del self.users[user]
    else:
        await self._create_user(new)  # <-- This causes the TypeError
        if new not in self.users:
            return

    for ch in self.channels.values():
        # Rename user in channel list.
        if user in ch['users']:
            ch['users'].discard(user)
            ch['users'].add(new)

In the code above, _create_user is defined as a synchronous function using def, but it's being called with await in the asynchronous methods _sync_user and _rename_user. This mismatch leads to the TypeError.

Steps to Reproduce

  1. Install pydle in a Python 3.9 environment.
  2. Run an IRC bot that connects to a server and handles user joins and messages.
  3. When the bot receives certain messages that trigger _sync_user or _rename_user, the error occurs.

Proposed Solutions

Option 1: Remove await from Calls to _create_user

Since _create_user is synchronous, it should not be awaited. Updating the methods as follows resolves the issue:

async def _sync_user(self, nick, metadata):
    if nick not in self.users:
        self._create_user(nick)  # Removed 'await'
        if nick not in self.users:
            return
    self.users[nick].update(metadata)

async def _rename_user(self, user, new):
    if user in self.users:
        self.users[new] = self.users[user]
        self.users[new]['nickname'] = new
        del self.users[user]
    else:
        self._create_user(new)  # Removed 'await'
        if new not in self.users:
            return

    for ch in self.channels.values():
        if user in ch['users']:
            ch['users'].discard(user)
            ch['users'].add(new)

Option 2: Make _create_user Asynchronous

Alternatively, if there is a need for _create_user to be asynchronous (e.g., future asynchronous operations), it can be defined as an async function:

async def _create_user(self, nickname):
    if not nickname or '.' in nickname:
        return

    self.users[nickname] = {
        'nickname': nickname,
        'username': None,
        'realname': None,
        'hostname': None
    }

However, this requires reviewing all calls to _create_user to ensure they are awaited appropriately.

Additional Information

  • Python Version: 3.9
  • Pydle Version: (Please specify the version you're using)
  • Environment: Running in a miniconda3 environment

Request

Could the maintainers consider adjusting the method definitions or the calls to _create_user to ensure consistency between synchronous and asynchronous functions? This would prevent the TypeError and improve compatibility with asynchronous code.

(Issue largely written using OpenAI's o1-preview model.)

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

No branches or pull requests

1 participant