Skip to content

Commit

Permalink
fix: Raise ValueError in get_component_by_name if module or class don…
Browse files Browse the repository at this point in the history
…'t exist
  • Loading branch information
saattrupdan committed May 22, 2024
1 parent 6facb53 commit aa64ac4
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/ragger/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,27 @@ def get_component_by_name(class_name: str, component_type: str) -> Type:
Returns:
The class.
Raises:
ValueError:
If the module or class cannot be found.
"""
# Get the snake_case and PascalCase version of the class name
full_class_name = f"{class_name}_{component_type}"
name_pascal = snake_to_pascal(snake_string=full_class_name)

# Get the class from the module
# Get the module
module_name = f"ragger.{component_type}"
module = importlib.import_module(name=module_name)
class_: Type = getattr(module, name_pascal)
try:
module = importlib.import_module(name=module_name)
except ModuleNotFoundError:
raise ValueError(f"Module {module_name!r}' not found.")

# Get the class from the module
try:
class_: Type = getattr(module, name_pascal)
except AttributeError:
raise ValueError(f"Class {name_pascal!r} not found in module {module_name!r}.")

return class_

Expand Down

0 comments on commit aa64ac4

Please sign in to comment.