great-tables icon indicating copy to clipboard operation
great-tables copied to clipboard

Refactor: validate the setting of Options values

Open machow opened this issue 1 year ago • 0 comments

Currently, Great Tables models a wide range of options via the Options dataclass. This class looks like this...

@dataclass(frozen=True)
class OptionsInfo:
    scss: bool
    category: str
    type: str
    value: Any

@dataclass(frozen=True)
class Options:
    table_id: OptionsInfo = OptionsInfo(False, "table", "value", None)
    table_caption: OptionsInfo = OptionsInfo(False, "table", "value", None)

Notice three things it defines:

  • options names (e.g. table_id)
  • roughly, the part of the table they apply to (e.g. "table")
  • their 'type' in a loose sense (a string that could be "value", "px", some other things)
  • The default value (e.g. None)

Problem: These are used in a few places, like scss template rendering. However, there's a big challenge we don't have info to address: validating the type when setting options. If we could specify the types accepted for an option (e.g. in OptionInfo), we could automate validating the setting of options by users (e.g. OptionInfo.set(), or .validate() etc..).

Benefit: This would ensure users don't get too far before hitting errors due to invalid option types. It will consolidate the misc code we use to validate into one place, and apply validation consistently.

Examples of custom validation today

If you search for the tab_options() function, validations often happen before it's called. For example, when .opt_table_align_header() sets the heading_align option.

machow avatar Jun 28 '24 16:06 machow