Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/lightning/pytorch/callbacks/progress/rich_progress.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,9 @@ class RichProgressBarTheme:
metrics_text_delimiter: str = " "
metrics_format: str = ".3f"

def __init__(self) -> None:
pass
Comment on lines +224 to +225
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is unclear to me why this change is required. Data-classes generally don't require an init function

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The redundant __init__() here is just a workaround to ensure that jsonargparse or typeshed_client can correctly parse RichProgressBarTheme when it is used as the default argument to RichProgressBar in the CLI, as shown in the script above.

Without this workaround, the script throws a key-validation error:

$ python3 demo.py fit
usage: test.py [-h] [-c CONFIG] [--print_config[=flags]] {fit,validate,test,predict} ...
error: Validation failed: Parser key "rich_progress_bar.theme.progress_bar":
  Does not validate against any of the Union subtypes
  Subtypes: [<class 'str'>, <class 'rich.style.Style'>]
  Errors:
    - Expected a <class 'str'>. Got value: Not a valid subclass of Style. Got value: None
      Subclass types expect one of:
      - a class path (str)
      - a dict with class_path entry
      - a dict without class_path but with init_args entry (class path given previously)
    - Not a valid subclass of Style. Got value: Not a valid subclass of Style. Got value: None
      Subclass types expect one of:
      - a class path (str)
      - a dict with class_path entry
      - a dict without class_path but with init_args entry (class path given previously)
      Subclass types expect one of:
      - a class path (str)
      - a dict with class_path entry
      - a dict without class_path but with init_args entry (class path given previously)
  Given value type: <class 'ValueError'>
  Given value: Not a valid subclass of Style. Got value: None
  Subclass types expect one of:
  - a class path (str)
  - a dict with class_path entry
  - a dict without class_path but with init_args entry (class path given previously)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah but then it should be a subclass of rich.style.Style actually and not a dataclass anymore

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not familiar with the internal parsing mechanism, but as far as I know, either adding the redundant __init__() to this dataclass, or replacing all default values of arguments of RichProgressBarTheme typed Union[str, "Style"] with "" can solve this problem, where the latter one is clearly not ideal.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The following fix correctly applies the options to the progress bar theme, but it still triggers a JsonargparseWarning (although it is no longer an error):

@dataclass
class RichProgressBarTheme:
    description: Union[str, "Style"] = ""
    progress_bar: Union[str, "Style"] = Style(color="#6206E0")
    progress_bar_finished: Union[str, "Style"] = Style(color="#6206E0")
    progress_bar_pulse: Union[str, "Style"] = Style(color="#6206E0")
    batch_progress: Union[str, "Style"] = ""
    time: Union[str, "Style"] = Style(dim=True)
    processing_speed: Union[str, "Style"] = Style(dim=True, underline=True)
    metrics: Union[str, "Style"] = Style(italic=True)
    metrics_text_delimiter: str = " "
    metrics_format: str = ".3f"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mauvilsa could you maybe advise what's the best way to go here? Adding an init function to a data-class seems not very pythonic to me.



class RichProgressBar(ProgressBar):
"""Create a progress bar with `rich text formatting <https://github.com/Textualize/rich>`_.
Expand Down