fix: preserve shared option callback values#3562
Open
suzunn wants to merge 1 commit into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Shared options that target the same parameter can process the same parser value more than once. When one option has a callback and another sibling option does not, the callback result can be overwritten by the sibling's raw parser value. In #2786 this allows an internal
flag_valuesentinel to reach the command callback when only the shortcut flag is provided.Solution
I tightened the shared-parameter arbitration so a callback-less option with the same command-line source does not replace an existing processed value with a different raw value. This preserves the callback result while keeping the existing explicit-source ordering behavior for normal shared options.
I also added regression coverage for the flag-only case and both mixed ordering cases:
--fetchkeeps the callback result--custom typed --fetchlets the later flag win--fetch --custom typedlets the later explicit value winTesting
uv run --group tests pytest tests\test_options.py -k "shared_option_callback_value_is_preserved or default_dual_option_callback" -quv run --group tests pytest tests\test_options.py -quv run ruff check src\click\core.py tests\test_options.pyuv run --group tests pytest -qgit diff --check