typer icon indicating copy to clipboard operation
typer copied to clipboard

[BUG] Required typer.Option for Tuple raises TypeError: It would appear that nargs is set to conflict with the composite type arity

Open gadostal-swordfish opened this issue 5 years ago • 3 comments

Describe the bug

For an option that is of Tuple type and set to required with ..., a TypeError occurs when no value passed in the command line.

To Reproduce

Steps to reproduce the behavior with a minimum self-contained file.

Replace each part with your own scenario:

  • Create a file main.py with:
from typing import Optional, Tuple

import typer

app = typer.Typer()


@app.command()
def run(tuple_option: Tuple[float, float, float, float] = typer.Option(...)) -> None:
    print(tuple_option)

if __name__ == "__main__":
    app()
  • Call it with:
python main.py
  • It outputs:
Traceback (most recent call last):
  File "main.py", line 15, in <module>
    app()
  File "venv/lib/python3.8/site-packages/typer/main.py", line 214, in __call__
    return get_command(self)(*args, **kwargs)
  File "venv/lib/python3.8/site-packages/click/core.py", line 829, in __call__
    return self.main(*args, **kwargs)
  File "venv/lib/python3.8/site-packages/click/core.py", line 781, in main
    with self.make_context(prog_name, args, **extra) as ctx:
  File "venv/lib/python3.8/site-packages/click/core.py", line 700, in make_context
    self.parse_args(ctx, args)
  File "venv/lib/python3.8/site-packages/click/core.py", line 1048, in parse_args
    value, args = param.handle_parse_result(ctx, opts, args)
  File "venv/lib/python3.8/site-packages/click/core.py", line 1623, in handle_parse_result
    value = self.full_process_value(ctx, value)
  File "venv/lib/python3.8/site-packages/click/core.py", line 1965, in full_process_value
    return Parameter.full_process_value(self, ctx, value)
  File "venv/lib/python3.8/site-packages/click/core.py", line 1592, in full_process_value
    value = self.get_default(ctx)
  File "venv/lib/python3.8/site-packages/click/core.py", line 1917, in get_default
    return Parameter.get_default(self, ctx)
  File "venv/lib/python3.8/site-packages/click/core.py", line 1534, in get_default
    return self.type_cast_value(ctx, rv)
  File "venv/lib/python3.8/site-packages/click/core.py", line 1561, in type_cast_value
    return self.type(value or (), self, ctx)
  File "venv/lib/python3.8/site-packages/click/types.py", line 46, in __call__
    return self.convert(value, param, ctx)
  File "venv/lib/python3.8/site-packages/click/types.py", line 681, in convert
    raise TypeError(
TypeError: It would appear that nargs is set to conflict with the composite type arity.
  • But I expected it to output:
Usage: typer_bug.py [OPTIONS]
Try 'typer_bug.py --help' for help.

Error: Missing option '--tuple-option'.

Expected behavior

Act like other required options.

Environment

  • OS: Linux Ubuntu 20.04
  • Typer Version 0.3.2
  • Python 3.8.5

Additional context

Same exception with this setting:

@app.command()
def run(tuple_option: Tuple[float, float, float, float] = typer.Option(None)) -> None:
   pass

No exception with this:

@app.command()
def run(tuple_option: Tuple[float, float, float, float] = typer.Option((None, None, None, None))) -> None:
   pass

gadostal-swordfish avatar Nov 19 '20 10:11 gadostal-swordfish

I think this issue is due to Click -> https://github.com/pallets/click/issues/789

codethief avatar Mar 04 '21 17:03 codethief

https://github.com/pallets/click/issues/789 has now been fixed!

codethief avatar Apr 16 '21 17:04 codethief

~~But I still get the problem...~~

from typing import Tuple
import typer

app = typer.Typer()


@app.command()
def compute_windows(w1_range: Tuple[int, int, int] = typer.Option(...)):
    print(w1_range)


if __name__ == "__main__":
    app()

~~typer version is 0.3.2~~ ~~click version 7.1.2~~

Updating Typer to 0.4.0 solved the problem. Due to #377 I used the follwoing:

typer<1.0
click==8.0.4

in my requirements.txt.

drorata avatar Mar 29 '22 13:03 drorata

This has been resolved since Typer v0.4.0, so I'll go ahead and close this issue.

svlandeg avatar Mar 22 '24 14:03 svlandeg