-
Notifications
You must be signed in to change notification settings - Fork 121
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
Remove the need for explicit event decoration #78
Closed
johnsca
wants to merge
3
commits into
canonical:master
from
johnsca:refactor/automatic-event-wrapping
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -88,7 +88,16 @@ def from_path(cls, path): | |
return handle | ||
|
||
|
||
class EventBase: | ||
class EventMetaType(type): | ||
"""Metaclass for event types which automatically applies the Event descriptor when the | ||
event type is assigned in an emitter class definition. | ||
""" | ||
|
||
def __set_name__(event_type, emitter_type, event_kind): | ||
setattr(emitter_type, event_kind, Event(event_type, event_kind, emitter_type)) | ||
|
||
|
||
class EventBase(metaclass=EventMetaType): | ||
|
||
def __init__(self, handle): | ||
self.handle = handle | ||
|
@@ -115,32 +124,16 @@ def restore(self, snapshot): | |
class Event: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm definitely +1 to renaming this to |
||
"""Event creates class descriptors to operate with events. | ||
|
||
It is generally used as: | ||
|
||
class SomethingHappened(EventBase): | ||
pass | ||
|
||
class SomeObject: | ||
something_happened = Event(SomethingHappened) | ||
|
||
|
||
With that, instances of that type will offer the someobj.something_happened | ||
attribute which is a BoundEvent and may be used to emit and observe the event. | ||
It is automatically applied by the EventMetaType and causes the event types to | ||
be wrapped in BoundEvents when accessed from an emitter instance. The BoundEvent | ||
wrapper allows the event to be emitted and observed via the emitter instance's | ||
framework reference. | ||
""" | ||
|
||
def __init__(self, event_type): | ||
def __init__(self, event_type, event_kind, emitter_type): | ||
if not isinstance(event_type, type) or not issubclass(event_type, EventBase): | ||
raise RuntimeError(f"Event requires a subclass of EventBase as an argument, got {event_type}") | ||
self.event_type = event_type | ||
self.event_kind = None | ||
self.emitter_type = None | ||
|
||
def __set_name__(self, emitter_type, event_kind): | ||
if self.event_kind is not None: | ||
raise RuntimeError( | ||
f'Event({self.event_type.__name__}) reused as ' | ||
f'{self.emitter_type.__name__}.{self.event_kind} and ' | ||
f'{emitter_type.__name__}.{event_kind}') | ||
self.event_kind = event_kind | ||
self.emitter_type = emitter_type | ||
|
||
|
@@ -149,8 +142,14 @@ def __get__(self, emitter, emitter_type=None): | |
return self | ||
return BoundEvent(emitter, self.event_type, self.event_kind) | ||
|
||
def __repr__(self): | ||
return (f'<Event {self.event_type.__name__} assigned as ' | ||
f'{self.event_kind} to {self.emitter_type} ' | ||
f'at {hex(id(self))}') | ||
|
||
|
||
class BoundEvent: | ||
"""An event bound to an emitter.""" | ||
|
||
def __repr__(self): | ||
return (f'<BoundEvent {self.event_type.__name__} bound to ' | ||
|
@@ -247,8 +246,7 @@ def define_event(cls, event_kind, event_type): | |
except AttributeError: | ||
pass | ||
|
||
event_descriptor = Event(event_type) | ||
event_descriptor.__set_name__(cls, event_kind) | ||
event_descriptor = Event(event_type, event_kind, cls) | ||
setattr(cls, event_kind, event_descriptor) | ||
|
||
def events(self): | ||
|
@@ -289,8 +287,8 @@ class CommitEvent(EventBase): | |
|
||
|
||
class FrameworkEvents(EventsBase): | ||
pre_commit = Event(PreCommitEvent) | ||
commit = Event(CommitEvent) | ||
pre_commit = PreCommitEvent | ||
commit = CommitEvent | ||
|
||
|
||
class NoSnapshotError(Exception): | ||
|
@@ -594,7 +592,7 @@ class StoredStateChanged(EventBase): | |
|
||
|
||
class StoredStateEvents(EventsBase): | ||
changed = Event(StoredStateChanged) | ||
changed = StoredStateChanged | ||
|
||
|
||
class StoredStateData(Object): | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is actually the metaclass of the event. Assigned, unassigned, or whatever else happens, it's still its metaclass. It's also a single line long, and never going to be used directly by developers, so no need to document it in that style. Accordingly, its name should indicate what it really is. I suggest
EventMetaType
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Following what's going on when 2 Python features (
__set_name__
and descriptors) are involved is a bit difficult without either helpful naming or a docstring in my view.By glancing over at names at first I could not tell the difference between
Assigned
andBound
.If I read out loud what's going on:
UnassignedEvent
to a class attribute will instead result in an assignment of an event descriptor to that attribute;__get__
method is invoked, an instance ofBoundEvent
is returned which can then be used to emit events tied to an emitter type.I think it should be clear that we aim to bind "unbound" event types to another type. This is done via descriptors so
EventDescriptor
sounds like a good name.UnboundEvent
instead ofUnassignedEvent
correlates withBoundEvent
and makes the intention a bit clearer to me.EventMetaType
would also work so long as there is an explanation about the required binding in the docstring.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I went with
EventMetaType
as suggested and a hopefully short but useful doc string.