Skip to content

Click option value type incorrect if multiple=True. #2615

@adamtheturtle

Description

@adamtheturtle

Install dependencies:

$ pip install click mypy
$ mypy --version
mypy 0.641
$

Create example.py:

from typing import Sequence, Union

import click

def my_callback(
    ctx: click.core.Context,
    param: Union[click.core.Option, click.core.Parameter],
    value: Sequence[Union[int, bool, str]],
):
    print('Value is: ' + str(value))

@click.command('example')
@click.option('-x', multiple=True, callback=my_callback)
def example(x):
    pass

if __name__ == '__main__':
    example()

Run this to see that value is a tuple:

$ python example.py
Value is: ()
$

Run mypy which incorrectly tells us that value must be Union[bool, int, str]:

$ mypy example.py
example.py:13: error: Argument "callback" to "option" has incompatible type "Callable[[Context, Union[Option, Parameter], Sequence[Union[int, bool, str]]], Any]"; expected "Optional[Callable[[Context, Union[Option, Parameter], Union[bool, int, str]], Any]]"

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions