diff --git a/mypy/checker.py b/mypy/checker.py index b8b85be3fbe8..957da2cd33bc 100644 --- a/mypy/checker.py +++ b/mypy/checker.py @@ -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": diff --git a/test-data/unit/check-errorcodes.test b/test-data/unit/check-errorcodes.test index fc498c9aa6c0..2d97b3ccec90 100644 --- a/test-data/unit/check-errorcodes.test +++ b/test-data/unit/check-errorcodes.test @@ -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 diff --git a/test-data/unit/check-flags.test b/test-data/unit/check-flags.test index 244e728f3ab6..cd45ceb45834 100644 --- a/test-data/unit/check-flags.test +++ b/test-data/unit/check-flags.test @@ -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] diff --git a/test-data/unit/check-inline-config.test b/test-data/unit/check-inline-config.test index db04536dd4f9..0cc2bd71270a 100644 --- a/test-data/unit/check-inline-config.test +++ b/test-data/unit/check-inline-config.test @@ -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] diff --git a/test-data/unit/check-python38.test b/test-data/unit/check-python38.test index 5b077c45580a..423daaf5ae8f 100644 --- a/test-data/unit/check-python38.test +++ b/test-data/unit/check-python38.test @@ -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 diff --git a/test-data/unit/check-unreachable-code.test b/test-data/unit/check-unreachable-code.test index a9e025632e24..b2fd44043435 100644 --- a/test-data/unit/check-unreachable-code.test +++ b/test-data/unit/check-unreachable-code.test @@ -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: