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

Fix incorrectly inferred Never return type for some function implementations #490

Merged
merged 2 commits into from
Mar 5, 2022
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
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