diff --git a/doc/whatsnew/fragments/10721.false_positive b/doc/whatsnew/fragments/10721.false_positive new file mode 100644 index 0000000000..cb4fa3d485 --- /dev/null +++ b/doc/whatsnew/fragments/10721.false_positive @@ -0,0 +1,3 @@ +Fix a false positive for ``unbalanced-tuple-unpacking`` when a tuple is assigned to a function call and the structure of the function's return value is ambiguous. + +Closes #10721 diff --git a/pylint/checkers/variables.py b/pylint/checkers/variables.py index 43fb86fe34..5add0732c0 100644 --- a/pylint/checkers/variables.py +++ b/pylint/checkers/variables.py @@ -2162,11 +2162,10 @@ def visit_assign(self, node: nodes.Assign) -> None: # Check if we have starred nodes. if any(isinstance(target, nodes.Starred) for target in targets): return - try: - inferred = utils.safe_infer(node.value) - if inferred is not None: - self._check_unpacking(inferred, node, targets) + inferred = node.value.inferred() + if inferred is not None and len(inferred) == 1: + self._check_unpacking(inferred[0], node, targets) except astroid.InferenceError: return diff --git a/tests/functional/u/unbalanced/unbalanced_tuple_unpacking.py b/tests/functional/u/unbalanced/unbalanced_tuple_unpacking.py index d6c315e9e9..308715afaf 100644 --- a/tests/functional/u/unbalanced/unbalanced_tuple_unpacking.py +++ b/tests/functional/u/unbalanced/unbalanced_tuple_unpacking.py @@ -167,3 +167,14 @@ def my_function(mystring): # Using a lot of args, so we have a high probability to still trigger the problem if # we add arguments to our unittest command later (p, q, r, s, t, u, v, w, x, y, z) = sys.argv # pylint: disable=invalid-name + + +# https://github.com/pylint-dev/pylint/issues/10721 +def fruit(apple: int, pear: int, kiwi: int | None = None) -> tuple[int, int] | tuple[int, int, int]: + return (apple, pear) if kiwi is None else (apple, pear, kiwi) + +def main(): + _, _ = fruit(1, 2) + _, _ = fruit(1, 2, 3) # known false negative, requires better None comprehension in astroid + _, _, _ = fruit(1, 2) # known false negative, requires better None comprehension in astroid + _, _, _ = fruit(1, 2, 3)