Skip to content

Commit

Permalink
Emit unused-argument for functions that partially uses their argu…
Browse files Browse the repository at this point in the history
…ment list before raising an exception.

Close #3246
  • Loading branch information
PCManticore committed Feb 5, 2020
1 parent 594b4a0 commit dde9ac3
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 11 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@ Release date: TBA

Close #1603

* Emit ``unused-argument`` for functions that partially uses their argument list before raising an exception.

Close #3246

* Fixed ``broad_try_clause`` extension to check try/finally statements and to
check for nested statements (e.g., inside of an ``if`` statement).

Expand Down
13 changes: 3 additions & 10 deletions pylint/checkers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,16 +263,9 @@ def is_super(node: astroid.node_classes.NodeNG) -> bool:
return False


def is_error(node: astroid.node_classes.NodeNG) -> bool:
"""return true if the function does nothing but raising an exception"""
raises = False
returns = False
for child_node in node.nodes_of_class((astroid.Raise, astroid.Return)):
if isinstance(child_node, astroid.Raise):
raises = True
if isinstance(child_node, astroid.Return):
returns = True
return raises and not returns
def is_error(node: astroid.scoped_nodes.FunctionDef) -> bool:
"""Return true if the given function node only raises an exception"""
return len(node.body) == 1 and isinstance(node.body[0], astroid.Raise)


builtins = builtins.__dict__.copy() # type: ignore
Expand Down
5 changes: 5 additions & 0 deletions tests/functional/u/unused_argument_py3.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@

def func(first, *, second): # [unused-argument, unused-argument]
pass


def only_raises(first, second=42): # [unused-argument]
if first == 24:
raise ValueError
3 changes: 2 additions & 1 deletion tests/functional/u/unused_argument_py3.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
unused-argument:3:func:Unused argument 'first'
unused-argument:3:func:Unused argument 'second'
unused-argument:3:func:Unused argument 'second'
unused-argument:7:only_raises:Unused argument 'second'

0 comments on commit dde9ac3

Please sign in to comment.