Skip to content

Commit e380fd1

Browse files
temyurchenkoDanielNoord
authored andcommitted
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
1 parent ca0230d commit e380fd1

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

astroid/nodes/scoped_nodes/scoped_nodes.py

+8-10
Original file line numberDiff line numberDiff line change
@@ -2354,26 +2354,24 @@ def getattr(
23542354

23552355
if name in self.special_attributes and class_context and not values:
23562356
result = [self.special_attributes.lookup(name)]
2357-
if name == "__bases__":
2358-
# Need special treatment, since they are mutable
2359-
# and we need to return all the values.
2360-
result += values
23612357
return result
23622358

23632359
if class_context:
23642360
values += self._metaclass_lookup_attribute(name, context)
23652361

2366-
# Remove AnnAssigns without value, which are not attributes in the purest sense.
2367-
for value in values.copy():
2362+
result: list[InferenceResult] = []
2363+
for value in values:
23682364
if isinstance(value, node_classes.AssignName):
23692365
stmt = value.statement()
2366+
# Ignore AnnAssigns without value, which are not attributes in the purest sense.
23702367
if isinstance(stmt, node_classes.AnnAssign) and stmt.value is None:
2371-
values.pop(values.index(value))
2368+
continue
2369+
result.append(value)
23722370

2373-
if not values:
2371+
if not result:
23742372
raise AttributeInferenceError(target=self, attribute=name, context=context)
23752373

2376-
return values
2374+
return result
23772375

23782376
@lru_cache(maxsize=1024) # noqa
23792377
def _metaclass_lookup_attribute(self, name, context):
@@ -2385,7 +2383,7 @@ def _metaclass_lookup_attribute(self, name, context):
23852383
for cls in (implicit_meta, metaclass):
23862384
if cls and cls != self and isinstance(cls, ClassDef):
23872385
cls_attributes = self._get_attribute_from_metaclass(cls, name, context)
2388-
attrs.update(set(cls_attributes))
2386+
attrs.update(cls_attributes)
23892387
return attrs
23902388

23912389
def _get_attribute_from_metaclass(self, cls, name, context):

0 commit comments

Comments
 (0)