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

Rework ConfigOption schemas as class-based #2962

Merged
merged 16 commits into from
Sep 26, 2022
Merged

Rework ConfigOption schemas as class-based #2962

merged 16 commits into from
Sep 26, 2022

Conversation

oprypin
Copy link
Contributor

@oprypin oprypin commented Sep 3, 2022

This is NOT a breaking change, the old style keeps working.

Now plugin developers can make a subclass of Config, declare the schema of the config as fields of the class, and instances of this class will hold the processed config.

This better represents the relationship between what a config definition and a config instance is, now you think of config definitions as classes and parsed configs as instances.

We write these fields as descriptors and enable safe attribute-based access. Static analysis will be able to see when a missing field is accessed and, thanks to the Generic type annotations, even if the variable is of a mismatched type!

MkDocs' own config also becomes a subclass of Config, rather than just a sequence of field definitions. Then this MkDocsConfig is used as a more precise type restriction in many places like plugin events.

In the new style, fields become required by default, but can be wrapped into config_options.Optional. Validation of SubConfig is enabled by default as well.

Config subclasses can now also be passed to SubConfig and ConfigItems, rather than just a sequence of field definitions. This also recursively enables type safety.

@oprypin oprypin changed the base branch from master to cfgtest September 5, 2022 00:06
@oprypin oprypin changed the base branch from cfgtest to master September 5, 2022 22:26
@oprypin oprypin added this to the 1.4.0 milestone Sep 17, 2022
@oprypin oprypin changed the base branch from master to meta1 September 24, 2022 15:28
@oprypin oprypin marked this pull request as ready for review September 24, 2022 15:32
Base automatically changed from meta1 to master September 25, 2022 17:16
This is NOT a breaking change, the old style keeps working.

Now developers can make a subclass of Config, declare the schema of the config as fields of the class, and instances of this class will hold the processed config.

This better represents the relationship between what a config definition and a config instance is, now you think of configs definitions as classes and parsed configs as instances.

We also can write these fields as descriptors and enable safe attribute-based access. Static analysis will be able to see when a missing fields is accessed. And in followup changes I plan to add type annotations which will make even type checking fully sound.
when subclassing BasePlugin[FooConfig]
They are validated by default
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.

1 participant