mypy icon indicating copy to clipboard operation
mypy copied to clipboard

Improve Error message when type narrowing occured e.g Literal[True ] vs bool type.

Open mmarras opened this issue 3 years ago • 0 comments

Feature

Improve wording for "incompatible" types based on type narrowing (for boolean types). It should be suggested that the type is generically correct, but can be stated more strictly, otherwise it may be confusing, as the Literal value is a subset of the super-type which has been declared.

Pitch

For example, when type narrowing occurs for boolean values which in the program logic always equate to only one case, e.g. True, mypy narrows the type from bool to Literal[True]. If this variable is returned (maybe as part of a dictionary to make actual sense) and the output type is set to bool, which is correct just not "narrow" enough, the error message reads:

Incompatible return value type (got "Tuple[Dict[str, Literal[True]]", expected "Tuple[Dict[str, bool]") [return-value]

The word "incompatible" is confusing semantically, imho.

mmarras avatar Jan 17 '23 10:01 mmarras