Skip to content

Commit

Permalink
Improve error message for incompatible return type (#735)
Browse files Browse the repository at this point in the history
The existing message was too verbose and arguably the compatibility
was in the wrong direction.

For comparison:
- Pyre: Incompatible return type [7]: Expected `str` but got `bool`
- Mypy: Incompatible return value type (got "bool", expected "str")
- Pyright: Expression of type "Literal[False]" cannot be assigned to return type "None"
  • Loading branch information
JelleZijlstra authored Mar 2, 2024
1 parent 825e641 commit 9de69d4
Showing 1 changed file with 2 additions and 3 deletions.
5 changes: 2 additions & 3 deletions pyanalyze/name_check_visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4050,7 +4050,7 @@ def visit_Return(self, node: ast.Return) -> None:
if isinstance(can_assign, CanAssignError):
self._show_error_if_checking(
node,
f"Incompatible return value {value} (expected {expected})",
f"Incompatible return type: expected {expected}, got {value}",
error_code=ErrorCode.incompatible_return_value,
detail=can_assign.display(),
)
Expand All @@ -4059,8 +4059,7 @@ def visit_Return(self, node: ast.Return) -> None:
if isinstance(can_assign, CanAssignError):
self._show_error_if_checking(
node,
f"Declared return type {self.expected_return_value} is"
f" incompatible with actual return type {value}",
f"Incompatible return type: expected {self.expected_return_value}, got {value}",
error_code=ErrorCode.incompatible_return_value,
detail=can_assign.display(),
)
Expand Down

0 comments on commit 9de69d4

Please sign in to comment.