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

add arguments to listener call #4

Closed

Conversation

jovanepires
Copy link

Just an improvement I needed and I think it would be interesting for other people.

@coveralls
Copy link

Pull Request Test Coverage Report for Build 36

  • 4 of 4 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.2%) to 94.118%

Totals Coverage Status
Change from base Build 31: 0.2%
Covered Lines: 62
Relevant Lines: 64

💛 - Coveralls

@coveralls
Copy link

coveralls commented Apr 12, 2020

Pull Request Test Coverage Report for Build 39

  • 4 of 4 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+1.1%) to 95.05%

Totals Coverage Status
Change from base Build 31: 1.1%
Covered Lines: 61
Relevant Lines: 62

💛 - Coveralls

update requirements

fix coverage
@jovanepires
Copy link
Author

@hartym could you review this feature?

@neronmoon
Copy link

neronmoon commented Apr 17, 2020

Please, review! @hartym

Btw, @jovanepires is this feature can pass arguments as kwargs instead of args? Would be nice!

@jovanepires
Copy link
Author

Please, review! @hartym

Btw, @jovanepires is this feature can pass arguments as kwargs instead of args? Would be nice!

Not currently, but I could implement it.

Thank you for your support!

@hartym
Copy link
Member

hartym commented Dec 9, 2023

Very sorry for not seeing this, I'm very bad with notifications and whistle being very simple and stable, I don't come there often (although I'm currently working on a 2.0 supporting async events).

I don't really see why this would be useful. Usually, I create an Event class that contains all required parameters, and pass it to the dispatch method (see below for example).

Also, current usage encourage writing things like:

event = SomeEvent()
dispatcher.dispatch('foo', event)

With your patch, there would be an unwanted side effect of considering event as the first element of args instead of event, which changes the general usage behaviour. We could write code to consider the first element of args as event value if event is None, but that would be a little dubious code.

To achieve your use case, please just create your event class and embed your arguments in it:

class MyEvent(Event):
    def __init__(self, foo, bar):
        self.foo = foo
        self.bar = bar

dispatcher.dispatch("hello", MyEvent('foo', 'bar'))

def handler(event: MyEvent):
    print(event.foo, event.bar)

This way you can implement whatever you want, without adding arguments magic into the dispatcher.

Is there a concrete use case that you implement with *args that you can not implement by having your own event class ?

@hartym hartym closed this Dec 9, 2023
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