-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Description
Which component has the problem?
CuTe DSL
Bug Report
Describe the bug
When building a CLI, importing cutlass_library before parsing command line arguments breaks the help message for the CLI. My example uses click, but the same should hold true for any CLI tooling.
Steps/Code to reproduce bug
MCVE (in environment with click and cutlass installed from PyPI)
import click
import cutlass_library
@click.group()
def mycli():
pass
@mycli.command()
def mysubcommand():
print("Hello world")
if __name__ == "__main__":
mycli()$ python mycli.py --help
usage: mycli.py [-h] [--disable-cutlass-package-imports]
options:
-h, --help show this help message and exit
--disable-cutlass-package-imports
Disable use of cutlass_library from Python package
Note that the subcommand does trigger correctly even if the help is wrong, but incorrect help is pretty bad for my users!
Expected behavior
As occurs if I comment out the import cutlass_library line:
$ python mycli.py --help
Usage: mycli.py [OPTIONS] COMMAND [ARGS]...
Options:
--help Show this message and exit.
Commands:
mysubcommand
Workaround
Avoid importing cutlass_library until inside the CLI functions, e.g., change the subcommand above to:
@mycli.command()
def mysubcommand():
import cutlass_library
print("Hello world")What's happening
Here's the problem code:
cutlass/python/cutlass_library/generator.py
Lines 67 to 76 in b2ca083
| # Create a temporary argument parser to check only for the availability of the | |
| # --disable-cutlass-package-imports argument, which controls whether package-based | |
| # imports are disabled. | |
| def _add_package_disablement_flag(argparser): | |
| argparser.add_argument("--disable-cutlass-package-imports", action='store_true', required=False, | |
| help="Disable use of cutlass_library from Python package") | |
| _parser = argparse.ArgumentParser() | |
| _add_package_disablement_flag(_parser) | |
| _args, _ = _parser.parse_known_args() |
The fundamental issue is that the first parser that tries to parse any known command line arguments is the temporary parser linked above. If given the --help option, it does the default behavior for the --help option: It gives the help and exits. But this is happening on import, before progressing to the rest of the CLI, so any CLI that imports cutlass_library early will get the help statement from the temporary parser (and the code will exit).
Solution
Use add_help=False when creating the parser. PR incoming.
Environment details (please complete the following information):
- Environment location: Happens to be a uv venv on an AWS box based off an ubuntu DLAMI, but that probably doesn't matter.