Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better message for truthy functions #15193

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -5295,10 +5295,15 @@ def format_expr_type() -> str:
else:
return f"Expression has type {typ}"

def get_expr_name() -> str:
if isinstance(expr, (NameExpr, MemberExpr)):
return f'"{expr.name}"'
else:
# return type if expr has no name
return format_type(t, self.options)

if isinstance(t, FunctionLike):
self.fail(
message_registry.FUNCTION_ALWAYS_TRUE.format(format_type(t, self.options)), expr
)
self.fail(message_registry.FUNCTION_ALWAYS_TRUE.format(get_expr_name()), expr)
elif isinstance(t, UnionType):
self.fail(message_registry.TYPE_ALWAYS_TRUE_UNIONTYPE.format(format_expr_type()), expr)
elif isinstance(t, Instance) and t.type.fullname == "typing.Iterable":
Expand Down
6 changes: 3 additions & 3 deletions test-data/unit/check-errorcodes.test
Original file line number Diff line number Diff line change
Expand Up @@ -900,11 +900,11 @@ if any_or_object:
# flags: --strict-optional
def f():
pass
if f: # E: Function "Callable[[], Any]" could always be true in boolean context [truthy-function]
if f: # E: Function "f" could always be true in boolean context [truthy-function]
pass
if not f: # E: Function "Callable[[], Any]" could always be true in boolean context [truthy-function]
if not f: # E: Function "f" could always be true in boolean context [truthy-function]
pass
conditional_result = 'foo' if f else 'bar' # E: Function "Callable[[], Any]" could always be true in boolean context [truthy-function]
conditional_result = 'foo' if f else 'bar' # E: Function "f" could always be true in boolean context [truthy-function]

[case testTruthyIterable]
# flags: --strict-optional --enable-error-code truthy-iterable
Expand Down
4 changes: 2 additions & 2 deletions test-data/unit/check-flags.test
Original file line number Diff line number Diff line change
Expand Up @@ -2101,12 +2101,12 @@ import tests.foo
import bar
[file bar.py]
def foo() -> int: ...
if foo: ... # E: Function "Callable[[], int]" could always be true in boolean context
if foo: ... # E: Function "foo" could always be true in boolean context
42 + "no" # type: ignore # E: "type: ignore" comment without error code (consider "type: ignore[operator]" instead)
[file tests/__init__.py]
[file tests/foo.py]
def foo() -> int: ...
if foo: ... # E: Function "Callable[[], int]" could always be true in boolean context
if foo: ... # E: Function "foo" could always be true in boolean context
42 + "no" # type: ignore
[file mypy.ini]
\[mypy]
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-inline-config.test
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ if foo: ...
# mypy: enable-error-code="ignore-without-code"

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

[file tests/baz.py]
Expand Down
3 changes: 2 additions & 1 deletion test-data/unit/check-python38.test
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,8 @@ def f(x: int = (c := 4)) -> int:
z2: NT # E: Variable "NT" is not valid as a type \
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases

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

Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-unreachable-code.test
Original file line number Diff line number Diff line change
Expand Up @@ -936,7 +936,7 @@ class Case1:
return False and self.missing() # E: Right operand of "and" is never evaluated

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

def property_decorator_missing(self) -> bool:
Expand Down