Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove the BaseOption class #14312

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

dcbaker
Copy link
Member

@dcbaker dcbaker commented Feb 28, 2025

This class ends up being a really complicated, not type safe, way of creating a dictionary of OptionKey: UserOption without typing the name twice. We can get the same result with a function that constructs the OptionKey from the UserOption, but with better type safety. It also remove a layer of initialization at startup.

@dcbaker dcbaker requested a review from jpakkane as a code owner February 28, 2025 17:36
We have the OptionKey in the one caller that exists already, and this
allows us to do a hash lookup instead of a linear walk.
Initially this is just used for getting builtin option default values,
but it could be extended to show the default value of an option in the
summary and in the introspection API.
@dcbaker dcbaker force-pushed the submit/remove-base-options-class branch from 6de8803 to a90ab0f Compare February 28, 2025 17:52

base_options = {key: base_opt.init_option(key) for key, base_opt in BASE_OPTIONS.items()}
def _make_option_key(opt: options.AnyOptionType) -> T.Tuple[OptionKey, options.AnyOptionType]:
return OptionKey(opt.name), opt
Copy link
Member

Choose a reason for hiding this comment

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

should we assert opt.name.startswith('b_') ?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think I'd prefer to use a unittest for that if we want to enforce that, to avoid runtime overhead

This class only served one purpose, to avoid typing the name of the
option twice. Unfortunately the way it was implemented made getting the
type checking right difficult, and required storing the same data twice.
This patch replaces this approach with a dictionary comprehension that
creates the OptionKey from the UserOption. This allows us to initialize
a single dictionary once, avoid typing the name twice, delete lines of
code, and get better type safety.

As an added bonus, it means that the exported data from the module can
be marked module constant, ie, ALL_CAPS.
@dcbaker dcbaker force-pushed the submit/remove-base-options-class branch from a90ab0f to e7f4085 Compare February 28, 2025 20:09
@jpakkane
Copy link
Member

jpakkane commented Mar 1, 2025

⚔️ conflicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants