Repository navigation
Closed
Labels
help outputHelp text formatting, layout, and --help rendering
Milestone
Description
Click 8.18 has a regression wrt. to help with Option subclasses.
This was found in this issue originally:
This was introduced in this PR by @kdeldycke 👋 :
It breaks code that uses option subclasses.
- This code works in 8.1.7 and 8.1.8 with no custom class
import click
@click.command()
@click.help_option("-h", "--help")
def scancode():
"""OK in pkg:pypi/[email protected] and pkg:pypi/[email protected]"""
pass
if __name__ == "__main__":
scancode()and then with 8.1.7 and 8.1.8:
$ python works.py --help
Usage: works.py [OPTIONS]
Regression in pkg:pypi/[email protected]
Options:
-h, --help Show this message and exit.
- This code works in 8.1.7 and fails in 8.1.8 with a custom class
import click
class PluggableCommandLineOption(click.Option):
pass
@click.command()
@click.help_option("-h", "--help", cls=PluggableCommandLineOption)
def scancode():
"""Regression in pkg:pypi/[email protected]"""
pass
if __name__ == "__main__":
scancode()and then with 8.1.7
python failing.py --help
Usage: failing.py [OPTIONS]
Regression in pkg:pypi/[email protected]
Options:
-h, --help Show this message and exit.
$ python failing.py -h
Usage: failing.py [OPTIONS]
Regression in pkg:pypi/[email protected]
Options:
-h, --help Show this message and exit.
and then with 8.1.8
$ python failing.py -h
Error: Option '-h' requires an argument.
$ python failing.py --help
Error: Option '--help' requires an argument.
- This code works more or less in 8.1.7 and 8.1.8 with custom class and no "--help" option
import click
class PluggableCommandLineOption(click.Option):
pass
@click.command()
@click.help_option("-h", cls=PluggableCommandLineOption)
def scancode():
"""Regression in pkg:pypi/[email protected]"""
pass
if __name__ == "__main__":
scancode()and then with 8.1.7
$ python works2.py -h
Usage: works2.py [OPTIONS]
Regression in pkg:pypi/[email protected]
Options:
-h Show this message and exit.
--help Show this message and exit.
and then with 8.1.7
$ python works2.py --help
Usage: works2.py [OPTIONS]
Regression in pkg:pypi/[email protected]
Options:
-h Show this message and exit.
--help Show this message and exit
and then with 8.1.8
$ python works2.py -h
Error: Option '-h' requires an argument.
and then with 8.1.8 note the changes in -h TEXT
$ python works2.py --help
Usage: works2.py [OPTIONS]
Regression in pkg:pypi/[email protected]
Options:
-h TEXT
--help Show this message and exit.
Environment:
- Python version: 3.9 to 3.11
- Click version: 8.1.8
Metadata
Metadata
Assignees
Labels
help outputHelp text formatting, layout, and --help rendering
Activity
pombredanne commentedon Dec 30, 2024
@kdeldycke I guess that no refactor goes unpunished!
[-]Regression: Help option sublassing no longer works with verion 8.1.8 [/-][+]Regression: Help option subclassing no longer works with verion 8.1.8 [/+]pombredanne commentedon Jan 6, 2025
The short term fix by @AyanSinhaMahapatra is that we skip using 8.1.8 entirely in https://github.com/aboutcode-org/scancode-toolkit/pull/4043/files#diff-fa602a8a75dc9dcc92261bac5f533c2a85e34fcceaff63b3a3a81d9acde2fc52R70
click >= 6.7, !=7.0, !=8.1.8kdeldycke commentedon Jan 8, 2025
I just started to update Click Extra to the new v8.1.8 (bad release timing 😅), but I've already seen some strange behavior with the help option.
Thanks @pombredanne for the details, I'll investigate the issue and keep you informed.
kdeldycke commentedon Jan 9, 2025
An immediate solution would be to make the custom class inherit
click.decorators.HelpOptioninstead ofclick.Option, like so:kdeldycke commentedon Jan 9, 2025
The root of the problem is that by deduplicating the code generating the help option in 8.1.8, I introduced a
HelpOptionclass. This class carry all the setup necessary to make a standardOptionbehave like an help option.The problem is by using a custom class on the
@help_optiondecorator, you bypass all the custom setup made byHelpOption. Hence the discrepency in behavior between 8.1.7 and 8.1.8.I just made a proposal in PR #2840 to revert the behavior to what it was in 8.1.7.
Set --help/-h as defaults for @help_option decorator
AndreasBackx commentedon Jan 12, 2025
@kdeldycke I assume we want to keep the
HelpOptionin 8.2.0 but just not include it in 8.1.x, right? If so, could you verify this is documented in 8.2.0? I don't think we should stop 8.2.0's release for this, but it's good to have it documented.kdeldycke commentedon Jan 12, 2025
In retrospect I think introducing the
HelpOptionwas a mistake.Yes, it was more in line with the original (duplicated) implementation of the
--helpoption. And it made the code within theget_help_option()less hackish than my proposal in #2840 (see that ugly construct ending withdummy_func.__click_params__.pop()).But on the other hand we ended up with the
@help_optiondecorator being too dissimilar from the@password_option,@version_optionand other pre-configured option proposed by Click.For the sake of simplicity and keeping the code aligned, I would propose to go with my implementation in #2840 for both 8.1.x and 8.2.0, and forget about changing the option decorators from decorators to classes altogether.
Rowlando13 commentedon Mar 17, 2025
@davidism Could you weigh in on this?
davidism commentedon Mar 17, 2025
The PR in #2840 looks good, with a couple suggestions before merging.
Long term, it would definitely be good to attempt to unify the decorators with the classes more.
kdeldycke commentedon Mar 18, 2025
@Rowlando13 I just merged back the suggestions from @davidism, so unless you find something else, #2840 is ready to be merged upstream.
kdeldycke commentedon Mar 19, 2025
Thanks @Rowlando13 for the merge!