pyqt_reactive.forms.widget_factory
Widget factory with explicit type-based dispatch.
Replaces duck typing with fail-loud type checking. Mirrors MemoryType converter pattern - explicit type → widget mapping.
Design: - WIDGET_TYPE_REGISTRY: Type → factory function mapping - Explicit dispatch (no hasattr checks) - Fail-loud if type not registered - Handles Optional[T], Enum, List[Enum] types
Functions
|
Extract enum type from List[Enum]. |
|
Check if type is an Enum. |
|
Check if type is List[Enum]. |
|
Resolve Optional[T] to T. |
Classes
Widget factory using explicit type-based dispatch. |
- pyqt_reactive.forms.widget_factory.resolve_optional(param_type: Type) Type[source]
Resolve Optional[T] to T.
- Parameters:
param_type – Type to resolve (e.g., Optional[int])
- Returns:
Unwrapped type (e.g., int)
- pyqt_reactive.forms.widget_factory.is_enum_type(param_type: Type) bool[source]
Check if type is an Enum.
- Parameters:
param_type – Type to check
- Returns:
True if param_type is an Enum subclass
- pyqt_reactive.forms.widget_factory.is_list_of_enums(param_type: Type) bool[source]
Check if type is List[Enum].
- Parameters:
param_type – Type to check
- Returns:
True if param_type is List[SomeEnum]
- pyqt_reactive.forms.widget_factory.get_enum_from_list(param_type: Type) Type[source]
Extract enum type from List[Enum].
- Parameters:
param_type – List[Enum] type
- Returns:
The Enum type
- class pyqt_reactive.forms.widget_factory.WidgetFactory[source]
Widget factory using explicit type-based dispatch.
Replaces duck typing with fail-loud type checking. Mirrors the pattern from MemoryType converters.
Example
factory = WidgetFactory()
# Create widget for int parameter widget = factory.create_widget(int, “my_param”) # Returns SpinBoxAdapter instance
# Create widget for Enum parameter widget = factory.create_widget(MyEnum, “mode”) # Returns ComboBoxAdapter populated with enum values
- create_widget(param_type: Type, param_name: str = '') Any[source]
Create widget for parameter type using explicit dispatch.
- Parameters:
param_type – The parameter type to create widget for
param_name – Optional parameter name for debugging
- Returns:
Widget instance implementing required ABCs
- Raises:
TypeError – If no widget registered for this type
Example
>>> factory = WidgetFactory() >>> widget = factory.create_widget(int, "threshold") >>> isinstance(widget, SpinBoxAdapter) True
- register_widget_type(param_type: Type, factory_func: Callable) None[source]
Register custom widget factory for a type.
- Parameters:
param_type – The Python type to register
factory_func – Function that creates widget instance (no args)
Example
>>> def create_path_widget(): ... return PathSelectorWidget() >>> factory = WidgetFactory() >>> factory.register_widget_type(Path, create_path_widget)