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

Optional Literal argument with None default does not generate tab completion #158

Closed
KolinGuo opened this issue Aug 27, 2024 · 6 comments
Closed

Comments

@KolinGuo
Copy link

It seemed like Optional[Literal] argument with None as its default value does not generate tab completion correctly.

This might be related to #156

See the minimal example below where the preset argument fails to generate choices for tab completion while the frame argument works.

#!/usr/bin/env python3
from typing import Annotated, Literal, Optional

import tyro


def start_device(
    preset: Annotated[
        Optional[Literal["rgb", "depth", "ir"]], tyro.conf.arg(aliases=["-p"])
    ] = None,
    frame: Annotated[Literal["world", "base"], tyro.conf.arg(aliases=["-f"])] = "world",
) -> None:
    """
    Start device with the given preset.

    :param preset: device preset to use.
    :param frame: coordinate frame to use.
    """
    print(f"{preset=} {frame=}")


if __name__ == "__main__":
    tyro.cli(start_device)

Below is the first few lines of the generated completion file:

# AUTOMATICALLY GENERATED by `shtab`

_shtab_tyro_test_py_option_strings=('-h' '--help' '--preset' '-p' '--frame' '-f')

_shtab_tyro_test_py___frame_choices=('world' 'base')
_shtab_tyro_test_py__f_choices=('world' 'base')

_shtab_tyro_test_py__h_nargs=0
_shtab_tyro_test_py___help_nargs=0

# $1=COMP_WORDS[1]
_shtab_compgen_files() {
  compgen -f -- $1  # files
}

# $1=COMP_WORDS[1]
_shtab_compgen_dirs() {
  compgen -d -- $1  # recurse into subdirs
}

# $1=COMP_WORDS[1]
_shtab_replace_nonword() {
  echo "${1//[^[:word:]]/_}"
}
brentyi added a commit that referenced this issue Aug 27, 2024
This should only impact completion script generation (#158)
Repository owner deleted a comment from amir1387aht Aug 27, 2024
Repository owner deleted a comment Aug 27, 2024
brentyi added a commit that referenced this issue Aug 27, 2024
This should only impact completion script generation (#158)
@brentyi
Copy link
Owner

brentyi commented Aug 27, 2024

Thanks @KolinGuo! I just pushed a change to main that should fix this. If you have a chance could you double-check?

@KolinGuo
Copy link
Author

KolinGuo commented Aug 27, 2024

Hi @brentyi, thanks for your update!
However, it seems to generate the same completion script when I run python3 test.py --tyro-write-completion bash /etc/bash_completion.d/test
Below is the first few lines of the generated completion script

# AUTOMATICALLY GENERATED by `shtab`
_shtab_tyro_test_py_option_strings=('-h' '--help' '--preset' '-p' '--frame' '-f')

_shtab_tyro_test_py___frame_choices=('world' 'base')
_shtab_tyro_test_py__f_choices=('world' 'base')

_shtab_tyro_test_py__h_nargs=0
_shtab_tyro_test_py___help_nargs=0

# $1=COMP_WORDS[1]
_shtab_compgen_files() {
  compgen -f -- $1  # files
}

# $1=COMP_WORDS[1]
_shtab_compgen_dirs() {
  compgen -d -- $1  # recurse into subdirs
}

# $1=COMP_WORDS[1]
_shtab_replace_nonword() {
  echo "${1//[^[:word:]]/_}"
}

Does it work on your end?

@brentyi
Copy link
Owner

brentyi commented Aug 27, 2024

Are you installing from source? I didn't do a release, sorry for not clarifying.

Here's a pip command:

pip install git+https://github.com/brentyi/tyro.git

The output I get from your command:

# AUTOMATICALLY GENERATED by `shtab`
_shtab_tyro_test_py_option_strings=('-h' '--help' '--preset' '-p' '--frame' '-f')

_shtab_tyro_test_py___preset_choices=('ir' 'rgb' 'None' 'depth')
_shtab_tyro_test_py__p_choices=('ir' 'rgb' 'None' 'depth')
_shtab_tyro_test_py___frame_choices=('world' 'base')
_shtab_tyro_test_py__f_choices=('world' 'base')

_shtab_tyro_test_py__h_nargs=0
_shtab_tyro_test_py___help_nargs=0


# $1=COMP_WORDS[1]
_shtab_compgen_files() {
  compgen -f -- $1  # files
}

# $1=COMP_WORDS[1]
_shtab_compgen_dirs() {
  compgen -d -- $1  # recurse into subdirs
}

@KolinGuo
Copy link
Author

KolinGuo commented Aug 27, 2024

Sorry my bad, the main branch worked. I wasn't installing your main branch before

@KolinGuo
Copy link
Author

Thank you very much! Do you plan to publish a quick release to PyPI?

@brentyi
Copy link
Owner

brentyi commented Aug 27, 2024

Done!

@brentyi brentyi closed this as completed Aug 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants
@brentyi @KolinGuo and others