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

Allow passing multiple events to the @listen decorator #1103

Merged

Conversation

Jonxslays
Copy link
Contributor

Summary

This pull requests allows passing multiple events to the listen decorator.
Alternatively, a Union can be used in the type hint to achieve the same thing.

@bot.listen(hikari.GuildAvailableEvent, hikari.GuildJoinEvent)
async def on_guild_available(event: ...) -> None:
    ...

@bot.listen()
async def on_start(event: hikari.StartingEvent | hikari.StartedEvent) -> None:
    ...

Checklist

  • I have run nox and all the pipelines have passed.
  • I have made unittests according to the code I have added/modified/deleted.

Flake8 session is failing - but it was on master before I touched anything.

hikari/api/event_manager.py Outdated Show resolved Hide resolved
tests/hikari/impl/test_event_manager_base.py Outdated Show resolved Hide resolved
@thesadru
Copy link
Contributor

Ideally, the origin of the annotation should be checked too in case a type with __args__ is passed in.
Note that in 3.9 there's only typing.Union but in 3.10 you must also check for types.UnionType

@davfsa
Copy link
Member

davfsa commented Mar 25, 2022

Ideally, the origin of the annotation should be checked too in case a type with __args__ is passed in.
Note that in 3.9 there's only typing.Union but in 3.10 you must also check for types.UnionType

This works with all python versions we support, as it never actually checks for the union. It just expands the arguments of the annotation using a typing function, which will support each python version

@thesadru
Copy link
Contributor

thesadru commented Mar 25, 2022

Oops, I wanted to suggest also checking the origin for the edge cases when the passed-in type is not a union. 😅

@davfsa
Copy link
Member

davfsa commented Mar 25, 2022

Oops, I wanted to suggest also checking the origin for the edge cases when the passed-in type is not a union. sweat_smile

In the case it is not a union, it will get expanded as far as it can go and then passed onto subscribe. Subscribe will then make the necessary checks and error accordingly

hikari/impl/event_manager_base.py Outdated Show resolved Hide resolved
tests/hikari/impl/test_event_manager_base.py Outdated Show resolved Hide resolved
hikari/impl/event_manager_base.py Outdated Show resolved Hide resolved
@FasterSpeeding
Copy link
Collaborator

Also this needs a changelog entry

@Jonxslays Jonxslays force-pushed the feature/allow-multiple-event-listener branch from bbec9c8 to b5d858a Compare March 26, 2022 00:29
@FasterSpeeding
Copy link
Collaborator

Linting is failing

@Jonxslays Jonxslays force-pushed the feature/allow-multiple-event-listener branch from b5d858a to ecaf665 Compare March 29, 2022 04:14
@davfsa
Copy link
Member

davfsa commented Mar 29, 2022

Lining failing due to psf/black#2964

Will be fixed by updating black to the latest version, which I will open a PR for in a sec. Then this PR should be rebased

Relevant PR: #1114

@Jonxslays Jonxslays force-pushed the feature/allow-multiple-event-listener branch from ecaf665 to c5d41af Compare March 29, 2022 23:29
@davfsa davfsa merged commit 3253fe3 into hikari-py:master Mar 30, 2022
@Jonxslays Jonxslays deleted the feature/allow-multiple-event-listener branch March 31, 2022 22:29
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.

None yet

4 participants