Skip to content

Commit

Permalink
Merge pull request #3016 from mkdocs/optw
Browse files Browse the repository at this point in the history
Update warnings of config options
  • Loading branch information
oprypin committed Oct 23, 2022
2 parents 0584e51 + 9d73c2a commit d76cae9
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 39 deletions.
6 changes: 3 additions & 3 deletions mkdocs/config/base.py
Expand Up @@ -363,13 +363,13 @@ def load_config(config_file: Optional[Union[str, IO]] = None, **kwargs) -> MkDoc
errors, warnings = cfg.validate()

for config_name, warning in warnings:
log.warning(f"Config value: '{config_name}'. Warning: {warning}")
log.warning(f"Config value '{config_name}': {warning}")

for config_name, error in errors:
log.error(f"Config value: '{config_name}'. Error: {error}")
log.error(f"Config value '{config_name}': {error}")

for key, value in cfg.items():
log.debug(f"Config value: '{key}' = {value!r}")
log.debug(f"Config value '{key}' = {value!r}")

if len(errors) > 0:
raise exceptions.Abort(f"Aborted with {len(errors)} Configuration Errors!")
Expand Down
18 changes: 12 additions & 6 deletions mkdocs/config/config_options.py
Expand Up @@ -96,11 +96,11 @@ def run_validation(self, value: object) -> SomeConfig:

if self._do_validation:
# Capture errors and warnings
self.warnings = [f'Sub-option {key!r}: {msg}' for key, msg in warnings]
self.warnings = [f"Sub-option '{key}': {msg}" for key, msg in warnings]
if failed:
# Get the first failing one
key, err = failed[0]
raise ValidationError(f"Sub-option {key!r} configuration error: {err}")
raise ValidationError(f"Sub-option '{key}': {err}")

return config

Expand Down Expand Up @@ -309,7 +309,7 @@ def __init__(
else:
message = (
"The configuration option '{}' has been deprecated and "
"will be removed in a future release of MkDocs."
"will be removed in a future release."
)
if moved_to:
message += f" Use '{moved_to}' instead."
Expand Down Expand Up @@ -996,11 +996,17 @@ def load_plugin(self, name: str, config) -> plugins.BasePlugin:
if hasattr(plugin, 'on_startup') or hasattr(plugin, 'on_shutdown'):
self.plugin_cache[name] = plugin

errors, warnings = plugin.load_config(
errors, warns = plugin.load_config(
config, self._config.config_file_path if self._config else None
)
self.warnings.extend(f"Plugin '{name}' value: '{x}'. Warning: {y}" for x, y in warnings)
errors_message = '\n'.join(f"Plugin '{name}' value: '{x}'. Error: {y}" for x, y in errors)
for warning in warns:
if isinstance(warning, str):
self.warnings.append(f"Plugin '{name}': {warning}")
else:
key, msg = warning
self.warnings.append(f"Plugin '{name}' option '{key}': {msg}")

errors_message = '\n'.join(f"Plugin '{name}' option '{key}': {msg}" for key, msg in errors)
if errors_message:
raise ValidationError(errors_message)
return plugin
Expand Down
2 changes: 1 addition & 1 deletion mkdocs/tests/config/base_tests.py
Expand Up @@ -147,7 +147,7 @@ def test_load_missing_required(self, temp_dir):
base.load_config(config_file=config_file.name)
self.assertEqual(
'\n'.join(cm.output),
"ERROR:mkdocs.config:Config value: 'site_name'. Error: Required configuration not provided.",
"ERROR:mkdocs.config:Config value 'site_name': Required configuration not provided.",
)

def test_pre_validation_error(self):
Expand Down
24 changes: 11 additions & 13 deletions mkdocs/tests/config/config_options_legacy_tests.py
Expand Up @@ -190,7 +190,7 @@ class Schema:
{'d': 'value'},
warnings=dict(
d="The configuration option 'd' has been deprecated and will be removed in a "
"future release of MkDocs."
"future release."
),
)

Expand All @@ -209,7 +209,7 @@ class Schema:
{'d': 'value'},
warnings=dict(
d="The configuration option 'd' has been deprecated and will be removed in a "
"future release of MkDocs."
"future release."
),
)

Expand All @@ -223,7 +223,7 @@ class Schema:
{'d': 'value'},
warnings=dict(
d="The configuration option 'd' has been deprecated and will be removed in a "
"future release of MkDocs."
"future release."
),
)

Expand Down Expand Up @@ -252,7 +252,7 @@ class Schema:
{'old': 'value'},
warnings=dict(
old="The configuration option 'old' has been deprecated and will be removed in a "
"future release of MkDocs. Use 'new' instead."
"future release. Use 'new' instead."
),
)
self.assertEqual(conf, {'new': 'value', 'old': None})
Expand All @@ -267,7 +267,7 @@ class Schema:
{'old': 'value'},
warnings=dict(
old="The configuration option 'old' has been deprecated and will be removed in a "
"future release of MkDocs. Use 'foo.bar' instead."
"future release. Use 'foo.bar' instead."
),
)
self.assertEqual(conf, {'foo': {'bar': 'value'}, 'old': None})
Expand All @@ -282,7 +282,7 @@ class Schema:
{'old': 'value', 'foo': {'existing': 'existing'}},
warnings=dict(
old="The configuration option 'old' has been deprecated and will be removed in a "
"future release of MkDocs. Use 'foo.bar' instead."
"future release. Use 'foo.bar' instead."
),
)
self.assertEqual(conf, {'foo': {'existing': 'existing', 'bar': 'value'}, 'old': None})
Expand All @@ -298,7 +298,7 @@ class Schema:
{'old': 'value', 'foo': 'wrong type'},
warnings=dict(
old="The configuration option 'old' has been deprecated and will be removed in a "
"future release of MkDocs. Use 'foo.bar' instead."
"future release. Use 'foo.bar' instead."
),
)

Expand Down Expand Up @@ -1247,7 +1247,7 @@ class Schema:
)

with self.expect_error(
option="Sub-option 'cc' configuration error: Expected one of: ('foo', 'bar') but received: True"
option="Sub-option 'cc': Expected one of: ('foo', 'bar') but received: True"
):
self.get_config(Schema, {'option': {'cc': True}})

Expand Down Expand Up @@ -1320,7 +1320,7 @@ class Schema:
conf = self.get_config(Schema, {'sub': None})

with self.expect_error(
sub="Sub-option 'opt' configuration error: Expected type: <class 'int'> but received: <class 'str'>"
sub="Sub-option 'opt': Expected type: <class 'int'> but received: <class 'str'>"
):
conf = self.get_config(Schema, {'sub': [{'opt': 'asdf'}, {}]})

Expand All @@ -1330,14 +1330,12 @@ class Schema:
self.assertEqual(conf['sub'], [{'opt': 1}, {'opt': 2}])

with self.expect_error(
sub="Sub-option 'opt' configuration error: Expected type: <class 'int'> but "
"received: <class 'str'>"
sub="Sub-option 'opt': Expected type: <class 'int'> but received: <class 'str'>"
):
self.get_config(Schema, {'sub': [{'opt': 'z'}, {'opt': 2}]})

with self.expect_error(
sub="Sub-option 'opt' configuration error: "
"Expected type: <class 'int'> but received: <class 'str'>"
sub="Sub-option 'opt': Expected type: <class 'int'> but received: <class 'str'>"
):
conf = self.get_config(Schema, {'sub': [{'opt': 'z'}, {'opt': 2}]})

Expand Down
30 changes: 14 additions & 16 deletions mkdocs/tests/config/config_options_tests.py
Expand Up @@ -171,7 +171,7 @@ class Schema(Config):
{'d': 'value'},
warnings=dict(
d="The configuration option 'd' has been deprecated and will be removed in a "
"future release of MkDocs."
"future release."
),
)

Expand All @@ -190,7 +190,7 @@ class Schema(Config):
{'d': 'value'},
warnings=dict(
d="The configuration option 'd' has been deprecated and will be removed in a "
"future release of MkDocs."
"future release."
),
)

Expand All @@ -204,7 +204,7 @@ class Schema(Config):
{'d': 'value'},
warnings=dict(
d="The configuration option 'd' has been deprecated and will be removed in a "
"future release of MkDocs."
"future release."
),
)

Expand Down Expand Up @@ -233,7 +233,7 @@ class Schema(Config):
{'old': 'value'},
warnings=dict(
old="The configuration option 'old' has been deprecated and will be removed in a "
"future release of MkDocs. Use 'new' instead."
"future release. Use 'new' instead."
),
)
self.assertEqual(conf, {'new': 'value', 'old': None})
Expand All @@ -248,7 +248,7 @@ class Schema(Config):
{'old': 'value'},
warnings=dict(
old="The configuration option 'old' has been deprecated and will be removed in a "
"future release of MkDocs. Use 'foo.bar' instead."
"future release. Use 'foo.bar' instead."
),
)
self.assertEqual(conf, {'foo': {'bar': 'value'}, 'old': None})
Expand All @@ -263,7 +263,7 @@ class Schema(Config):
{'old': 'value', 'foo': {'existing': 'existing'}},
warnings=dict(
old="The configuration option 'old' has been deprecated and will be removed in a "
"future release of MkDocs. Use 'foo.bar' instead."
"future release. Use 'foo.bar' instead."
),
)
self.assertEqual(conf, {'foo': {'existing': 'existing', 'bar': 'value'}, 'old': None})
Expand All @@ -279,7 +279,7 @@ class Schema(Config):
{'old': 'value', 'foo': 'wrong type'},
warnings=dict(
old="The configuration option 'old' has been deprecated and will be removed in a "
"future release of MkDocs. Use 'foo.bar' instead."
"future release. Use 'foo.bar' instead."
),
)

Expand Down Expand Up @@ -1252,7 +1252,7 @@ class Schema(Config):
option = c.SubConfig(Sub)

with self.expect_error(
option="Sub-option 'cc' configuration error: Expected one of: ('foo', 'bar') but received: True"
option="Sub-option 'cc': Expected one of: ('foo', 'bar') but received: True"
):
self.get_config(Schema, {'option': {'cc': True}})

Expand Down Expand Up @@ -1330,7 +1330,7 @@ class Schema(Config):
conf = self.get_config(Schema, {'sub': None})

with self.expect_error(
sub="Sub-option 'opt' configuration error: Expected type: <class 'int'> but received: <class 'str'>"
sub="Sub-option 'opt': Expected type: <class 'int'> but received: <class 'str'>"
):
conf = self.get_config(Schema, {'sub': [{'opt': 'asdf'}, {}]})

Expand All @@ -1343,14 +1343,12 @@ class Schema(Config):
self.assertEqual(conf.sub[0].opt, 1)

with self.expect_error(
sub="Sub-option 'opt' configuration error: Expected type: <class 'int'> but "
"received: <class 'str'>"
sub="Sub-option 'opt': Expected type: <class 'int'> but received: <class 'str'>"
):
self.get_config(Schema, {'sub': [{'opt': 'z'}, {'opt': 2}]})

with self.expect_error(
sub="Sub-option 'opt' configuration error: "
"Expected type: <class 'int'> but received: <class 'str'>"
sub="Sub-option 'opt': Expected type: <class 'int'> but received: <class 'str'>"
):
conf = self.get_config(Schema, {'sub': [{'opt': 'z'}, {'opt': 2}]})

Expand Down Expand Up @@ -1927,7 +1925,7 @@ class Schema(Config):
}
}
with self.expect_error(
plugins="Plugin 'sample' value: 'bar'. Error: Expected type: <class 'int'> but received: <class 'str'>"
plugins="Plugin 'sample' option 'bar': Expected type: <class 'int'> but received: <class 'str'>"
):
self.get_config(Schema, cfg)

Expand All @@ -1944,8 +1942,8 @@ class Schema(Config):
Schema,
cfg,
warnings=dict(
plugins="Plugin 'sample2' value: 'depr'. Warning: The configuration option "
"'depr' has been deprecated and will be removed in a future release of MkDocs."
plugins="Plugin 'sample2' option 'depr': The configuration option "
"'depr' has been deprecated and will be removed in a future release."
),
)

Expand Down

0 comments on commit d76cae9

Please sign in to comment.