Skip to content

Commit

Permalink
Fix incorrectly inferred Never return type for some function implem…
Browse files Browse the repository at this point in the history
…entations (#490)
  • Loading branch information
JelleZijlstra authored Mar 5, 2022
1 parent 0e1b9f7 commit 8cac649
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
2 changes: 2 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Fix incorrectly inferred `Never` return type for some function
implementations (#490)
- Infer precise call signatures for `TypedDict` types (#487)
- Add mechanism to prevent crashes on objects
with unusual `__getattr__` methods (#486)
Expand Down
6 changes: 5 additions & 1 deletion pyanalyze/implementation.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
ParameterKind,
)
from .value import (
NO_RETURN_VALUE,
UNINITIALIZED_VALUE,
AnnotatedValue,
AnySource,
Expand Down Expand Up @@ -98,8 +99,11 @@ def flatten_unions(
unwrap_annotated: bool = False,
) -> ImplReturn:
value_lists = [
flatten_values(val, unwrap_annotated=unwrap_annotated) for val in values
list(flatten_values(val, unwrap_annotated=unwrap_annotated)) for val in values
]
# If the lists are empty, we end up inferring Never as the return type, which
# generally isn't right.
value_lists = [lst if lst else [NO_RETURN_VALUE] for lst in value_lists]
results = [
clean_up_implementation_fn_return(callable(*vals))
for vals in product(*value_lists)
Expand Down
13 changes: 13 additions & 0 deletions pyanalyze/test_implementation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from .test_node_visitor import assert_passes

from .value import (
NO_RETURN_VALUE,
AnnotatedValue,
AnySource,
AnyValue,
Expand Down Expand Up @@ -519,6 +520,18 @@ def capybara(x: int, y: str) -> None:
lst += [x]
assert_is_value(lst, GenericValue(list, [TypedValue(int)]))

@assert_passes()
def test_list_iadd_never(self):
def render_feedback_text():
z = []

detail_text = None
if detail_text:
assert_is_value(detail_text, NO_RETURN_VALUE)
z += detail_text

return z

@assert_passes()
def test_weak_value(self):
from typing import List
Expand Down

0 comments on commit 8cac649

Please sign in to comment.