talon
The directory provides a mock implementation of the Talon Voice API. When running unit tests or static analysis tools outside of the active Talon Voice application runner—such as in a continuous integration (CI) pipeline or a local command-line terminal—the native, closed-source talon APIs are not accessible.
These stub files prevent ModuleNotFoundError crashes and allow developers to validate the logic of their community scripts in isolation.
Key Components
Core API Stubs
The __init__.py file acts as the primary mock entry point for the talon package. It mimics core Talon infrastructure:
- Actions Management: Implements the
ActionsandRegisteredActionsAccessorclasses to mimic Talon's action dispatching system. It comes with built-in mock definitions for basic actions likekey,insert,sleep, andedit.selected_text. It supports registering custom test actions viaregister_test_actionand resetting them between runs withreset_test_actions. - Modules and Contexts: Provides dummy implementations of the
ModuleandContextclasses. TheModule.action_classdecorator automatically registers class methods into the mockactionssystem under theusernamespace using Python'sinspectlibrary. - UI and Resources: Stubs out UI frameworks (
ImgUI,UI), configuration storage (Settings,Registry), and resource handling (Resourceredirects file requests to standard Python files;Appdefaults the system platform to"mac"during test runs). - Environment Indicator: Exposes a
test_mode = Trueflag, which allows user-space scripts to detect that they are running within a test suite and adapt their behavior accordingly.
Grammar Stubbing
The grammar.py file provides empty stub classes for Talon's speech-parsing engine:
Phraseandvm.Phrase: These classes are stubbed as empty structures. This allows user code containing type annotations or runtime checks likeisinstance(input, Phrase)to run smoothly and returnFalseduring testing without throwing reference errors.
Experimental API Stubs
The experimental subdirectory mimics the talon.experimental package:
- It provides mock placeholders for advanced UI elements like
TextArea,Span,DarkThemeLabels, andLightThemeLabels. - Setting these components to safe defaults (such as
None) allows scripts relying on experimental UI layouts to be imported and parsed in non-GUI testing environments without causing initialization crashes.
How the Stubs Work Together
When a test runner (like pytest) executes a test suite, it prepends the stub directory to the Python import path (sys.path). When a script runs import talon or from talon import Context, Module, actions, Python loads these mock definitions instead of searching for the real, closed-source Talon runtime.
During execution:
- Module and Context definitions are evaluated, capturing settings, lists, and action registrations.
- Custom user action classes are registered directly into the mock
Actionsregistry via Python decorators in theModulemock. - Tests can mock specific actions dynamically and assert that user-defined commands trigger the correct internal mock behaviors.