Skip to content

Commit

Permalink
improve: warn when CLI defaults conflict with TOML (#512)
Browse files Browse the repository at this point in the history
* fix(#507): warn on store_true and store_false

* fix: more readable plugin name in warning

* refactor: implement code review feedback

* refactor: split long-string for black

* refactor: use 'not in'

* Revert "refactor: split long-string for black"

This reverts commit 55db115.

* fix: use source of plugin

Co-authored-by: Taneli Hukkinen <[email protected]>

* refactor: move text inline and add resolution guidance

* refactor: consider restoring UserWarning

* Update src/mdformat/_cli.py

* Update tests/test_plugins.py

---------

Co-authored-by: Taneli Hukkinen <[email protected]>
  • Loading branch information
KyleKing and hukkin authored Feb 6, 2025
1 parent 6b47904 commit 56e221d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/mdformat/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,16 @@ def make_arg_parser(
plugin.add_cli_argument_group(group)
for action in group._group_actions:
action.dest = f"plugin.{plugin_id}.{action.dest}"
if action.default not in {None, argparse.SUPPRESS}:
import warnings

plugin_file, plugin_line = get_source_file_and_line(plugin)
warnings.warn_explicit(
f"The `default` ({action.default!r}) for {action.option_strings!r} from the {plugin_id!r} plugin, will always override any value configured in TOML. The only supported CLI defaults are `None` or `argparse.SUPPRESS`. To resolve, consider refactoring to `.add_argument(..., default=None)` ", # noqa: E501
DeprecationWarning,
filename=plugin_file,
lineno=plugin_line,
)
return parser


Expand Down
29 changes: 29 additions & 0 deletions tests/test_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,35 @@ def test_cli_options_group(monkeypatch, tmp_path):
)


def test_plugin_argument_warnings(monkeypatch, tmp_path):
"""Test for warnings of plugin arguments that conflict with TOML."""

class ExamplePluginWithStoreTrue:
@staticmethod
def update_mdit(mdit: MarkdownIt):
pass

@staticmethod
def add_cli_argument_group(group: argparse._ArgumentGroup) -> None:
group.add_argument("--store-true", action="store_true")
group.add_argument("--store-false", action="store_false")
group.add_argument("--store-zero", default=0)
group.add_argument("--store-const", action="store_const", const=True)

monkeypatch.setitem(PARSER_EXTENSIONS, "table", ExamplePluginWithStoreTrue)
file_path = tmp_path / "test_markdown.md"
file_path.touch()

with patch.object(MDRenderer, "render", return_value=""):
with pytest.warns(DeprecationWarning) as warnings:
assert run([str(file_path)]) == 0

assert "--store-true" in str(warnings.pop().message)
assert "--store-false" in str(warnings.pop().message)
assert "--store-zero" in str(warnings.pop().message)
assert len(warnings) == 0


def test_cli_options_group__no_toml(monkeypatch, tmp_path):
"""Test add_cli_argument_group plugin API with configuration only from
CLI."""
Expand Down

0 comments on commit 56e221d

Please sign in to comment.