Infer the args of an exception instance as a tuple of unknown contents - #3241
Infer the args of an exception instance as a tuple of unknown contents#3241ekanshul wants to merge 1 commit into
args of an exception instance as a tuple of unknown contents#3241Conversation
…tents
``ExceptionInstanceModel.attr_args`` returned a ``nodes.Tuple`` with no
elements, which consumers read as a known empty tuple: pylint reported
``unbalanced-tuple-unpacking`` ("right side has 0 values") for
``(message,) = exc.args``. The arguments are not known from the instance
alone, so return an instance of ``builtins.tuple`` instead.
Closes pylint-dev/pylint#11312
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
DanielNoord
left a comment
There was a problem hiding this comment.
Can't we set something on the Tuple node to indicate this as well?
|
I tried that, and a marker on the pylint reaches the message through if isinstance(node, (nodes.Tuple, nodes.List, nodes.Set, *DICT_TYPES)):
return node.itered()A The difference is which way each option fails. Returning an There is also precedent for this shape a few lines down in the same class, where That said, if you want the marker as well as the type change, so that consumers can distinguish "tuple of unknown contents" from any other |
Merging this PR will not alter performance
Comparing Footnotes
|
DanielNoord
left a comment
There was a problem hiding this comment.
@Pierre-Sassoulas @jacobtylerwalls What do you think of this change? I guess the explanation sort of makes sense?
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #3241 +/- ##
=======================================
Coverage 93.65% 93.65%
=======================================
Files 93 93
Lines 11613 11614 +1
=======================================
+ Hits 10876 10877 +1
Misses 737 737
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
Type of Changes
Description
ExceptionInstanceModel.attr_argsreturns anodes.Tuplewith no elements. Consumers read that as a known empty tuple: pylint reportsunbalanced-tuple-unpackingwith "right side has 0 values" forand for
first, second = exc.argsinside anexceptblock.The arguments an exception was created with are not known from the instance alone, so
argsis now inferred as an instance ofbuiltins.tuple(unknown contents, still iterable and subscriptable). This keeps the intent of #1749 (no assumption that the first argument is astr) while no longer claiming a length of zero. The three existing tests that assertednodes.Tupleare updated and a regression test is added; pylint's functional suite passes against this branch (apart fromunbalanced_tuple_unpacking, which fails identically on current astroid main in my environment because of thectypesbrain).Closes pylint-dev/pylint#11312