From e380fd1e6902c9ff6a411ba9aefb62444a57950b Mon Sep 17 00:00:00 2001 From: Artem Yurchenko Date: Tue, 1 Oct 2024 00:03:17 -0700 Subject: [PATCH] remove no-op if Note the `if` condition above, the `values` are asserted to be empty, so adding them to the result doesn't do anything. Also remove small inefficiencies --- astroid/nodes/scoped_nodes/scoped_nodes.py | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/astroid/nodes/scoped_nodes/scoped_nodes.py b/astroid/nodes/scoped_nodes/scoped_nodes.py index f94404086f..99ed79675b 100644 --- a/astroid/nodes/scoped_nodes/scoped_nodes.py +++ b/astroid/nodes/scoped_nodes/scoped_nodes.py @@ -2354,26 +2354,24 @@ def getattr( if name in self.special_attributes and class_context and not values: result = [self.special_attributes.lookup(name)] - if name == "__bases__": - # Need special treatment, since they are mutable - # and we need to return all the values. - result += values return result if class_context: values += self._metaclass_lookup_attribute(name, context) - # Remove AnnAssigns without value, which are not attributes in the purest sense. - for value in values.copy(): + result: list[InferenceResult] = [] + for value in values: if isinstance(value, node_classes.AssignName): stmt = value.statement() + # Ignore AnnAssigns without value, which are not attributes in the purest sense. if isinstance(stmt, node_classes.AnnAssign) and stmt.value is None: - values.pop(values.index(value)) + continue + result.append(value) - if not values: + if not result: raise AttributeInferenceError(target=self, attribute=name, context=context) - return values + return result @lru_cache(maxsize=1024) # noqa def _metaclass_lookup_attribute(self, name, context): @@ -2385,7 +2383,7 @@ def _metaclass_lookup_attribute(self, name, context): for cls in (implicit_meta, metaclass): if cls and cls != self and isinstance(cls, ClassDef): cls_attributes = self._get_attribute_from_metaclass(cls, name, context) - attrs.update(set(cls_attributes)) + attrs.update(cls_attributes) return attrs def _get_attribute_from_metaclass(self, cls, name, context):