Skip to content

Shtab complete: the action.complete of Classes, methods and functions should be shtab.FILE #556

@tshu-w

Description

@tshu-w

🐛 Bug report

Currently, the action.complete for shtab is FILE only when it is ActionConfigFile or Pathlike for ActionTypeHint, however, these doesn't cover the action of _ActionConfigLoad.

complete = None
if isinstance(action, ActionConfigFile):
complete = shtab.FILE
elif isinstance(action, ActionTypeHint):
typehint = action._typehint
if get_typehint_origin(typehint) == Union:
subtypes = [s for s in typehint.__args__ if s not in {NoneType, str, dict, list, tuple, bytes}]
if len(subtypes) == 1:
typehint = subtypes[0]
if is_subclass(typehint, Path):
if "f" in typehint._mode:
complete = shtab.FILE
elif "d" in typehint._mode:
complete = shtab.DIRECTORY
elif is_subclass(typehint, os.PathLike):
complete = shtab.FILE
if complete:
action.complete = complete
return

To reproduce

Simple Example

# cli.py
from jsonargparse import ArgumentParser
from calendar import Calendar

class MyClass:
     def __init__(self, calendar: Calendar):
         self.calendar = calendar

parser = ArgumentParser()
parser.add_class_arguments(MyClass, "myclass", sub_configs=True)
cfg = parser.parse_args()
λ python cli.py --print_shtab=zsh
...
"--myclass[Path to a configuration file.]:myclass:"
...

Expected:

λ python cli.py --print_shtab=zsh
...
"--myclass[Path to a configuration file.]:myclass:_files"
...

since the following command work:

python cli.py --myclass test.yaml

with test.yaml:

calendar:
  class_path: calendar.Calendar
  init_args:
    firstweekday: 1

Environment

  • jsonargparse version: 4.32.0
  • Python version: 3.12
  • How jsonargparse was installed: pip
  • OS: Ubuntu 22.04

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions