-
Notifications
You must be signed in to change notification settings - Fork 10
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
Connet set attribute to signal (alternate) #38
Conversation
Codecov Report
@@ Coverage Diff @@
## main #38 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 2 2
Lines 355 405 +50
=========================================
+ Hits 355 405 +50
Continue to review full report at Codecov.
|
psygnal/_signal.py
Outdated
class PartialWeakrefCallback: | ||
def __init__(self, slot_partial: PartialMethod): | ||
self.obj, self.name = _get_proper_name(slot_partial.func) | ||
self.args = slot_partial.args | ||
self.kwargs = slot_partial.keywords | ||
|
||
def alive(self) -> bool: | ||
return self.obj() is not None | ||
|
||
def __call__(self, *args: Any, **kwargs: Any) -> Any: | ||
return getattr(self.obj(), self.name)( | ||
*self.args, *args, **self.kwargs, **kwargs | ||
) | ||
|
||
def __eq__(self, other: Any) -> bool: | ||
try: | ||
return ( | ||
isinstance(other, PartialWeakrefCallback) | ||
and self.name == other.name | ||
and self.obj() == other.obj() | ||
and self.args == other.args | ||
and self.kwargs == other.kwargs | ||
) | ||
except: # noqa: E722 | ||
return False | ||
|
||
|
||
class PropertyWeakrefCallback: | ||
def __init__(self, obj: Union[weakref.ref, object], name: str): | ||
if not isinstance(obj, weakref.ref): | ||
obj = weakref.ref(obj) | ||
self.obj = obj | ||
self.name = name | ||
|
||
def alive(self) -> bool: | ||
return self.obj() is not None | ||
|
||
def __call__(self, *args: Any) -> None: | ||
if len(args) == 1: | ||
setattr(self.obj(), self.name, args[0]) | ||
else: | ||
setattr(self.obj(), self.name, args) | ||
|
||
def __eq__(self, other: Any) -> bool: | ||
return ( | ||
isinstance(other, PropertyWeakrefCallback) | ||
and self.name == other.name | ||
and self.obj() == other.obj() | ||
) |
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 decide to switch to class to allow compare two objects
merged #39, so converted this to draft. Some nice class patterns in here that we should remember. perhaps we can figure out the performance stuff later and use it then |
This PR implements #36