Skip to content

Commit 0562ce3

Browse files
authored
Improve conditionals (#2845)
1 parent f25ef82 commit 0562ce3

File tree

8 files changed

+26
-23
lines changed

8 files changed

+26
-23
lines changed

astroid/brain/brain_builtin_inference.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -998,7 +998,7 @@ def _infer_copy_method(
998998

999999
def _is_str_format_call(node: nodes.Call) -> bool:
10001000
"""Catch calls to str.format()."""
1001-
if not isinstance(node.func, nodes.Attribute) or not node.func.attrname == "format":
1001+
if not (isinstance(node.func, nodes.Attribute) and node.func.attrname == "format"):
10021002
return False
10031003

10041004
if isinstance(node.func.expr, nodes.Name):
@@ -1018,8 +1018,9 @@ def _infer_str_format_call(
10181018

10191019
value: nodes.Const
10201020
if isinstance(node.func.expr, nodes.Name):
1021-
if not (inferred := util.safe_infer(node.func.expr)) or not isinstance(
1022-
inferred, nodes.Const
1021+
if not (
1022+
(inferred := util.safe_infer(node.func.expr))
1023+
and isinstance(inferred, nodes.Const)
10231024
):
10241025
return iter([util.Uninferable])
10251026
value = inferred

astroid/brain/brain_dataclasses.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def is_decorated_with_dataclass(
4545
node: nodes.ClassDef, decorator_names: frozenset[str] = DATACLASSES_DECORATORS
4646
) -> bool:
4747
"""Return True if a decorated node has a `dataclass` decorator applied."""
48-
if not isinstance(node, nodes.ClassDef) or not node.decorators:
48+
if not (isinstance(node, nodes.ClassDef) and node.decorators):
4949
return False
5050

5151
return any(
@@ -112,8 +112,9 @@ def _get_dataclass_attributes(
112112
If init is True, also include InitVars.
113113
"""
114114
for assign_node in node.body:
115-
if not isinstance(assign_node, nodes.AnnAssign) or not isinstance(
116-
assign_node.target, nodes.AssignName
115+
if not (
116+
isinstance(assign_node, nodes.AnnAssign)
117+
and isinstance(assign_node.target, nodes.AssignName)
117118
):
118119
continue
119120

astroid/brain/brain_type.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def infer_type_sub(node, context: InferenceContext | None = None):
5353
:rtype: nodes.NodeNG
5454
"""
5555
node_scope, _ = node.scope().lookup("type")
56-
if not isinstance(node_scope, nodes.Module) or node_scope.qname() != "builtins":
56+
if not (isinstance(node_scope, nodes.Module) and node_scope.qname() == "builtins"):
5757
raise UseInferenceDefault()
5858
class_src = """
5959
class type:

astroid/brain/brain_typing.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -291,10 +291,10 @@ def infer_typing_alias(
291291
292292
# TODO: evaluate if still necessary when Py3.12 is minimum
293293
"""
294-
if (
295-
not isinstance(node.parent, nodes.Assign)
296-
or not len(node.parent.targets) == 1
297-
or not isinstance(node.parent.targets[0], nodes.AssignName)
294+
if not (
295+
isinstance(node.parent, nodes.Assign)
296+
and len(node.parent.targets) == 1
297+
and isinstance(node.parent.targets[0], nodes.AssignName)
298298
):
299299
raise UseInferenceDefault
300300
try:
@@ -412,10 +412,10 @@ def infer_typing_cast(
412412
func = next(node.func.infer(context=ctx))
413413
except (InferenceError, StopIteration) as exc:
414414
raise UseInferenceDefault from exc
415-
if (
416-
not isinstance(func, nodes.FunctionDef)
417-
or func.qname() != "typing.cast"
418-
or len(node.args) != 2
415+
if not (
416+
isinstance(func, nodes.FunctionDef)
417+
and func.qname() == "typing.cast"
418+
and len(node.args) == 2
419419
):
420420
raise UseInferenceDefault
421421

astroid/helpers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def object_type(
104104
types = set(_object_type(node, context))
105105
except InferenceError:
106106
return util.Uninferable
107-
if len(types) > 1 or not types:
107+
if len(types) != 1:
108108
return util.Uninferable
109109
return next(iter(types))
110110

astroid/nodes/scoped_nodes/scoped_nodes.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,7 +1223,7 @@ def extra_decorators(self) -> list[node_classes.Call]:
12231223
The property will return all the callables that are used for
12241224
decoration.
12251225
"""
1226-
if not self.parent or not isinstance(frame := self.parent.frame(), ClassDef):
1226+
if not (self.parent and isinstance(frame := self.parent.frame(), ClassDef)):
12271227
return []
12281228

12291229
decorators: list[node_classes.Call] = []
@@ -1517,7 +1517,7 @@ def _infer(
15171517
) -> Generator[objects.Property | FunctionDef, None, InferenceErrorInfo]:
15181518
from astroid import objects # pylint: disable=import-outside-toplevel
15191519

1520-
if not self.decorators or not bases._is_property(self):
1520+
if not (self.decorators and bases._is_property(self)):
15211521
yield self
15221522
return InferenceErrorInfo(node=self, context=context)
15231523

@@ -2725,9 +2725,10 @@ def _islots(self):
27252725
for elt in values:
27262726
try:
27272727
for inferred in elt.infer():
2728-
if not isinstance(
2729-
inferred, node_classes.Const
2730-
) or not isinstance(inferred.value, str):
2728+
if not (
2729+
isinstance(inferred, node_classes.Const)
2730+
and isinstance(inferred.value, str)
2731+
):
27312732
continue
27322733
if not inferred.value:
27332734
continue

astroid/protocols.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ def _multiply_seq_by_int(
143143
context: InferenceContext,
144144
) -> _TupleListNodeT:
145145
node = self.__class__(parent=opnode)
146-
if value <= 0 or not self.elts:
146+
if not (value > 0 and self.elts):
147147
node.elts = []
148148
return node
149149
if len(self.elts) * value > 1e8:

tests/test_get_relative_base_path.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def setUp(self):
1414
self.cwd = os.getcwd()
1515

1616
def _run_relative_path_test(self, target, base, expected):
17-
if not target or not base:
17+
if not (target and base):
1818
result = None
1919
else:
2020
base_dir = os.path.join(self.cwd, base)

0 commit comments

Comments
 (0)