Skip to content

Commit 40aea35

Browse files
committed
proper error message
1 parent 6c8b65e commit 40aea35

File tree

4 files changed

+32
-17
lines changed

4 files changed

+32
-17
lines changed

docs/source/error_code_list2.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ values should indeed behave like `None`.
392392
# Use "mypy --enable-error-code optional-non-truthy ..."
393393
394394
foo: None | int = 0
395-
# Error: FIXME
395+
# Error: "foo" has type "Optional[int]" where both None and some other values (of type: "int") may behave as false in boolean contexts. Consider being explicit about the behaviour for None vs other falsy values. [optional-non-truthy]
396396
if foo:
397397
... # executes when foo is an int != 0
398398
else:

mypy/checker.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5746,7 +5746,15 @@ def check_for_optional_non_truthy_type(self, t: Type, expr: Expression) -> None:
57465746
if not non_truthy:
57475747
return # all the other types are just 'normal' types, not collections or ints, etc.
57485748

5749-
self.fail(message_registry.OPTIONAL_WITH_NON_TRUTHY.format("x"), expr)
5749+
non_truthy_formatted = ", ".join(format_type(t, self.options) for t in non_truthy)
5750+
self.fail(
5751+
message_registry.OPTIONAL_WITH_NON_TRUTHY.format(
5752+
self._format_expr_type(t, expr),
5753+
"type" if len(non_truthy) == 1 else "types",
5754+
non_truthy_formatted,
5755+
),
5756+
expr,
5757+
)
57505758

57515759
def find_type_equals_check(
57525760
self, node: ComparisonExpr, expr_indices: list[int]

mypy/message_registry.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,10 @@ def with_additional_msg(self, info: str) -> ErrorMessage:
167167
"{} which can always be true in boolean context. Consider using {} instead.",
168168
code=codes.TRUTHY_ITERABLE,
169169
)
170-
OPTIONAL_WITH_NON_TRUTHY: Final = ErrorMessage("FIXME {}", code=codes.OPTIONAL_NON_TRUTHY)
170+
OPTIONAL_WITH_NON_TRUTHY: Final = ErrorMessage(
171+
"{} where both None and some other values (of {}: {}) may behave as false in boolean contexts. Consider being explicit about the behaviour for None vs other falsy values.",
172+
code=codes.OPTIONAL_NON_TRUTHY,
173+
)
171174
NOT_CALLABLE: Final = "{} not callable"
172175
TYPE_MUST_BE_USED: Final = "Value of type {} must be used"
173176

test-data/unit/check-errorcodes.test

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -969,50 +969,54 @@ def no_error_typevar(var: None | OnlyTruthy) -> None:
969969
not var
970970

971971
def error_int(var: None | int) -> None:
972-
if var: # E: FIXME x [optional-non-truthy]
972+
if var: # E: "var" has type "Optional[int]" where both None and some other values (of type: "int") may behave as false in boolean contexts. Consider being explicit about the behaviour for None vs other falsy values. [optional-non-truthy]
973973
...
974974

975-
not var # E: FIXME x [optional-non-truthy]
975+
not var # E: "var" has type "Optional[int]" where both None and some other values (of type: "int") may behave as false in boolean contexts. Consider being explicit about the behaviour for None vs other falsy values. [optional-non-truthy]
976976

977977
def error_bool(var: None | bool) -> None:
978-
if var: # E: FIXME x [optional-non-truthy]
978+
if var: # E: "var" has type "Optional[bool]" where both None and some other values (of type: "bool") may behave as false in boolean contexts. Consider being explicit about the behaviour for None vs other falsy values. [optional-non-truthy]
979979
...
980980

981-
not var # E: FIXME x [optional-non-truthy]
981+
not var # E: "var" has type "Optional[bool]" where both None and some other values (of type: "bool") may behave as false in boolean contexts. Consider being explicit about the behaviour for None vs other falsy values. [optional-non-truthy]
982982

983983
def error_list(var: None | list[Truthy]) -> None:
984-
if var: # E: FIXME x [optional-non-truthy]
984+
if var: # E: "var" has type "Optional[List[Truthy]]" where both None and some other values (of type: "List[Truthy]") may behave as false in boolean contexts. Consider being explicit about the behaviour for None vs other falsy values. [optional-non-truthy]
985985
...
986986

987-
not var # E: FIXME x [optional-non-truthy]
987+
not var # E: "var" has type "Optional[List[Truthy]]" where both None and some other values (of type: "List[Truthy]") may behave as false in boolean contexts. Consider being explicit about the behaviour for None vs other falsy values. [optional-non-truthy]
988988

989989
def error_user_defined(var: None | NonTruthy) -> None:
990-
if var: # E: FIXME x [optional-non-truthy]
990+
if var: # E: "var" has type "Optional[NonTruthy]" where both None and some other values (of type: "NonTruthy") may behave as false in boolean contexts. Consider being explicit about the behaviour for None vs other falsy values. [optional-non-truthy]
991991
...
992992

993-
not var # E: FIXME x [optional-non-truthy]
993+
not var # E: "var" has type "Optional[NonTruthy]" where both None and some other values (of type: "NonTruthy") may behave as false in boolean contexts. Consider being explicit about the behaviour for None vs other falsy values. [optional-non-truthy]
994994

995995
def error_union(var: None | int | Truthy | NonTruthy) -> None:
996-
if var: # E: FIXME x [optional-non-truthy]
996+
if var: # E: "var" has type "Union[int, Truthy, NonTruthy, None]" where both None and some other values (of types: "int", "NonTruthy") may behave as false in boolean contexts. Consider being explicit about the behaviour for None vs other falsy values. [optional-non-truthy]
997997
...
998998

999-
not var # E: FIXME x [optional-non-truthy]
999+
not var # E: "var" has type "Union[int, Truthy, NonTruthy, None]" where both None and some other values (of types: "int", "NonTruthy") may behave as false in boolean contexts. Consider being explicit about the behaviour for None vs other falsy values. [optional-non-truthy]
10001000

10011001
Unconstrained = TypeVar("Unconstrained")
10021002

10031003
def error_unconstrained_var(var: None | Unconstrained) -> None:
1004-
if var: # E: FIXME x [optional-non-truthy]
1004+
if var: # E: "var" has type "Optional[Unconstrained]" where both None and some other values (of type: "Unconstrained") may behave as false in boolean contexts. Consider being explicit about the behaviour for None vs other falsy values. [optional-non-truthy]
10051005
...
10061006

1007-
not var # E: FIXME x [optional-non-truthy]
1007+
not var # E: "var" has type "Optional[Unconstrained]" where both None and some other values (of type: "Unconstrained") may behave as false in boolean contexts. Consider being explicit about the behaviour for None vs other falsy values. [optional-non-truthy]
10081008

10091009
Constrained = TypeVar("Constrained", str, int, Truthy, NonTruthy)
10101010

10111011
def error_constrained_var(var: None | Constrained) -> None:
1012-
if var: # E: FIXME x [optional-non-truthy]
1012+
if var: # E: "var" has type "Optional[str]" where both None and some other values (of type: "str") may behave as false in boolean contexts. Consider being explicit about the behaviour for None vs other falsy values. [optional-non-truthy] \
1013+
# E: "var" has type "Optional[int]" where both None and some other values (of type: "int") may behave as false in boolean contexts. Consider being explicit about the behaviour for None vs other falsy values. [optional-non-truthy] \
1014+
# E: "var" has type "Optional[NonTruthy]" where both None and some other values (of type: "NonTruthy") may behave as false in boolean contexts. Consider being explicit about the behaviour for None vs other falsy values. [optional-non-truthy]
10131015
...
10141016

1015-
not var # E: FIXME x [optional-non-truthy]
1017+
not var # E: "var" has type "Optional[str]" where both None and some other values (of type: "str") may behave as false in boolean contexts. Consider being explicit about the behaviour for None vs other falsy values. [optional-non-truthy] \
1018+
# E: "var" has type "Optional[int]" where both None and some other values (of type: "int") may behave as false in boolean contexts. Consider being explicit about the behaviour for None vs other falsy values. [optional-non-truthy] \
1019+
# E: "var" has type "Optional[NonTruthy]" where both None and some other values (of type: "NonTruthy") may behave as false in boolean contexts. Consider being explicit about the behaviour for None vs other falsy values. [optional-non-truthy]
10161020

10171021
[builtins fixtures/list.pyi]
10181022

0 commit comments

Comments
 (0)