Skip to content

Commit f411a8b

Browse files
pytype authorscopybara-github
authored andcommitted
Don't crash when a TypedDict definition is recursive.
PiperOrigin-RevId: 860115788
1 parent 2d87a2a commit f411a8b

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

pytype/overlays/typed_dict.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,9 @@ def make_class_from_pyi(self, cls_name, pytd_cls):
221221
)
222222

223223
for c in pytd_cls.constants:
224-
typ = self.ctx.convert.constant_to_value(c.type)
224+
# The field types may refer back to the class being built.
225+
with self.ctx.allow_recursive_convert():
226+
typ = self.ctx.convert.constant_to_value(c.type)
225227
props.add(c.name, typ, total)
226228

227229
# Process base classes and generate the __init__ signature.

pytype/tests/test_typed_dict.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,24 @@ class Bar:
784784
""",
785785
)
786786

787+
def test_recursive(self):
788+
with self.DepTree([(
789+
"foo.pyi",
790+
"""
791+
from typing import Union, Optional
792+
from typing_extensions import TypedDict
793+
class Foo(TypedDict, total=False):
794+
name: str
795+
nested: 'Foo'
796+
""",
797+
)]):
798+
self.CheckWithErrors("""
799+
import foo
800+
foo.Foo(name='foo', a=1) # wrong-keyword-args
801+
# 'nested' cannot be checked because Foo is recursive.
802+
foo.Foo(name='foo', nested=45)
803+
""")
804+
787805
def test_total_false(self):
788806
with self.DepTree([
789807
(

0 commit comments

Comments
 (0)