talonvoice / talon

Issue Tracker for the main Talon app
85 stars 0 forks source link

Feature request: cleanly check for presence of action #520

Open wolfmanstout opened 2 years ago

wolfmanstout commented 2 years ago

Would be helpful to expose a function that returns a bool indicating whether an action is defined. hasattr(actions.user, "some_action") always returns True. Currently I can work around this by catching KeyError, but this isn't the best because it could catch other KeyErrors, and it forces me to do this check right before I want to use the action.

lunixbochs commented 2 years ago

Can you share the broader user story for this?

wolfmanstout commented 2 years ago

Sure, this is about degrading gracefully if knausj_talon isn't present. The specific function I'm applying this to is dictation_peek_left, used in gaze-ocr if available to augment the visual context in some instances where that's helpful (e.g. distinguishing carriage return from wrapped text). The main issue with the workaround for me is simply that it could catch (and hide) other KeyErrors arising from this action.

lunixbochs commented 2 years ago

the easiest answer here might be to throw a more specific exception if an action is missing

wolfmanstout commented 2 years ago

That would work and seems reasonably clean to me. You could raise AttributeError as that is the standard exception raised for this purpose.

Of course if there's a way to support hasattr here that seems like the most intuitive/Pythonic way to handle this.

lunixbochs commented 2 years ago

hasattr internally is just a check for AttributeError, but we need to throw at call time rather than get time, for a variety of reasons