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

Idea: Allow conect setting attribute to Signal #36

Closed
Czaki opened this issue Nov 7, 2021 · 2 comments · Fixed by #39
Closed

Idea: Allow conect setting attribute to Signal #36

Czaki opened this issue Nov 7, 2021 · 2 comments · Fixed by #39

Comments

@Czaki
Copy link
Contributor

Czaki commented Nov 7, 2021

As psygnal comes from Qt which is a C++ world that does not contain the equivalent of @property or descriptors. Using property is quite popular in python projects, but connected with signals could produce a big amount of redundant code.

So maybe it could be a nice idea to allow connect a property to signal.

def property_weakref(obj: Union[weakref.ref, object], name:str) -> Tuple[weakref.ref, Callable]:
    if not isinstance(obj, weakref.ref):
        obj = weakref.ref(obj)
    def wrap(*args: Any) -> None:
        if len(args) == 1:
            setattr(obj(), name, args[0])
        else:
            setattr(obj(), name, args)

    return (obj, wrap)

and a proper modification for the connect function which allows providing tuple (obj, str) as input.

@tlambert03
Copy link
Member

love it. dealing with setter callbacks is one of the most annoying things about using property.setter... would be great to make that case seamless

@tlambert03
Copy link
Member

(can't decide if i think it should be a new connect method though:

emitter.connect_attr(obj, 'attr_name')

do you want to implement this? Or just happy to see it get added?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

2 participants