Generate the create function definition for a Component. Args: node: The existing create functiondef node from the ast clz: The Component class to generate the create functiondef for. type_hint_globals: The globals to use to resolving a type hint str. Returns:
(
node: ast.FunctionDef | None,
clz: type[Component],
type_hint_globals: dict[str, Any],
)
| 291 | |
| 292 | |
| 293 | def _generate_component_create_functiondef( |
| 294 | node: ast.FunctionDef | None, |
| 295 | clz: type[Component], |
| 296 | type_hint_globals: dict[str, Any], |
| 297 | ) -> ast.FunctionDef: |
| 298 | """Generate the create function definition for a Component. |
| 299 | |
| 300 | Args: |
| 301 | node: The existing create functiondef node from the ast |
| 302 | clz: The Component class to generate the create functiondef for. |
| 303 | type_hint_globals: The globals to use to resolving a type hint str. |
| 304 | |
| 305 | Returns: |
| 306 | The create functiondef node for the ast. |
| 307 | """ |
| 308 | # add the imports needed by get_type_hint later |
| 309 | type_hint_globals.update( |
| 310 | {name: getattr(typing, name) for name in DEFAULT_TYPING_IMPORTS} |
| 311 | ) |
| 312 | |
| 313 | if clz.__module__ != clz.create.__module__: |
| 314 | _imports = _get_parent_imports(clz.create) |
| 315 | for name, values in _imports.items(): |
| 316 | exec(f"from {name} import {','.join(values)}", type_hint_globals) |
| 317 | |
| 318 | kwargs = _extract_func_kwargs_as_ast_nodes(clz.create, type_hint_globals) |
| 319 | |
| 320 | # kwargs associated with props defined in the class and its parents |
| 321 | all_classes = [c for c in clz.__mro__ if issubclass(c, Component)] |
| 322 | prop_kwargs = _extract_class_props_as_ast_nodes( |
| 323 | clz.create, all_classes, type_hint_globals |
| 324 | ) |
| 325 | all_props = [arg[0].arg for arg in prop_kwargs] |
| 326 | kwargs.extend(prop_kwargs) |
| 327 | |
| 328 | # event handler kwargs |
| 329 | kwargs.extend( |
| 330 | ( |
| 331 | ast.arg( |
| 332 | arg=trigger, |
| 333 | annotation=ast.Name( |
| 334 | id="Optional[Union[EventHandler, EventSpec, list, function, BaseVar]]" |
| 335 | ), |
| 336 | ), |
| 337 | ast.Constant(value=None), |
| 338 | ) |
| 339 | for trigger in sorted(clz().get_event_triggers().keys()) |
| 340 | ) |
| 341 | logger.debug(f"Generated {clz.__name__}.create method with {len(kwargs)} kwargs") |
| 342 | create_args = ast.arguments( |
| 343 | args=[ast.arg(arg="cls")], |
| 344 | posonlyargs=[], |
| 345 | vararg=ast.arg(arg="children"), |
| 346 | kwonlyargs=[arg[0] for arg in kwargs], |
| 347 | kw_defaults=[arg[1] for arg in kwargs], |
| 348 | kwarg=ast.arg(arg="props"), |
| 349 | defaults=[], |
| 350 | ) |
no test coverage detected