Skip to content

mypy raises errors when using click.prompt() due to type argument having incompatible type #2062

@andrewlangemann

Description

@andrewlangemann

I have a project that uses the latest version of mypy (0.910) and click (8.0.1). I am attempting to use click.prompt() and pass in the type argument but this is always causing mypy (with default settings) to find errors.

Here is an example:

import click
name = click.prompt('What is your name?', type=str)
age = click.prompt('What is your age?', type=int)

When I run mypy against this file, I get two errors:

main.py:2: error: Argument "type" to "prompt" has incompatible type "Type[str]"; expected "Optional[ParamType]"
main.py:3: error: Argument "type" to "prompt" has incompatible type "Type[int]"; expected "Optional[ParamType]"

To address these, I tried passing in some different values for type but to no avail (same issues all happen for int as well):

name = click.prompt('What is your name?', type=Optional[str])
# Returns error: Argument "type" to "prompt" has incompatible type "object"; expected "Optional[ParamType]"

name = click.prompt('What is your name?', type=StringParamType)
# Returns error: Argument "type" to "prompt" has incompatible type "Type[StringParamType]"; expected "Optional[ParamType]"

name = click.prompt('What is your name?', type=Optional[StringParamType])
# Returns error: Argument "type" to "prompt" has incompatible type "object"; expected "Optional[ParamType]"

Is this a bug or should I be providing something different to type?

Environment:

  • Python version: 3.7.10
  • Click version: 8.0.1
  • mypy version: 0.910

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions