Closed as not planned
Description
Current problem
Here's a Pylint internal function:
def is_super(node: nodes.NodeNG) -> bool:
"""Return True if the node is referencing the "super" builtin function."""
if getattr(node, "name", None) == "super" and node.root().name == "builtins":
return True
return False
This is a very common pattern, and it is easy to simplify:
def is_super(node: nodes.NodeNG) -> bool:
"""Return True if the node is referencing the "super" builtin function."""
return getattr(node, "name", None) == "super" and node.root().name == "builtins"
IMO, returning booleans conditionally can be a code smell. For example, here is another Pylint internal function:
def is_ancestor_name(frame: nodes.ClassDef, node: nodes.NodeNG) -> bool:
"""Return whether `frame` is an astroid.Class node with `node` in the
subtree of its bases attribute.
"""
if not isinstance(frame, nodes.ClassDef):
return False
return any(node in base.nodes_of_class(nodes.Name) for base in frame.bases)
This can be simplifed to:
def is_ancestor_name(frame: nodes.ClassDef, node: nodes.NodeNG) -> bool:
"""Return whether `frame` is an astroid.Class node with `node` in the
subtree of its bases attribute.
"""
return (
isinstance(frame, nodes.ClassDef)
and any(node in base.nodes_of_class(nodes.Name) for base in frame.bases)
)
Maybe one checker could handle both of these cases. It might be tricky to get the right coordination between not
and False
, so the various cases could be implemented incrementally.
Desired solution
Add new checker to simplify boolean returns
Additional context
No response