Skip to content

Commit 2e78dd1

Browse files
committed
Fix spurious not-callable error for typing.OrderedDict.
I also added a __repr__ for AnnotationContainer because the fact that it was using the __repr__ of the AnnotationClass base class initially threw off my debugging attempts. Resolves #1196. PiperOrigin-RevId: 446039108
1 parent dafb129 commit 2e78dd1

File tree

4 files changed

+21
-5
lines changed

4 files changed

+21
-5
lines changed

pytype/abstract/_instance_base.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from pytype.abstract import function
1010

1111
log = logging.getLogger(__name__)
12+
_isinstance = abstract_utils._isinstance # pylint: disable=protected-access
1213

1314

1415
class SimpleValue(_base.BaseValue):
@@ -99,15 +100,18 @@ def merge_instance_type_parameter(self, node, name, value):
99100
else:
100101
self.instance_type_parameters[name] = value
101102

102-
def call(self, node, func, args, alias_map=None):
103-
binding = func if self == func.data else self.to_binding(node)
103+
def _call_helper(self, node, obj, binding, args):
104+
obj_binding = binding if obj == binding.data else obj.to_binding(node)
104105
node, var = self.ctx.attribute_handler.get_attribute(
105-
node, self, "__call__", binding)
106+
node, obj, "__call__", obj_binding)
106107
if var is not None and var.bindings:
107108
return function.call_function(self.ctx, node, var, args)
108109
else:
109110
raise function.NotCallable(self)
110111

112+
def call(self, node, func, args, alias_map=None):
113+
return self._call_helper(node, self, func, args)
114+
111115
def argcount(self, node):
112116
node, var = self.ctx.attribute_handler.get_attribute(
113117
node, self, "__call__", self.to_binding(node))

pytype/abstract/_typing.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ def __init__(self, name, ctx, base_cls):
7575
super().__init__(name, ctx)
7676
self.base_cls = base_cls
7777

78+
def __repr__(self):
79+
return "AnnotationContainer(%s)" % self.name
80+
7881
def _sub_annotation(
7982
self, annot: _base.BaseValue, subst: Mapping[str, _base.BaseValue]
8083
) -> _base.BaseValue:
@@ -300,6 +303,9 @@ def _build_value(self, node, inner, ellipses):
300303
self.ctx.errorlog.invalid_annotation(self.ctx.vm.frames, e.annot, e.error)
301304
return self.ctx.convert.unsolvable
302305

306+
def call(self, node, func, args, alias_map=None):
307+
return self._call_helper(node, self.base_cls, func, args)
308+
303309

304310
class TypeParameter(_base.BaseValue):
305311
"""Parameter of a type."""

pytype/tests/test_errors2.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ def f(x: str):
6666
def test_wrong_brackets(self):
6767
_, errors = self.InferWithErrors("""
6868
from typing import List
69-
def f(x: List(str)): # not-callable[e]
69+
def f(x: List(str)): # invalid-annotation[e]
7070
pass
7171
""")
72-
self.assertErrorSequences(errors, {"e": ["List"]})
72+
self.assertErrorSequences(errors, {"e": ["<instance of list>"]})
7373

7474
def test_interpreter_class_printing(self):
7575
_, errors = self.InferWithErrors("""

pytype/tests/test_typing2.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,12 @@ def f(x: OrderedDict[str, int]): ...
761761
f(collections.OrderedDict(a=0))
762762
""")
763763

764+
def test_instantiate_ordered_dict(self):
765+
self.Check("""
766+
from typing import OrderedDict
767+
OrderedDict()
768+
""")
769+
764770
def test_typed_dict(self):
765771
with file_utils.Tempdir() as d:
766772
d.create_file("foo.pyi", """

0 commit comments

Comments
 (0)