Skip to content

Commit

Permalink
Strip trailing _ from config members, to bypass Python reserved words
Browse files Browse the repository at this point in the history
Example of adding an `async` boolean option that can be read through `config['async']` or `config.async_`:

    class ExampleConfig(Config):
        async_ = Type(bool, default=False)
  • Loading branch information
oprypin committed Jun 3, 2023
1 parent fce89f7 commit 1db8e88
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion mkdocs/config/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ def post_validation(self, config: Config, key_name: str) -> None:
"""

def __set_name__(self, owner, name):
if name.endswith('_') and not name.startswith('_'):
name = name[:-1]
self._name = name

@overload
Expand Down Expand Up @@ -123,7 +125,7 @@ def __init_subclass__(cls):
schema = dict(getattr(cls, '_schema', ()))
for attr_name, attr in cls.__dict__.items():
if isinstance(attr, BaseConfigOption):
schema[attr_name] = attr
schema[getattr(attr, '_name', attr_name)] = attr
cls._schema = tuple(schema.items())

for attr_name, attr in cls._schema:
Expand Down

0 comments on commit 1db8e88

Please sign in to comment.