posit-dev / great-tables

Make awesome display tables using Python.
https://posit-dev.github.io/great-tables/
MIT License
1.42k stars 48 forks source link

Refactor: validate the setting of Options values #386

Open machow opened 3 days ago

machow commented 3 days ago

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:

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.