Skip to content

Commit f176f6a

Browse files
authored
Better message for truthy functions (#15193)
Fixes case 1 in #14529
1 parent 9ecc4ef commit f176f6a

File tree

6 files changed

+17
-11
lines changed

6 files changed

+17
-11
lines changed

mypy/checker.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5295,10 +5295,15 @@ def format_expr_type() -> str:
52955295
else:
52965296
return f"Expression has type {typ}"
52975297

5298+
def get_expr_name() -> str:
5299+
if isinstance(expr, (NameExpr, MemberExpr)):
5300+
return f'"{expr.name}"'
5301+
else:
5302+
# return type if expr has no name
5303+
return format_type(t, self.options)
5304+
52985305
if isinstance(t, FunctionLike):
5299-
self.fail(
5300-
message_registry.FUNCTION_ALWAYS_TRUE.format(format_type(t, self.options)), expr
5301-
)
5306+
self.fail(message_registry.FUNCTION_ALWAYS_TRUE.format(get_expr_name()), expr)
53025307
elif isinstance(t, UnionType):
53035308
self.fail(message_registry.TYPE_ALWAYS_TRUE_UNIONTYPE.format(format_expr_type()), expr)
53045309
elif isinstance(t, Instance) and t.type.fullname == "typing.Iterable":

test-data/unit/check-errorcodes.test

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -900,11 +900,11 @@ if any_or_object:
900900
# flags: --strict-optional
901901
def f():
902902
pass
903-
if f: # E: Function "Callable[[], Any]" could always be true in boolean context [truthy-function]
903+
if f: # E: Function "f" could always be true in boolean context [truthy-function]
904904
pass
905-
if not f: # E: Function "Callable[[], Any]" could always be true in boolean context [truthy-function]
905+
if not f: # E: Function "f" could always be true in boolean context [truthy-function]
906906
pass
907-
conditional_result = 'foo' if f else 'bar' # E: Function "Callable[[], Any]" could always be true in boolean context [truthy-function]
907+
conditional_result = 'foo' if f else 'bar' # E: Function "f" could always be true in boolean context [truthy-function]
908908

909909
[case testTruthyIterable]
910910
# flags: --strict-optional --enable-error-code truthy-iterable

test-data/unit/check-flags.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2101,12 +2101,12 @@ import tests.foo
21012101
import bar
21022102
[file bar.py]
21032103
def foo() -> int: ...
2104-
if foo: ... # E: Function "Callable[[], int]" could always be true in boolean context
2104+
if foo: ... # E: Function "foo" could always be true in boolean context
21052105
42 + "no" # type: ignore # E: "type: ignore" comment without error code (consider "type: ignore[operator]" instead)
21062106
[file tests/__init__.py]
21072107
[file tests/foo.py]
21082108
def foo() -> int: ...
2109-
if foo: ... # E: Function "Callable[[], int]" could always be true in boolean context
2109+
if foo: ... # E: Function "foo" could always be true in boolean context
21102110
42 + "no" # type: ignore
21112111
[file mypy.ini]
21122112
\[mypy]

test-data/unit/check-inline-config.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ if foo: ...
192192
# mypy: enable-error-code="ignore-without-code"
193193

194194
def foo() -> int: ...
195-
if foo: ... # E: Function "Callable[[], int]" could always be true in boolean context
195+
if foo: ... # E: Function "foo" could always be true in boolean context
196196
42 + "no" # type: ignore # E: "type: ignore" comment without error code (consider "type: ignore[operator]" instead)
197197

198198
[file tests/baz.py]

test-data/unit/check-python38.test

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,8 @@ def f(x: int = (c := 4)) -> int:
310310
z2: NT # E: Variable "NT" is not valid as a type \
311311
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
312312

313-
if Alias := int: # E: Function "Type[int]" could always be true in boolean context
313+
if Alias := int: # E: Function "Alias" could always be true in boolean context \
314+
# E: Function "int" could always be true in boolean context
314315
z3: Alias # E: Variable "Alias" is not valid as a type \
315316
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
316317

test-data/unit/check-unreachable-code.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -936,7 +936,7 @@ class Case1:
936936
return False and self.missing() # E: Right operand of "and" is never evaluated
937937

938938
def test2(self) -> bool:
939-
return not self.property_decorator_missing and self.missing() # E: Function "Callable[[], bool]" could always be true in boolean context \
939+
return not self.property_decorator_missing and self.missing() # E: Function "property_decorator_missing" could always be true in boolean context \
940940
# E: Right operand of "and" is never evaluated
941941

942942
def property_decorator_missing(self) -> bool:

0 commit comments

Comments
 (0)