From 5d82c4b702c7c4ba66d14d0e15e2f5104b18dce6 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Thu, 2 Oct 2025 13:48:20 +0200 Subject: [PATCH] Improve conditionals --- astroid/brain/brain_builtin_inference.py | 7 ++++--- astroid/brain/brain_dataclasses.py | 7 ++++--- astroid/brain/brain_type.py | 2 +- astroid/brain/brain_typing.py | 16 ++++++++-------- astroid/helpers.py | 2 +- astroid/nodes/scoped_nodes/scoped_nodes.py | 11 ++++++----- astroid/protocols.py | 2 +- tests/test_get_relative_base_path.py | 2 +- 8 files changed, 26 insertions(+), 23 deletions(-) diff --git a/astroid/brain/brain_builtin_inference.py b/astroid/brain/brain_builtin_inference.py index 02d487581..e21d36141 100644 --- a/astroid/brain/brain_builtin_inference.py +++ b/astroid/brain/brain_builtin_inference.py @@ -998,7 +998,7 @@ def _infer_copy_method( def _is_str_format_call(node: nodes.Call) -> bool: """Catch calls to str.format().""" - if not isinstance(node.func, nodes.Attribute) or not node.func.attrname == "format": + if not (isinstance(node.func, nodes.Attribute) and node.func.attrname == "format"): return False if isinstance(node.func.expr, nodes.Name): @@ -1018,8 +1018,9 @@ def _infer_str_format_call( value: nodes.Const if isinstance(node.func.expr, nodes.Name): - if not (inferred := util.safe_infer(node.func.expr)) or not isinstance( - inferred, nodes.Const + if not ( + (inferred := util.safe_infer(node.func.expr)) + and isinstance(inferred, nodes.Const) ): return iter([util.Uninferable]) value = inferred diff --git a/astroid/brain/brain_dataclasses.py b/astroid/brain/brain_dataclasses.py index 58ae82f95..bafd5f1c4 100644 --- a/astroid/brain/brain_dataclasses.py +++ b/astroid/brain/brain_dataclasses.py @@ -45,7 +45,7 @@ def is_decorated_with_dataclass( node: nodes.ClassDef, decorator_names: frozenset[str] = DATACLASSES_DECORATORS ) -> bool: """Return True if a decorated node has a `dataclass` decorator applied.""" - if not isinstance(node, nodes.ClassDef) or not node.decorators: + if not (isinstance(node, nodes.ClassDef) and node.decorators): return False return any( @@ -112,8 +112,9 @@ def _get_dataclass_attributes( If init is True, also include InitVars. """ for assign_node in node.body: - if not isinstance(assign_node, nodes.AnnAssign) or not isinstance( - assign_node.target, nodes.AssignName + if not ( + isinstance(assign_node, nodes.AnnAssign) + and isinstance(assign_node.target, nodes.AssignName) ): continue diff --git a/astroid/brain/brain_type.py b/astroid/brain/brain_type.py index c0cad6e65..8391e5997 100644 --- a/astroid/brain/brain_type.py +++ b/astroid/brain/brain_type.py @@ -53,7 +53,7 @@ def infer_type_sub(node, context: InferenceContext | None = None): :rtype: nodes.NodeNG """ node_scope, _ = node.scope().lookup("type") - if not isinstance(node_scope, nodes.Module) or node_scope.qname() != "builtins": + if not (isinstance(node_scope, nodes.Module) and node_scope.qname() == "builtins"): raise UseInferenceDefault() class_src = """ class type: diff --git a/astroid/brain/brain_typing.py b/astroid/brain/brain_typing.py index e1679ae72..694ef2622 100644 --- a/astroid/brain/brain_typing.py +++ b/astroid/brain/brain_typing.py @@ -291,10 +291,10 @@ def infer_typing_alias( # TODO: evaluate if still necessary when Py3.12 is minimum """ - if ( - not isinstance(node.parent, nodes.Assign) - or not len(node.parent.targets) == 1 - or not isinstance(node.parent.targets[0], nodes.AssignName) + if not ( + isinstance(node.parent, nodes.Assign) + and len(node.parent.targets) == 1 + and isinstance(node.parent.targets[0], nodes.AssignName) ): raise UseInferenceDefault try: @@ -408,10 +408,10 @@ def infer_typing_cast( func = next(node.func.infer(context=ctx)) except (InferenceError, StopIteration) as exc: raise UseInferenceDefault from exc - if ( - not isinstance(func, nodes.FunctionDef) - or func.qname() != "typing.cast" - or len(node.args) != 2 + if not ( + isinstance(func, nodes.FunctionDef) + and func.qname() == "typing.cast" + and len(node.args) == 2 ): raise UseInferenceDefault diff --git a/astroid/helpers.py b/astroid/helpers.py index 360c1ae90..9c370aa32 100644 --- a/astroid/helpers.py +++ b/astroid/helpers.py @@ -104,7 +104,7 @@ def object_type( types = set(_object_type(node, context)) except InferenceError: return util.Uninferable - if len(types) > 1 or not types: + if len(types) != 1: return util.Uninferable return next(iter(types)) diff --git a/astroid/nodes/scoped_nodes/scoped_nodes.py b/astroid/nodes/scoped_nodes/scoped_nodes.py index 020ea4016..885562393 100644 --- a/astroid/nodes/scoped_nodes/scoped_nodes.py +++ b/astroid/nodes/scoped_nodes/scoped_nodes.py @@ -1223,7 +1223,7 @@ def extra_decorators(self) -> list[node_classes.Call]: The property will return all the callables that are used for decoration. """ - if not self.parent or not isinstance(frame := self.parent.frame(), ClassDef): + if not (self.parent and isinstance(frame := self.parent.frame(), ClassDef)): return [] decorators: list[node_classes.Call] = [] @@ -1517,7 +1517,7 @@ def _infer( ) -> Generator[objects.Property | FunctionDef, None, InferenceErrorInfo]: from astroid import objects # pylint: disable=import-outside-toplevel - if not self.decorators or not bases._is_property(self): + if not (self.decorators and bases._is_property(self)): yield self return InferenceErrorInfo(node=self, context=context) @@ -2725,9 +2725,10 @@ def _islots(self): for elt in values: try: for inferred in elt.infer(): - if not isinstance( - inferred, node_classes.Const - ) or not isinstance(inferred.value, str): + if not ( + isinstance(inferred, node_classes.Const) + and isinstance(inferred.value, str) + ): continue if not inferred.value: continue diff --git a/astroid/protocols.py b/astroid/protocols.py index 41f9c5b2a..50e5cfa5e 100644 --- a/astroid/protocols.py +++ b/astroid/protocols.py @@ -143,7 +143,7 @@ def _multiply_seq_by_int( context: InferenceContext, ) -> _TupleListNodeT: node = self.__class__(parent=opnode) - if value <= 0 or not self.elts: + if not (value > 0 and self.elts): node.elts = [] return node if len(self.elts) * value > 1e8: diff --git a/tests/test_get_relative_base_path.py b/tests/test_get_relative_base_path.py index ddf35588a..280a72588 100644 --- a/tests/test_get_relative_base_path.py +++ b/tests/test_get_relative_base_path.py @@ -14,7 +14,7 @@ def setUp(self): self.cwd = os.getcwd() def _run_relative_path_test(self, target, base, expected): - if not target or not base: + if not (target and base): result = None else: base_dir = os.path.join(self.cwd, base)