Skip to content

Commit

Permalink
remove no-op if
Browse files Browse the repository at this point in the history
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
  • Loading branch information
temyurchenko authored and DanielNoord committed Oct 28, 2024
1 parent ca0230d commit e380fd1
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions astroid/nodes/scoped_nodes/scoped_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand Down

0 comments on commit e380fd1

Please sign in to comment.