Fix eagerness of help option generated by help_option_names#2811
Merged
AndreasBackx merged 2 commits intopallets:stablefrom Nov 30, 2024
kdeldycke:eagerness-sorting-unittest
Merged
Fix eagerness of help option generated by help_option_names#2811AndreasBackx merged 2 commits intopallets:stablefrom kdeldycke:eagerness-sorting-unittest
help_option_names#2811AndreasBackx merged 2 commits intopallets:stablefrom
kdeldycke:eagerness-sorting-unittest
Conversation
help_option_names
kdeldycke
added a commit
to kdeldycke/click-extra
that referenced
this pull request
Nov 28, 2024
AndreasBackx
requested changes
Nov 28, 2024
This enforce respect of its eagerness
Collaborator
Author
|
Just fixed all the remaining issues. I was just wondering why @cached_property
def help_option(self) -> t.Optional["Option"]:
ctx = self.get_current_context()
help_options = self.get_help_option_names(ctx)
if not help_options or not self.add_help_option:
return None
# Avoid circular import.
from .decorators import HelpOption
return HelpOption(help_options)
def get_help_option(self, ctx: Context) -> t.Optional["Option"]:
return self.help_optionBut I guess there are some edge-cases regarding the context management. And I don't want to propose refactors that are too deep just as 8.1.8 is around the corner and the 8.2.0 is coming soon after. Anyway, this PR is ready to be merged upstream for the 8.1.8 release. |
Collaborator
|
Thanks for the fix! We can look at cached properties in the future perhaps. I'm not bothered by a simple if None. |
Collaborator
Author
|
Thanks @AndreasBackx for the merge! |
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
When a help option is generated by the use of
help_option_namessetting, it loose its eagerness. This issue has an effect on the order in which parameters (and their callbacks) are processed, and breaks the evaluation order described in the documentation: https://click.palletsprojects.com/en/stable/advanced/#callback-evaluation-orderThat's because the utility method
iter_params_for_processing()is comparing option objects, and the help option is generated on everyget_help_option()orget_params(ctx)call. This behavior is demonstrated by a unit-test in this PR.My solution to fix this issue consist in caching locally the auto-generated help option. This solves the issue.
I used this oportunity to clarify some documentation, and unit-test the main method responsible for the ordering of parameters.
This is more or less a follow up of #2563, which has been merged in the upcoming v8.1.8 release.
Checklist:
CHANGES.rstsummarizing the change and linking to the issue... versionchanged::entries in any relevant code docs.pytestandtox, no tests failed.