Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

format_usage() doesn't work on captured parser #501

Closed
maticus opened this issue May 17, 2024 · 2 comments · Fixed by #502
Closed

format_usage() doesn't work on captured parser #501

maticus opened this issue May 17, 2024 · 2 comments · Fixed by #502
Labels
bug Something isn't working

Comments

@maticus
Copy link

maticus commented May 17, 2024

🐛 Bug report

jsonargparse.capture_parser(main).format_usage() fails with
LookupError: <ContextVar name='parent_parser' at 0x7f0859cb54e0>

To reproduce

import jsonargparse

def fun(a: int):
    pass

def main():
    jsonargparse.CLI(fun)

# THIS IS OK
jsonargparse.capture_parser(main).print_usage()

# THIS FAILS
print(jsonargparse.capture_parser(main).format_usage())

Output:

$ python python-jsonargparse-format-usage.py 
usage: python-jsonargparse-format-usage.py [-h] [--config CONFIG] [--print_config[=flags]] a
Traceback (most recent call last):
  File "/home/mati/tests/python-jsonargparse-format-usage.py", line 12, in <module>
    print(jsonargparse.capture_parser(main).format_usage())
  File "/usr/lib/python3.10/argparse.py", line 2540, in format_usage
    return formatter.format_help()
  File "/usr/lib/python3.10/argparse.py", line 295, in format_help
    help = self._root_section.format_help()
  File "/usr/lib/python3.10/argparse.py", line 226, in format_help
    item_help = join([func(*args) for func, args in self.items])
  File "/usr/lib/python3.10/argparse.py", line 226, in <listcomp>
    item_help = join([func(*args) for func, args in self.items])
  File "/home/mati/.local/lib/python3.10/site-packages/jsonargparse/_formatters.py", line 90, in _format_usage
    parser = parent_parser.get()
LookupError: <ContextVar name='parent_parser' at 0x7f2efed7c130>

Expected behavior

Should print same thing in both cases.

Environment

  • jsonargparse version: 4.28.0
  • Python version: 3.10.12
  • How jsonargparse was installed: pip install jsonargparse
  • OS (e.g., Linux): Ubuntu Jammy
@maticus maticus added the bug Something isn't working label May 17, 2024
@mauvilsa
Copy link
Member

Thank you for reporting! The same happens with normal parsers, so not really related to capture_parser.

@mauvilsa
Copy link
Member

Fixed in #502.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants