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

Class Decorator API #84

Merged
merged 7 commits into from Jan 12, 2022
Merged

Class Decorator API #84

merged 7 commits into from Jan 12, 2022

Conversation

jfhbrook
Copy link
Owner

This MR fixes #81 by adding a class decorator API for binding events to methods prior to class instantiation.

I'm not in a rush to merge this and I'm not confident in this API so I'm going to leave this up to give people a chance to comment on it. If people really want it I can merge/release.

@johnsonchak
Copy link

It means i can use self argument when i use decorator on ?!

@jfhbrook
Copy link
Owner Author

correct, if you use this API then your event handlers will have a reference to self.

@johnsonchak
Copy link

awesome ! Do you have any plan to merge these changes?

@jfhbrook
Copy link
Owner Author

jfhbrook commented Aug 17, 2021

yeah I think so, just maybe not this week. One challenge with this project is I don't have anyone to spot me but have enough users that having to fix stuff in post kinda sucks. So what I do instead is give myself time to forget about it and review later. But code reviews from interested parties definitely help!

@johnsonchak
Copy link

I want to do so but i am not as good as you did. LOL
Thanks for sharing this project~

@jfhbrook
Copy link
Owner Author

hey, I know people are shy about code review but seriously anything helps! there's no such thing as dumb feedback!

@johnsonchak
Copy link

Ok, i will continue follow this project and try to help it.

Copy link
Owner Author

@jfhbrook jfhbrook left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just some notes to self. I think I'll want to do another iteration of this before merging now that I'm looking at it a few days later.

pyee/cls.py Outdated Show resolved Hide resolved
pyee/cls.py Show resolved Hide resolved
pyee/cls.py Outdated Show resolved Hide resolved
pyee/cls.py Show resolved Hide resolved
tests/test_cls.py Show resolved Hide resolved
tests/test_cls.py Show resolved Hide resolved
@johnsonchak
Copy link

i already add listeners class to achieve use self

@jfhbrook jfhbrook mentioned this pull request Jan 12, 2022
10 tasks
* Set up virtualenv, pyright and isort

* Run isort

* Passing type annotations for base.py

* action to run type checks

* Alas!

* Happy type checker for trio

* MOST of the library is type-checking

* working, non-cranky type annotations for uplift laul

* Type check the tests, cause an explosion

* Clean up requirements.txt

* tests type-checking

* py.typed file

* tests and linting happy

* Update build

* obvious action bugfix

* trailing comma

* remove inconsequential and angry type annotation

* Ignore type issues w asyncio import

* messy typecast

* anyway thats when I started blasting

* carnage!

* uplift bugfixes

* update pytest

* bye 3.6
@jfhbrook jfhbrook merged commit 2e212b0 into main Jan 12, 2022
@jfhbrook jfhbrook deleted the 81-class-decorator-api branch January 12, 2022 04:13
jfhbrook added a commit that referenced this pull request Jan 12, 2022
* Add class decorator API

* Add autofunctions for pyee.cls

* Remove travis file

* docs: Fix a few typos (#91)

* Type Annotations (#97)

* Set up virtualenv, pyright and isort

* Run isort

* Passing type annotations for base.py

* action to run type checks

* Alas!

* Happy type checker for trio

* MOST of the library is type-checking

* working, non-cranky type annotations for uplift laul

* Type check the tests, cause an explosion

* Clean up requirements.txt

* tests type-checking

* py.typed file

* tests and linting happy

* Update build

* obvious action bugfix

* trailing comma

* remove inconsequential and angry type annotation

* Ignore type issues w asyncio import

* messy typecast

* anyway thats when I started blasting

* carnage!

* uplift bugfixes

* update pytest

* bye 3.6

* type annotations for cls

Co-authored-by: Tim Gates <tim.gates@iress.com>
jfhbrook added a commit that referenced this pull request Jan 12, 2022
* Class Decorator API (#84)

* Add class decorator API

* Add autofunctions for pyee.cls

* Remove travis file

* docs: Fix a few typos (#91)

* Type Annotations (#97)

* Set up virtualenv, pyright and isort

* Run isort

* Passing type annotations for base.py

* action to run type checks

* Alas!

* Happy type checker for trio

* MOST of the library is type-checking

* working, non-cranky type annotations for uplift laul

* Type check the tests, cause an explosion

* Clean up requirements.txt

* tests type-checking

* py.typed file

* tests and linting happy

* Update build

* obvious action bugfix

* trailing comma

* remove inconsequential and angry type annotation

* Ignore type issues w asyncio import

* messy typecast

* anyway thats when I started blasting

* carnage!

* uplift bugfixes

* update pytest

* bye 3.6

* type annotations for cls

Co-authored-by: Tim Gates <tim.gates@iress.com>

* added function that returns an array listing the events

* ee.event_names tested and passing

Now that there are some asserts for the value of event_names, we can see
what issue @leirons was running into with `new_listener`. It turns out
the issue ran pretty deep.

Internally, pyee used to use a defaultdict to store events. This was
mildly convenient for implementing on and emit, but it also meant that
event names were added after an emit, even if there were no handlers.

OK, so you patch it to use a regular dict and do the bookkeeping
manually. But there's another reason an event might show up even if it
has no handlers: pyee doesn't make an effort to clean up the
OrderedDicts which contain the actual handlers.

To solve this, I removed the defaultdict (so no event after an
emit) and added a step on listener removal to clean up the OrderedDict.

* Make event_names return a set instead of a list

Co-authored-by: Tim Gates <tim.gates@iress.com>
Co-authored-by: Ivan <grecigor11@gmail.com>
@paulofreitas
Copy link

I came to a use case where I need to emit an event on the evented class __init__() method, but since it's being decorated it won't work because handlers are processed at a later time. Couldn't figure out how to do that without the @evented decorator.

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.

Possible to use @ee.on() decorator on class method?
4 participants