Skip to content

Commit

Permalink
Fix consider-using-min-max-builtin (#9802) (#9803)
Browse files Browse the repository at this point in the history
Fix a false positive for `consider-using-min-max-builtin` when the
assignment target is an attribute.

(cherry picked from commit 6236b91)

Co-authored-by: Ekin Dursun <ekindursun@gmail.com>
  • Loading branch information
github-actions[bot] and onlined committed Jul 16, 2024
1 parent 8410f57 commit 1d877de
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 9 deletions.
3 changes: 3 additions & 0 deletions doc/whatsnew/fragments/9800.false_negative
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix a false positive for `consider-using-min-max-builtin` when the assignment target is an attribute.

Refs #9800
12 changes: 4 additions & 8 deletions pylint/checkers/refactoring/refactoring_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -919,11 +919,9 @@ def get_node_name(node: nodes.NodeNG) -> str:
"""Obtain simplest representation of a node as a string."""
if isinstance(node, nodes.Name):
return node.name # type: ignore[no-any-return]
if isinstance(node, nodes.Attribute):
return node.attrname # type: ignore[no-any-return]
if isinstance(node, nodes.Const):
return str(node.value)
# this is a catch-all for nodes that are not of type Name or Attribute
# this is a catch-all for nodes that are not of type Name or Const
# extremely helpful for Call or BinOp
return node.as_string() # type: ignore[no-any-return]

Expand All @@ -944,13 +942,11 @@ def get_node_name(node: nodes.NodeNG) -> str:
# is of type name or attribute. Attribute referring to NamedTuple.x perse.
# So we have to check that target is of these types

if hasattr(target, "name"):
target_assignation = target.name
elif hasattr(target, "attrname"):
target_assignation = target.attrname
else:
if not (hasattr(target, "name") or hasattr(target, "attrname")):
return

target_assignation = get_node_name(target)

if len(node.test.ops) > 1:
return
operator, right_statement = node.test.ops[0]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ consider-using-max-builtin:26:0:27:19::Consider using 'value3 = max(value3, valu
consider-using-min-builtin:29:0:30:18::Consider using 'value2 = min(value2, value)' instead of unnecessary if block:UNDEFINED
consider-using-min-builtin:32:0:33:25::Consider using 'value = min(value, float(value3))' instead of unnecessary if block:UNDEFINED
consider-using-min-builtin:36:0:37:27::Consider using 'value2 = min(value2, offset + value)' instead of unnecessary if block:UNDEFINED
consider-using-min-builtin:45:0:46:17::Consider using 'value = min(value, 10)' instead of unnecessary if block:UNDEFINED
consider-using-min-builtin:45:0:46:17::Consider using 'A1.value = min(A1.value, 10)' instead of unnecessary if block:UNDEFINED
consider-using-min-builtin:69:0:70:11::Consider using 'A1 = min(A1, A2)' instead of unnecessary if block:UNDEFINED
consider-using-max-builtin:72:0:73:11::Consider using 'A2 = max(A2, A1)' instead of unnecessary if block:UNDEFINED
consider-using-min-builtin:75:0:76:11::Consider using 'A1 = min(A1, A2)' instead of unnecessary if block:UNDEFINED
Expand Down

0 comments on commit 1d877de

Please sign in to comment.