Skip to content

Commit 1d877de

Browse files
Fix consider-using-min-max-builtin (#9802) (#9803)
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 <[email protected]>
1 parent 8410f57 commit 1d877de

File tree

3 files changed

+8
-9
lines changed

3 files changed

+8
-9
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix a false positive for `consider-using-min-max-builtin` when the assignment target is an attribute.
2+
3+
Refs #9800

pylint/checkers/refactoring/refactoring_checker.py

+4-8
Original file line numberDiff line numberDiff line change
@@ -919,11 +919,9 @@ def get_node_name(node: nodes.NodeNG) -> str:
919919
"""Obtain simplest representation of a node as a string."""
920920
if isinstance(node, nodes.Name):
921921
return node.name # type: ignore[no-any-return]
922-
if isinstance(node, nodes.Attribute):
923-
return node.attrname # type: ignore[no-any-return]
924922
if isinstance(node, nodes.Const):
925923
return str(node.value)
926-
# this is a catch-all for nodes that are not of type Name or Attribute
924+
# this is a catch-all for nodes that are not of type Name or Const
927925
# extremely helpful for Call or BinOp
928926
return node.as_string() # type: ignore[no-any-return]
929927

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

947-
if hasattr(target, "name"):
948-
target_assignation = target.name
949-
elif hasattr(target, "attrname"):
950-
target_assignation = target.attrname
951-
else:
945+
if not (hasattr(target, "name") or hasattr(target, "attrname")):
952946
return
953947

948+
target_assignation = get_node_name(target)
949+
954950
if len(node.test.ops) > 1:
955951
return
956952
operator, right_statement = node.test.ops[0]

tests/functional/c/consider/consider_using_min_max_builtin.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ consider-using-max-builtin:26:0:27:19::Consider using 'value3 = max(value3, valu
88
consider-using-min-builtin:29:0:30:18::Consider using 'value2 = min(value2, value)' instead of unnecessary if block:UNDEFINED
99
consider-using-min-builtin:32:0:33:25::Consider using 'value = min(value, float(value3))' instead of unnecessary if block:UNDEFINED
1010
consider-using-min-builtin:36:0:37:27::Consider using 'value2 = min(value2, offset + value)' instead of unnecessary if block:UNDEFINED
11-
consider-using-min-builtin:45:0:46:17::Consider using 'value = min(value, 10)' instead of unnecessary if block:UNDEFINED
11+
consider-using-min-builtin:45:0:46:17::Consider using 'A1.value = min(A1.value, 10)' instead of unnecessary if block:UNDEFINED
1212
consider-using-min-builtin:69:0:70:11::Consider using 'A1 = min(A1, A2)' instead of unnecessary if block:UNDEFINED
1313
consider-using-max-builtin:72:0:73:11::Consider using 'A2 = max(A2, A1)' instead of unnecessary if block:UNDEFINED
1414
consider-using-min-builtin:75:0:76:11::Consider using 'A1 = min(A1, A2)' instead of unnecessary if block:UNDEFINED

0 commit comments

Comments
 (0)