Skip to content

Commit e3dcd51

Browse files
committed
Fix a crash caused by SimpleValue not implementing instantiate().
PiperOrigin-RevId: 412116246
1 parent 30d84c3 commit e3dcd51

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

pytype/abstract/abstract.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,10 +396,17 @@ def _unique_parameters(self):
396396
parameters.extend(self.instance_type_parameters.values())
397397
return parameters
398398

399+
def instantiate(self, node, container=None):
400+
return Instance.from_value(self, container).to_variable(node)
401+
399402

400403
class Instance(SimpleValue):
401404
"""An instance of some object."""
402405

406+
@classmethod
407+
def from_value(cls, value, container=None):
408+
return cls(value, value.ctx, container=container)
409+
403410
def __init__(self, cls, ctx, container=None):
404411
super().__init__(cls.name, ctx)
405412
self.cls = cls
@@ -2694,7 +2701,7 @@ def instantiate(self, node, container=None):
26942701
# When the analyze_x methods in CallTracer instantiate classes in
26952702
# preparation for analysis, often there is no frame on the stack yet, or
26962703
# the frame is a SimpleFrame with no opcode.
2697-
return self._to_instance(container).to_variable(node)
2704+
return super().instantiate(node, container)
26982705

26992706
def __repr__(self):
27002707
return "InterpreterClass(%s)" % self.name

pytype/abstract/class_mixin.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -381,9 +381,6 @@ def call_init(self, node, value, args):
381381
node = self._call_method(node, value, method, function.Args(()))
382382
return node
383383

384-
def _to_instance(self, container):
385-
return self._instance_abstract_class(self, self.ctx, container=container)
386-
387384
def _new_instance(self, container, node, args):
388385
"""Returns a (possibly cached) instance of 'self'."""
389386
del args # unused
@@ -394,7 +391,8 @@ def _new_instance(self, container, node, args):
394391
key = node
395392
assert key
396393
if key not in self._instance_cache:
397-
self._instance_cache[key] = self._to_instance(container)
394+
self._instance_cache[key] = self._instance_abstract_class.from_value(
395+
self, container)
398396
return self._instance_cache[key]
399397

400398
def _check_not_instantiable(self):

0 commit comments

Comments
 (0)