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

[tasks] support defining tasks on a slotted class #9570

Closed
wants to merge 3 commits into from

Conversation

SebbyLaw
Copy link
Contributor

Summary

support defining tasks on a slotted class.

Before:

import asyncio
from discord.ext import tasks

class MyClass:
    __slots__ = ("foo", "bar")

    def __init__(self):
        self.foo = "Hello"
        self.bar = "World!"

    @tasks.loop(seconds=5.0)
    async def baz(self):
        print(self.foo, self.bar)

async def main():
    a = MyClass()
    a.baz.start()  # error

    await asyncio.sleep(float("inf"))

if __name__ == "__main__":
    asyncio.run(main())
Traceback (most recent call last):
  File "/home/sebnlaw/discord.py/_test.py", line 25, in <module>
    asyncio.run(main())
  File "/home/sebnlaw/miniconda3/lib/python3.10/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/home/sebnlaw/miniconda3/lib/python3.10/asyncio/base_events.py", line 649, in run_until_complete
    return future.result()
  File "/home/sebnlaw/discord.py/_test.py", line 19, in main
    a.baz.start()
  File "/home/sebnlaw/discord.py/discord/ext/tasks/__init__.py", line 293, in __get__
    setattr(obj, self.coro.__name__, copy)
AttributeError: 'MyClass' object attribute 'baz' is read-only. Did you mean: 'bar'?

After:

import asyncio
from discord.ext import tasks

class MyClass:
    __slots__ = ("foo", "bar", "_cs_baz")

    def __init__(self):
        self.foo = "Hello"
        self.bar = "World!"

    @tasks.loop(seconds=5.0, slot_name="_cs_baz")
    async def baz(self):
        print(self.foo, self.bar)

async def main():
    a = MyClass()
    a.baz.start()  # yippee

    await asyncio.sleep(float("inf"))

if __name__ == "__main__":
    asyncio.run(main())

Checklist

  • If code changes were made then they have been tested.
    • I have updated the documentation to reflect the changes.
  • This PR fixes an issue.
  • This PR adds something new (e.g. new method or parameters).
  • This PR is a breaking change (e.g. methods or parameters removed/renamed)
  • This PR is not a code change (e.g. documentation, README, ...)

@SebbyLaw SebbyLaw changed the title Tasks slotted owner [tasks] support defining tasks on a slotted class Sep 18, 2023
@Rapptz
Copy link
Owner

Rapptz commented Sep 19, 2023

Thanks for the PR.

I don't really see much point in this one though. If you're making a slotted class which is meant for classes that have thousands of instances then I don't see why you'd have a task associated with each of those instances. Aside from that, you can always just add e.g. baz to the __slots__ list yourself and have it work as-is.

@Rapptz Rapptz closed this Sep 19, 2023
@SebbyLaw
Copy link
Contributor Author

Aside from that, you can always just add e.g. baz to the __slots__ list yourself and have it work as-is.

You cannot.

import asyncio
from discord.ext import tasks

class MyClass:
    __slots__ = ("foo", "bar", "baz")

    def __init__(self):
        self.foo = "Hello"
        self.bar = "World!"

    @tasks.loop(seconds=5.0)
    async def baz(self):
        print(self.foo, self.bar)

async def main():
    a = MyClass()
    a.baz.start()  # error

    await asyncio.sleep(float("inf"))

if __name__ == "__main__":
    asyncio.run(main())
Traceback (most recent call last):
  File "/home/sebnlaw/discord.py/_test.py", line 5, in <module>
    class MyClass:
ValueError: 'baz' in __slots__ conflicts with class variable

@Rapptz
Copy link
Owner

Rapptz commented Sep 19, 2023

Ah right, it fails to create the slot descriptor if it's a class variable. My mistake. I still don't get the point of doing this on a slotted class though.

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