jsonargparse icon indicating copy to clipboard operation
jsonargparse copied to clipboard

Class title is not the name of the class but a string representation of it.

Open eldadcool opened this issue 1 year ago • 3 comments

🐛 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

eldadcool avatar Oct 27 '24 14:10 eldadcool

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__}".

mauvilsa avatar Oct 28 '24 04:10 mauvilsa

@eldadcool would you be interested in contributing the change?

mauvilsa avatar Oct 28 '24 21:10 mauvilsa

Sure, I will create a pull request as soon as I will have time to work on it

eldadcool avatar Oct 29 '24 07:10 eldadcool

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.

mauvilsa avatar Aug 23 '25 20:08 mauvilsa