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

lookup_cache, update_cache and ignore_cached_errors passed twice, first as keyword args and then in **kwargs, breaks a lot of methods. #255

Closed
AumJavalgikar opened this issue Jan 4, 2025 · 2 comments

Comments

@AumJavalgikar
Copy link
Contributor

In the newest version of coc.py==3.8.0, the async client.get_players() method returns an error,

Traceback (most recent call last):
  File "/Users/user/Documents/project/debugging/main.py", line 46, in <module>
    client.loop.run_until_complete(main(client))
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/asyncio/base_events.py", line 664, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/Users/user/Documents/project/debugging/main.py", line 41, in main
    async for player in client.get_players(players):
  File "/Users/user/Documents/project/venv/lib/python3.12/site-packages/coc/iterators.py", line 40, in __anext__
    msg = await self._next()
          ^^^^^^^^^^^^^^^^^^
  File "/Users/user/Documents/project/venv/lib/python3.12/site-packages/coc/iterators.py", line 108, in _next
    await self._fill_queue()
  File "/Users/user/Documents/project/venv/lib/python3.12/site-packages/coc/iterators.py", line 98, in _fill_queue
    results = await asyncio.gather(*tasks)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/user/Documents/project/venv/lib/python3.12/site-packages/coc/iterators.py", line 90, in _run_method
    return await self.get_method(tag, cls=self.cls, **self.kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/user/Documents/project/venv/lib/python3.12/site-packages/coc/iterators.py", line 142, in get_player
    player = await self.client.get_player(tag, cls=cls, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/user/Documents/project/venv/lib/python3.12/site-packages/coc/client.py", line 2482, in get_player
    return cls(data=data, client=self, load_game_data=load_game_data,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: coc.players.Player() got multiple values for keyword argument 'lookup_cache'

Script to reproduce the problem:

import logging
import os

import coc
from dotenv import load_dotenv


load_dotenv()
user = ...
password = ..

LOGGER_FORMAT = '%(asctime)s %(message)s'
logging.basicConfig(format=LOGGER_FORMAT, datefmt='[%H:%M:%S}')
log = logging.getLogger()
log.setLevel(logging.INFO)

client = coc.client.Client(key_count=1, throttle_limit=20)

players = ['2L0YPQ0G8']

async def main(client: CustomClient):
    await client.login(user, password)
    print('In main')
    async for player in client.get_players(players):
        print(f"{player} received")
    return

if __name__ == '__main__':
    client.loop.run_until_complete(main(client))

This error occurs because of line 2482 in client.py

        return cls(data=data, client=self, load_game_data=load_game_data,
                   lookup_cache=kwargs.get("lookup_cache", self.lookup_cache),
                   update_cache=kwargs.get("update_cache", self.update_cache),
                   ignore_cached_errors=kwargs.get("ignore_cached_errors", self.ignore_cached_errors),
                   **kwargs)

Passing lookup_cache as keyword arg that uses kwargs.get(), which means when kwargs contains {'lookup_cache':..} the lookup_cache keyword argument is passed, but then kwargs still contains lookup_cache, hence it gets passed via **kwargs also, resulting in a TypeError due to the same argument being passed twice, the solution to this would be using kwargs.pop() instead.

kwargs.pop("lookup_cache", self.lookup_cache),
update_cache=kwargs.pop("update_cache", self.update_cache),
ignore_cached_errors=kwargs.pop("ignore_cached_errors", self.ignore_cached_errors),

CTRL+F reveals 42 other instances of kwargs.get("lookup_cache", which do seem to be following the same pattern of passing the kwarg twice, hence I suspect a lot of other methods are also broken due to this.

I have not been following the development of coc.py and hence am not aware where else in the codebase this has been applied, I would be happy to fix this myself, if someone can confirm this is the only file where this has been implemented or there are other places as well.

@MagicTheDev
Copy link
Collaborator

#254

@AumJavalgikar
Copy link
Contributor Author

Understood, thank you.

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

2 participants