Class title is not the name of the class but a string representation of it.
🐛 Bug report
The title of a class added using "add_class_arguments" is printed as str(TestClass) instead of TestClass.__name__ as expected.
This lead to the title be: "<class 'main.TestClass'>" instead of "TestClass"
To reproduce
from jsonargparse import ArgumentParser
class TestClass:
def __init__(self, test_field: int = 5):
pass
parser = ArgumentParser()
parser.add_class_arguments(TestClass)
parser.print_help()
The result is:
usage: [-h] [--test_field TEST_FIELD]
options:
-h, --help Show this help message and exit.
<class '__main__.TestClass'>:
--test_field TEST_FIELD
(type: int, default: 5)
Expected behavior
usage: [-h] [--test_field TEST_FIELD]
options:
-h, --help Show this help message and exit.
TestClass:
--test_field TEST_FIELD
(type: int, default: 5)
Environment
- jsonargparse version: 4.33.2
- Python version: 3.10.12
- How jsonargparse was installed: pip install jsonargparse
- OS: wsl2
Thank you for reporting! Actually this was implemented explicitly like this. The idea was more that people would add docstrings to their classes and functions. But sure, it could be changed to not be a raw str conversion. Though, if changed, I would say it should be a short phrase instead of just the class name, e.g. f"Options for {function_or_class.__name__}".
@eldadcool would you be interested in contributing the change?
Sure, I will create a pull request as soon as I will have time to work on it
I am closing this. I think it is preferable to leave things as they are. Yes, it is ugly, but it is a small motivation for people to add a docstring.