Skip to content

Commit 0d4f73d

Browse files
committed
Revert "Remove the restore_path() method"
This reverts commit 8151853 which was added while investigating the slowdown caused by context's path copying. Turns out this change doesn't have a negative impact on the performance but it has a positive one on the inference.
1 parent 7fa547b commit 0d4f73d

File tree

2 files changed

+31
-24
lines changed

2 files changed

+31
-24
lines changed

astroid/context.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
# For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER
88

99
"""Various context related utilities, including inference and call contexts."""
10-
10+
import contextlib
1111
import pprint
1212
from typing import Optional
1313

@@ -116,6 +116,12 @@ def cache_generator(self, key, generator):
116116

117117
self.inferred[key] = tuple(results)
118118

119+
@contextlib.contextmanager
120+
def restore_path(self):
121+
path = set(self.path)
122+
yield
123+
self.path = path
124+
119125
def __str__(self):
120126
state = (
121127
"%s=%s"

astroid/scoped_nodes.py

+24-23
Original file line numberDiff line numberDiff line change
@@ -2190,30 +2190,31 @@ def ancestors(self, recurs=True, context=None):
21902190
return
21912191

21922192
for stmt in self.bases:
2193-
try:
2194-
for baseobj in stmt.infer(context):
2195-
if not isinstance(baseobj, ClassDef):
2196-
if isinstance(baseobj, bases.Instance):
2197-
baseobj = baseobj._proxied
2198-
else:
2199-
continue
2200-
if not baseobj.hide:
2201-
if baseobj in yielded:
2202-
continue
2203-
yielded.add(baseobj)
2204-
yield baseobj
2205-
if not recurs:
2206-
continue
2207-
for grandpa in baseobj.ancestors(recurs=True, context=context):
2208-
if grandpa is self:
2209-
# This class is the ancestor of itself.
2210-
break
2211-
if grandpa in yielded:
2193+
with context.restore_path():
2194+
try:
2195+
for baseobj in stmt.infer(context):
2196+
if not isinstance(baseobj, ClassDef):
2197+
if isinstance(baseobj, bases.Instance):
2198+
baseobj = baseobj._proxied
2199+
else:
2200+
continue
2201+
if not baseobj.hide:
2202+
if baseobj in yielded:
2203+
continue
2204+
yielded.add(baseobj)
2205+
yield baseobj
2206+
if not recurs:
22122207
continue
2213-
yielded.add(grandpa)
2214-
yield grandpa
2215-
except exceptions.InferenceError:
2216-
continue
2208+
for grandpa in baseobj.ancestors(recurs=True, context=context):
2209+
if grandpa is self:
2210+
# This class is the ancestor of itself.
2211+
break
2212+
if grandpa in yielded:
2213+
continue
2214+
yielded.add(grandpa)
2215+
yield grandpa
2216+
except exceptions.InferenceError:
2217+
continue
22172218

22182219
def local_attr_ancestors(self, name, context=None):
22192220
"""Iterate over the parents that define the given name.

0 commit comments

Comments
 (0)