Skip to content

Commit d18fe8c

Browse files
committed
Make sure we consistently use the same dict class for types.
We have a `dict` special builtin that is used to implement `dict(...)` calls, but it was sneaking into ParameterizedClass base classes as well. PiperOrigin-RevId: 600882569
1 parent 0fd504c commit d18fe8c

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

pytype/overlays/special_builtins.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,11 @@ def call(self, node, func, args, alias_map=None):
781781
else:
782782
return super().call(node, func, args, alias_map)
783783

784+
def get_special_attribute(self, node, name, valself):
785+
# For doing something like getting the __getitem__ attribute to subscript
786+
# dict, we want to use the real dict type.
787+
return self.ctx.convert.dict_type.get_special_attribute(node, name, valself)
788+
784789

785790
class Type(BuiltinClass, mixin.HasSlots):
786791

pytype/tests/test_typevar2.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,6 +1057,23 @@ def f(x: T) -> T:
10571057
f("oops") # wrong-arg-types
10581058
""")
10591059

1060+
@test_utils.skipBeforePy((3, 9), "subscripting builtins.dict is new in 3.9")
1061+
def test_builtin_dict_constraint(self):
1062+
with self.DepTree([("foo.pyi", """
1063+
from typing import TypeVar
1064+
T = TypeVar('T', int, dict[str, int])
1065+
class C:
1066+
def f(self, x: T) -> T: ...
1067+
""")]):
1068+
self.Check("""
1069+
import foo
1070+
from typing import TypeVar
1071+
T = TypeVar('T', int, dict[str, int])
1072+
class C(foo.C):
1073+
def f(self, x: T) -> T:
1074+
return x
1075+
""")
1076+
10601077

10611078
if __name__ == "__main__":
10621079
test_base.main()

0 commit comments

Comments
 (0)