-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
False negative - tuple unpacking #5708
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
Changes from 3 commits
cfd6990
4005ac6
32535b7
f0c9292
4402f1d
9ed721d
d26875f
2f8ac19
186d45e
26eeae3
e5ab145
a8798ad
1804cb8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -2400,15 +2400,8 @@ def _check_unpacking(self, inferred, node, targets): | |||||||||
return | ||||||||||
|
||||||||||
# Attempt to check unpacking is properly balanced | ||||||||||
values: Optional[List] = None | ||||||||||
if isinstance(inferred, (nodes.Tuple, nodes.List)): | ||||||||||
values = inferred.itered() | ||||||||||
elif isinstance(inferred, astroid.Instance) and any( | ||||||||||
ancestor.qname() == "typing.NamedTuple" for ancestor in inferred.ancestors() | ||||||||||
): | ||||||||||
values = [i for i in inferred.values() if isinstance(i, nodes.AssignName)] | ||||||||||
|
||||||||||
if values: | ||||||||||
values = self._get_values_to_unpack(inferred) | ||||||||||
if values is not None: | ||||||||||
mbyrnepr2 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
if len(targets) != len(values): | ||||||||||
# Check if we have starred nodes. | ||||||||||
if any(isinstance(target, nodes.Starred) for target in targets): | ||||||||||
|
@@ -2430,6 +2423,17 @@ def _check_unpacking(self, inferred, node, targets): | |||||||||
args=(_get_unpacking_extra_info(node, inferred),), | ||||||||||
) | ||||||||||
|
||||||||||
@staticmethod | ||||||||||
def _get_values_to_unpack(node: nodes.Assign) -> Optional[List]: | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add typing to the List? I guess:
Suggested change
Or:
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point; I think it is more accurate to use Moreover we can make the function name more accurate ( |
||||||||||
"""Return the list of values of the `Assign` node""" | ||||||||||
if isinstance(node, (nodes.Tuple, nodes.List)): | ||||||||||
return node.itered() | ||||||||||
if isinstance(node, astroid.Instance) and any( | ||||||||||
ancestor.qname() == "typing.NamedTuple" for ancestor in node.ancestors() | ||||||||||
): | ||||||||||
return [i for i in node.values() if isinstance(i, nodes.AssignName)] | ||||||||||
mbyrnepr2 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
return None | ||||||||||
|
||||||||||
def _check_module_attrs(self, node, module, module_names): | ||||||||||
"""check that module_names (list of string) are accessible through the | ||||||||||
given module | ||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
unbalanced-tuple-unpacking:10:4:10:27:do_stuff:"Possible unbalanced tuple unpacking with sequence (1, 2, 3): left side has 2 label(s), right side has 3 value(s)":UNDEFINED | ||
unbalanced-tuple-unpacking:15:4:15:29:do_stuff1:"Possible unbalanced tuple unpacking with sequence [1, 2, 3]: left side has 2 label(s), right side has 3 value(s)":UNDEFINED | ||
unbalanced-tuple-unpacking:20:4:20:29:do_stuff2:"Possible unbalanced tuple unpacking with sequence (1, 2, 3): left side has 2 label(s), right side has 3 value(s)":UNDEFINED | ||
unbalanced-tuple-unpacking:70:4:70:28:do_stuff9:"Possible unbalanced tuple unpacking with sequence defined at line 7 of functional.u.unpacking: left side has 2 label(s), right side has 3 value(s)":UNDEFINED | ||
unbalanced-tuple-unpacking:82:8:82:33:UnbalancedUnpacking.test:"Possible unbalanced tuple unpacking with sequence defined at line 7 of functional.u.unpacking: left side has 2 label(s), right side has 3 value(s)":UNDEFINED | ||
unbalanced-tuple-unpacking:124:8:124:43:MyClass.sum_unpack_3_into_4:"Possible unbalanced tuple unpacking with sequence defined at line 112: left side has 4 label(s), right side has 3 value(s)":UNDEFINED | ||
unbalanced-tuple-unpacking:129:8:129:28:MyClass.sum_unpack_3_into_2:"Possible unbalanced tuple unpacking with sequence defined at line 112: left side has 2 label(s), right side has 3 value(s)":UNDEFINED | ||
unbalanced-tuple-unpacking:11:4:11:27:do_stuff:"Possible unbalanced tuple unpacking with sequence (1, 2, 3): left side has 2 label(s), right side has 3 value(s)":UNDEFINED | ||
unbalanced-tuple-unpacking:17:4:17:29:do_stuff1:"Possible unbalanced tuple unpacking with sequence [1, 2, 3]: left side has 2 label(s), right side has 3 value(s)":UNDEFINED | ||
unbalanced-tuple-unpacking:23:4:23:29:do_stuff2:"Possible unbalanced tuple unpacking with sequence (1, 2, 3): left side has 2 label(s), right side has 3 value(s)":UNDEFINED | ||
unbalanced-tuple-unpacking:82:4:82:28:do_stuff9:"Possible unbalanced tuple unpacking with sequence defined at line 7 of functional.u.unpacking: left side has 2 label(s), right side has 3 value(s)":UNDEFINED | ||
unbalanced-tuple-unpacking:96:8:96:33:UnbalancedUnpacking.test:"Possible unbalanced tuple unpacking with sequence defined at line 7 of functional.u.unpacking: left side has 2 label(s), right side has 3 value(s)":UNDEFINED | ||
unbalanced-tuple-unpacking:140:8:140:43:MyClass.sum_unpack_3_into_4:"Possible unbalanced tuple unpacking with sequence defined at line 128: left side has 4 label(s), right side has 3 value(s)":UNDEFINED | ||
unbalanced-tuple-unpacking:145:8:145:28:MyClass.sum_unpack_3_into_2:"Possible unbalanced tuple unpacking with sequence defined at line 128: left side has 2 label(s), right side has 3 value(s)":UNDEFINED | ||
unbalanced-tuple-unpacking:157:0:157:24::"Possible unbalanced tuple unpacking with sequence defined at line 151: left side has 2 label(s), right side has 0 value(s)":UNDEFINED |
Uh oh!
There was an error while loading. Please reload this page.