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

Give a UserWarning when Parameter is overriden by name #2397

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES.rst
Expand Up @@ -5,6 +5,9 @@ Version 8.2.0

Unreleased

- Adds a UserWarning when multiple parameters attempt to use the same
name. :issue:`2396``


Version 8.1.3
-------------
Expand Down
16 changes: 16 additions & 0 deletions src/click/decorators.py
Expand Up @@ -267,6 +267,22 @@ def _param_memo(f: FC, param: Parameter) -> None:
else:
if not hasattr(f, "__click_params__"):
f.__click_params__ = [] # type: ignore
else:
for opt in param.opts:
for preexisting_param in f.__click_params__: # type: ignore
if opt in preexisting_param.opts:

from warnings import warn

warn(
(
"Duplicate option added to command."
" The following option appears more than once:\n"
f"{opt} (used for {param.human_readable_name}"
f" and {preexisting_param.human_readable_name})"
),
stacklevel=3,
)

f.__click_params__.append(param) # type: ignore

Expand Down
10 changes: 10 additions & 0 deletions tests/test_options.py
Expand Up @@ -922,3 +922,13 @@ def test_invalid_flag_combinations(runner, kwargs, message):
click.Option(["-a"], **kwargs)

assert message in str(e.value)


def test_overridden_option_triggers_warning(runner):
with pytest.warns(UserWarning):

@click.command()
@click.option("-a", "--aardvark")
@click.option("-a", "--avocado")
def cli(aardvark, avocado):
pass