tmbo / questionary

Python library to build pretty command line user prompts ✨Easy to use multi-select lists, confirmations, free text prompts ...
MIT License
1.57k stars 91 forks source link

Adopting a type checker #61

Closed layday closed 4 years ago

layday commented 4 years ago

I wonder if there would be any interest in adopting a type checker. Questionary appears to be extensively annotated so this shouldn't prove too onerous. The four established type checkers are mypy, Pyright, pytype and pyre - would you have a preference for one of those?

kiancross commented 4 years ago

I think this is a good idea. I've used mypy before and it worked well.

layday commented 4 years ago

The latest version of Pyright is able to assess a library's type completeness. That is, it identifies public attributes which are not typed or only partially typed and calculates a 'completeness score'. You can read more about this feature here. I ran this on the Questionary codebase and it gave it a completeness score of 20.5%. However, this is slightly deceptive because it deducts every occurrence of an untyped attribute from the score, e.g. Choice is counted repeatedly because it has five public attributes which are not explicitly typed: disabled, title, checked, value and shortcut_key. Please see the output of pyright --verifytypes questionary below. (This is not the same thing as type checking Questionary itself but I thought it might be of interest.)

Output ``` Package name: "questionary" Package directory: "questionary" Path of py.typed file: "questionary/py.typed" error: Type not declared for constant "questionary.constants.DEFAULT_STYLE" error: Type partially unknown for type alias "questionary.form.FormField"   Type partially unknown for class "FormField"     Type unknown for variable "key"     Type unknown for variable "question"     Type partially unknown for function "__new__"       Parameter "key" is missing a type annotation       Parameter "question" is missing a type annotation error: Type partially unknown for function "questionary.form.form"   Type partially unknown for parameter "kwargs"     Type partially unknown for class "Question"       Type partially unknown for method "__init__"         Type unknown for parameter "application"       Type unknown for variable "application"       Type unknown for variable "should_skip_question"       Type unknown for variable "default"   Return type annotation is missing error: Type partially unknown for class "questionary.form.Form"   Type partially unknown for class "Form"     Type partially unknown for method "__init__"       Type partially unknown for parameter "form_fields"         Type partially unknown for class "FormField"           Type unknown for variable "key"           Type unknown for variable "question"           Type partially unknown for function "__new__"             Parameter "key" is missing a type annotation             Parameter "question" is missing a type annotation     Type partially unknown for method "unsafe_ask"       Parameter "patch_stdout" is missing a type annotation       Return type annotation is missing     Type partially unknown for method "unsafe_ask_async"       Parameter "patch_stdout" is missing a type annotation       Return type annotation is missing     Type partially unknown for method "ask"       Parameter "patch_stdout" is missing a type annotation       Parameter "kbi_msg" is missing a type annotation       Return type annotation is missing     Type partially unknown for method "ask_async"       Parameter "patch_stdout" is missing a type annotation       Parameter "kbi_msg" is missing a type annotation       Return type annotation is missing     Type unknown for variable "form_fields" error: Type partially unknown for method "questionary.form.Form.__init__"   Type partially unknown for parameter "form_fields"     Type partially unknown for class "FormField"       Type unknown for variable "key"       Type unknown for variable "question"       Type partially unknown for function "__new__"         Parameter "key" is missing a type annotation         Parameter "question" is missing a type annotation error: Type partially unknown for method "questionary.form.Form.unsafe_ask"   Parameter "patch_stdout" is missing a type annotation   Return type annotation is missing error: Type partially unknown for method "questionary.form.Form.unsafe_ask_async"   Parameter "patch_stdout" is missing a type annotation   Return type annotation is missing error: Type partially unknown for method "questionary.form.Form.ask"   Parameter "patch_stdout" is missing a type annotation   Parameter "kbi_msg" is missing a type annotation   Return type annotation is missing error: Type partially unknown for method "questionary.form.Form.ask_async"   Parameter "patch_stdout" is missing a type annotation   Parameter "kbi_msg" is missing a type annotation   Return type annotation is missing error: Type not declared for variable "questionary.form.Form.form_fields" error: Type partially unknown for class "questionary.prompt.PromptParameterException"   Type partially unknown for class "PromptParameterException"     Type partially unknown for method "__init__"       Parameter "errors" is missing a type annotation error: Type partially unknown for method "questionary.prompt.PromptParameterException.__init__"   Parameter "errors" is missing a type annotation error: Type partially unknown for function "questionary.prompt.prompt"   Parameter "kwargs" is missing a type annotation   Return type annotation is missing error: Type partially unknown for constant "questionary.prompts.AVAILABLE_PROMPTS"   Type partially unknown for class "Dict[str, Unknown]"     Type unknown for type argument 2 error: Type partially unknown for function "questionary.prompts.prompt_by_name"   Parameter "name" is missing a type annotation   Return type annotation is missing error: Type partially unknown for class "questionary.prompts.autocomplete.WordCompleter"   Type partially unknown for class "WordCompleter"     Type for metaclass is unknown       Type partially unknown for method "get_completions"         Type unknown for parameter "document"         Type unknown for parameter "complete_event"         Return type partially unknown           Type partially unknown for class "Iterable[Unknown]"             Type unknown for type argument 1 which corresponds to TypeVar _T_co       Type unknown for variable "choices_source"       Type unknown for variable "ignore_case"       Type unknown for variable "meta_information"       Type unknown for variable "match_middle"     Type partially unknown for method "get_completions"       Type unknown for parameter "document"       Type unknown for parameter "complete_event"       Return type partially unknown         Type partially unknown for class "Iterable[Unknown]"           Type unknown for type argument 1 which corresponds to TypeVar _T_co     Type unknown for variable "choices_source"     Type unknown for variable "ignore_case"     Type unknown for variable "meta_information"     Type unknown for variable "match_middle" error: Type partially unknown for method "questionary.prompts.autocomplete.WordCompleter.get_completions"   Type unknown for parameter "document"   Type unknown for parameter "complete_event"   Return type partially unknown     Type partially unknown for class "Iterable[Unknown]"       Type unknown for type argument 1 which corresponds to TypeVar _T_co error: Type not declared for variable "questionary.prompts.autocomplete.WordCompleter.choices_source" error: Type not declared for variable "questionary.prompts.autocomplete.WordCompleter.ignore_case" error: Type not declared for variable "questionary.prompts.autocomplete.WordCompleter.meta_information" error: Type not declared for variable "questionary.prompts.autocomplete.WordCompleter.match_middle" error: Type partially unknown for function "questionary.prompts.autocomplete.autocomplete"   Type partially unknown for parameter "completer"   Type unknown for parameter "complete_style"   Type partially unknown for parameter "style"   Return type partially unknown     Type partially unknown for class "Question"       Type partially unknown for method "__init__"         Type unknown for parameter "application"       Type unknown for variable "application"       Type unknown for variable "should_skip_question"       Type unknown for variable "default" error: Type partially unknown for function "questionary.prompts.checkbox.checkbox"   Type partially unknown for parameter "choices"     Type partially unknown for class "List[str | Choice | Dict[Text, Any]]"       Type partially unknown for type argument 1         Type partially unknown for class "Choice"           Type unknown for variable "disabled"           Type unknown for variable "title"           Type unknown for variable "checked"           Type unknown for variable "value"           Type unknown for variable "shortcut_key"   Type partially unknown for parameter "style"   Return type partially unknown     Type partially unknown for class "Question"       Type partially unknown for method "__init__"         Type unknown for parameter "application"       Type unknown for variable "application"       Type unknown for variable "should_skip_question"       Type unknown for variable "default" error: Type partially unknown for class "questionary.prompts.common.Choice"   Type partially unknown for class "Choice"     Type unknown for variable "disabled"     Type unknown for variable "title"     Type unknown for variable "checked"     Type unknown for variable "value"     Type unknown for variable "shortcut_key" error: Type partially unknown for method "questionary.prompts.common.Choice.build"   Type partially unknown for parameter "c"     Type partially unknown for class "Choice"       Type unknown for variable "disabled"       Type unknown for variable "title"       Type unknown for variable "checked"       Type unknown for variable "value"       Type unknown for variable "shortcut_key"   Return type partially unknown     Type partially unknown for class "Choice"       Type unknown for variable "disabled"       Type unknown for variable "title"       Type unknown for variable "checked"       Type unknown for variable "value"       Type unknown for variable "shortcut_key" error: Type not declared for variable "questionary.prompts.common.Choice.disabled" error: Type not declared for variable "questionary.prompts.common.Choice.title" error: Type not declared for variable "questionary.prompts.common.Choice.checked" error: Type not declared for variable "questionary.prompts.common.Choice.value" error: Type not declared for variable "questionary.prompts.common.Choice.shortcut_key" error: Type partially unknown for class "questionary.prompts.common.Separator"   Type partially unknown for class "Separator"     Type unknown for variable "default_separator"     Type unknown for variable "line"     Type partially unknown for symbol "disabled" defined in base class "Choice"       Type unknown for variable "disabled"     Type partially unknown for symbol "title" defined in base class "Choice"       Type unknown for variable "title"     Type partially unknown for symbol "checked" defined in base class "Choice"       Type unknown for variable "checked"     Type partially unknown for symbol "value" defined in base class "Choice"       Type unknown for variable "value"     Type partially unknown for symbol "shortcut_key" defined in base class "Choice"       Type unknown for variable "shortcut_key" error: Type not declared for variable "questionary.prompts.common.Separator.default_separator" error: Type not declared for variable "questionary.prompts.common.Separator.line" error: Type partially unknown for class "questionary.prompts.common.InquirerControl"   Type partially unknown for class "InquirerControl"     Type for metaclass is unknown       Type partially unknown for method "__init__"         Type partially unknown for parameter "choices"           Type partially unknown for class "List[str | Choice | Dict[Text, Any]]"             Type partially unknown for type argument 1               Type partially unknown for class "Choice"                 Type unknown for variable "disabled"                 Type unknown for variable "title"                 Type unknown for variable "checked"                 Type unknown for variable "value"                 Type unknown for variable "shortcut_key"         Parameter "kwargs" is missing a type annotation       Type partially unknown for variable "choice_count"         Type partially unknown for class "property"           Type partially unknown for function "fget"             Return type annotation is missing           Type partially unknown for function "__get__"             Return type annotation is missing       Type partially unknown for method "is_selection_a_separator"         Return type annotation is missing       Type partially unknown for method "is_selection_disabled"         Return type annotation is missing       Type partially unknown for method "is_selection_valid"         Return type annotation is missing       Type partially unknown for method "select_previous"         Return type annotation is missing       Type partially unknown for method "select_next"         Return type annotation is missing       Type partially unknown for method "get_pointed_at"         Return type annotation is missing       Type partially unknown for method "get_selected_values" ... error: Type partially unknown for method "questionary.prompts.common.InquirerControl.__init__"   Type partially unknown for parameter "choices"     Type partially unknown for class "List[str | Choice | Dict[Text, Any]]"       Type partially unknown for type argument 1         Type partially unknown for class "Choice"           Type unknown for variable "disabled"           Type unknown for variable "title"           Type unknown for variable "checked"           Type unknown for variable "value"           Type unknown for variable "shortcut_key"   Parameter "kwargs" is missing a type annotation error: Type partially unknown for variable "questionary.prompts.common.InquirerControl.choice_count"   Type partially unknown for class "property"     Type partially unknown for function "fget"       Return type annotation is missing     Type partially unknown for function "__get__"       Return type annotation is missing error: Type partially unknown for method "questionary.prompts.common.InquirerControl.is_selection_a_separator"   Return type annotation is missing error: Type partially unknown for method "questionary.prompts.common.InquirerControl.is_selection_disabled"   Return type annotation is missing error: Type partially unknown for method "questionary.prompts.common.InquirerControl.is_selection_valid"   Return type annotation is missing error: Type partially unknown for method "questionary.prompts.common.InquirerControl.select_previous"   Return type annotation is missing error: Type partially unknown for method "questionary.prompts.common.InquirerControl.select_next"   Return type annotation is missing error: Type partially unknown for method "questionary.prompts.common.InquirerControl.get_pointed_at"   Return type annotation is missing error: Type partially unknown for method "questionary.prompts.common.InquirerControl.get_selected_values"   Return type annotation is missing error: Type not declared for variable "questionary.prompts.common.InquirerControl.use_indicator" error: Type not declared for variable "questionary.prompts.common.InquirerControl.use_shortcuts" error: Type not declared for variable "questionary.prompts.common.InquirerControl.use_pointer" error: Type not declared for variable "questionary.prompts.common.InquirerControl.default" error: Type not declared for variable "questionary.prompts.common.InquirerControl.pointed_at" error: Type not declared for variable "questionary.prompts.common.InquirerControl.is_answered" error: Type not declared for variable "questionary.prompts.common.InquirerControl.choices" error: Type not declared for variable "questionary.prompts.common.InquirerControl.selected_options" error: Type partially unknown for function "questionary.prompts.common.build_validator"   Return type partially unknown error: Type partially unknown for function "questionary.prompts.common.create_inquirer_layout"   Type partially unknown for parameter "ic"     Type partially unknown for class "InquirerControl"       Type for metaclass is unknown         Type partially unknown for method "__init__"           Type partially unknown for parameter "choices"             Type partially unknown for class "List[str | Choice | Dict[Text, Any]]"               Type partially unknown for type argument 1                 Type partially unknown for class "Choice"                   Type unknown for variable "disabled"                   Type unknown for variable "title"                   Type unknown for variable "checked"                   Type unknown for variable "value"                   Type unknown for variable "shortcut_key"           Parameter "kwargs" is missing a type annotation         Type partially unknown for variable "choice_count"           Type partially unknown for class "property"             Type partially unknown for function "fget"               Return type annotation is missing             Type partially unknown for function "__get__"               Return type annotation is missing         Type partially unknown for method "is_selection_a_separator"           Return type annotation is missing         Type partially unknown for method "is_selection_disabled"           Return type annotation is missing         Type partially unknown for method "is_selection_valid"           Return type annotation is missing         Type partially unknown for method "select_previous"           Return type annotation is missing         Type partially unknown for method "select_next"           Return type annotation is missing         Type partially unknown for method "get_pointed_at"           Return type annotation is missing ... error: Type partially unknown for function "questionary.prompts.confirm.confirm"   Type partially unknown for parameter "style"   Return type partially unknown     Type partially unknown for class "Question"       Type partially unknown for method "__init__"         Type unknown for parameter "application"       Type unknown for variable "application"       Type unknown for variable "should_skip_question"       Type unknown for variable "default" error: Type partially unknown for function "questionary.prompts.password.password"   Type partially unknown for parameter "style"   Return type partially unknown     Type partially unknown for class "Question"       Type partially unknown for method "__init__"         Type unknown for parameter "application"       Type unknown for variable "application"       Type unknown for variable "should_skip_question"       Type unknown for variable "default" error: Type partially unknown for function "questionary.prompts.rawselect.rawselect"   Type partially unknown for parameter "choices"     Type partially unknown for class "List[str | Choice | Dict[Text, Any]]"       Type partially unknown for type argument 1         Type partially unknown for class "Choice"           Type unknown for variable "disabled"           Type unknown for variable "title"           Type unknown for variable "checked"           Type unknown for variable "value"           Type unknown for variable "shortcut_key"   Type partially unknown for parameter "style"   Return type partially unknown     Type partially unknown for class "Question"       Type partially unknown for method "__init__"         Type unknown for parameter "application"       Type unknown for variable "application"       Type unknown for variable "should_skip_question"       Type unknown for variable "default" error: Type partially unknown for function "questionary.prompts.select.select"   Type partially unknown for parameter "choices"     Type partially unknown for class "List[str | Choice | Dict[Text, Any]]"       Type partially unknown for type argument 1         Type partially unknown for class "Choice"           Type unknown for variable "disabled"           Type unknown for variable "title"           Type unknown for variable "checked"           Type unknown for variable "value"           Type unknown for variable "shortcut_key"   Type partially unknown for parameter "style"   Return type partially unknown     Type partially unknown for class "Question"       Type partially unknown for method "__init__"         Type unknown for parameter "application"       Type unknown for variable "application"       Type unknown for variable "should_skip_question"       Type unknown for variable "default" error: Type partially unknown for function "questionary.prompts.text.text"   Type partially unknown for parameter "style"   Return type partially unknown     Type partially unknown for class "Question"       Type partially unknown for method "__init__"         Type unknown for parameter "application"       Type unknown for variable "application"       Type unknown for variable "should_skip_question"       Type unknown for variable "default" error: Type partially unknown for class "questionary.question.Question"   Type partially unknown for class "Question"     Type partially unknown for method "__init__"       Type unknown for parameter "application"     Type unknown for variable "application"     Type unknown for variable "should_skip_question"     Type unknown for variable "default" error: Type partially unknown for method "questionary.question.Question.__init__"   Type unknown for parameter "application" error: Type partially unknown for method "questionary.question.Question.skip_if"   Return type partially unknown     Type partially unknown for class "Question"       Type partially unknown for method "__init__"         Type unknown for parameter "application"       Type unknown for variable "application"       Type unknown for variable "should_skip_question"       Type unknown for variable "default" error: Type not declared for variable "questionary.question.Question.application" error: Type not declared for variable "questionary.question.Question.should_skip_question" error: Type not declared for variable "questionary.question.Question.default" error: Type partially unknown for function "questionary.utils.is_prompt_toolkit_3"   Return type annotation is missing error: Type partially unknown for function "questionary.utils.default_values_of"   Parameter "func" is missing a type annotation   Return type annotation is missing error: Type partially unknown for function "questionary.utils.arguments_of"   Parameter "func" is missing a type annotation   Return type annotation is missing error: Type partially unknown for function "questionary.utils.required_arguments"   Parameter "func" is missing a type annotation   Return type annotation is missing error: Type partially unknown for function "questionary.utils.missing_arguments"   Parameter "func" is missing a type annotation   Parameter "argdict" is missing a type annotation   Return type annotation is missing error: Type partially unknown for function "questionary.utils.activate_prompt_toolkit_async_mode"   Return type annotation is missing warning: No docstring found for function "questionary.form.Form.unsafe_ask" warning: No docstring found for function "questionary.form.Form.unsafe_ask_async" warning: No docstring found for function "questionary.form.Form.ask" warning: No docstring found for function "questionary.form.Form.ask_async" warning: No docstring found for class "questionary.prompt.PromptParameterException" warning: No docstring found for function "questionary.prompts.prompt_by_name" warning: No docstring found for class "questionary.prompts.autocomplete.WordCompleter" warning: No docstring found for function "questionary.prompts.autocomplete.WordCompleter.get_completions" warning: No docstring found for class "questionary.prompts.common.InquirerControl" warning: No docstring found for function "questionary.prompts.common.InquirerControl.is_selection_a_separator" warning: No docstring found for function "questionary.prompts.common.InquirerControl.is_selection_disabled" warning: No docstring found for function "questionary.prompts.common.InquirerControl.is_selection_valid" warning: No docstring found for function "questionary.prompts.common.InquirerControl.select_previous" warning: No docstring found for function "questionary.prompts.common.InquirerControl.select_next" warning: No docstring found for function "questionary.prompts.common.InquirerControl.get_pointed_at" warning: No docstring found for function "questionary.prompts.common.InquirerControl.get_selected_values" warning: No docstring found for function "questionary.prompts.common.build_validator" warning: No docstring found for function "questionary.utils.is_prompt_toolkit_3" Public modules: 16 questionary (0 symbols) questionary.constants (10 symbols) questionary.form (9 symbols) questionary.prompt (3 symbols) questionary.prompts (2 symbols) questionary.prompts.autocomplete (8 symbols) questionary.prompts.checkbox (1 symbol) questionary.prompts.common (33 symbols) questionary.prompts.confirm (1 symbol) questionary.prompts.password (1 symbol) questionary.prompts.rawselect (1 symbol) questionary.prompts.select (1 symbol) questionary.prompts.text (1 symbol) questionary.question (10 symbols) questionary.utils (7 symbols) questionary.version (0 symbols) Public symbols: 88 Symbols with unknown type: 70 Functions with missing docstring: 15 Functions with missing default param: 0 Classes with missing docstring: 3 Type completeness score: 20.5% ```