Skip to content
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

[Backport maintenance/3.2.x] Fix consider-using-min-max-builtin #9803

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading