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

Is optional arg e.g. int | None supported somehow? #45

Closed
NikolayXHD opened this issue Jan 3, 2024 · 3 comments
Closed

Is optional arg e.g. int | None supported somehow? #45

NikolayXHD opened this issue Jan 3, 2024 · 3 comments

Comments

@NikolayXHD
Copy link
Contributor

Hi @sander76, I'am trying clipstick for the first time. Love the feeling, elegant & minimal. I take my 🎩 off!

The issue I am facing, I don't recall seeing in the docs, how do I handle the optional argument if the default value is None.

With good old argparse I'd write

parser = argparse.ArgumentParser()
parser.add_argument('--effort', '-e', type=int, required=False)
args = parser.parse_args()

effective type of args.effort would be int | None

With clipstick I do

# test.py
from clipstick import parse
from pydantic import BaseModel


class Arguments(BaseModel):
    effort: int | None = None


def main() -> None:
    print(parse(Arguments))


if __name__ == '__main__':
    main()
python -m test -h

Parsing fails with following output

Usage: test.py [Options]

Options:
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/home/kolia/git/sandbox/src/test.py", line 14, in <module>
    main()
  File "/home/kolia/git/sandbox/src/test.py", line 10, in main
    print(parse(Arguments))
          ^^^^^^^^^^^^^^^^
  File "/home/kolia/.cache/pypoetry/virtualenvs/sandbox-y84fort7-py3.11/lib/python3.11/site-packages/clipstick/_clipstick.py", line 45, in parse
    success, idx = root_node.match(0, args)
                   ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/kolia/.cache/pypoetry/virtualenvs/sandbox-y84fort7-py3.11/lib/python3.11/site-packages/clipstick/_tokens.py", line 501, in match
    _help.help(self)
  File "/home/kolia/.cache/pypoetry/virtualenvs/sandbox-y84fort7-py3.11/lib/python3.11/site-packages/clipstick/_help.py", line 99, in help
    tbl.add_row("", *_help_from_token(kwarg.help()))
                                      ^^^^^^^^^^^^
  File "/home/kolia/.cache/pypoetry/virtualenvs/sandbox-y84fort7-py3.11/lib/python3.11/site-packages/clipstick/_tokens.py", line 182, in help
    "type": self.field_info.annotation.__name__
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'types.UnionType' object has no attribute '__name__'. Did you mean: '__ne__'?
@NikolayXHD
Copy link
Contributor Author

Btw the problem only shows up while building help, actually running the program works as intended:

master[1] ~/g/sandbox> python -m test --effort 1
effort=1
master ~/g/sandbox> python -m test
effort=None

@NikolayXHD NikolayXHD mentioned this issue Jan 3, 2024
@sander76
Copy link
Owner

sander76 commented Jan 4, 2024

Thanks for this! (And the PR). I'll pick it up today

@sander76
Copy link
Owner

sander76 commented Jan 4, 2024

Fixed in https://github.com/sander76/clipstick/releases/tag/v0.4.2

@sander76 sander76 closed this as completed Jan 4, 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

2 participants