Skip to content

Commit 417e925

Browse files
miedzinskiilevkivskyi
authored andcommitted
Support docstrings in class based TypedDict (#3749)
1 parent 82cb95c commit 417e925

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

mypy/semanal.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1280,7 +1280,7 @@ def check_typeddict_classdef(self, defn: ClassDef,
12801280
# Still allow pass or ... (for empty TypedDict's).
12811281
if (not isinstance(stmt, PassStmt) and
12821282
not (isinstance(stmt, ExpressionStmt) and
1283-
isinstance(stmt.expr, EllipsisExpr))):
1283+
isinstance(stmt.expr, (EllipsisExpr, StrExpr)))):
12841284
self.fail(TPDICT_CLASS_ERROR, stmt)
12851285
elif len(stmt.lvalues) > 1 or not isinstance(stmt.lvalues[0], NameExpr):
12861286
# An assignment, but an invalid one.

test-data/unit/semanal-typeddict.test

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,24 @@ MypyFile:1(
3434
AssignmentStmt:2(
3535
NameExpr(Point* [__main__.Point])
3636
TypedDictExpr:2(Point)))
37+
38+
[case testTypedDictWithDocString]
39+
from mypy_extensions import TypedDict
40+
class A(TypedDict):
41+
"""foo"""
42+
x: str
43+
[builtins fixtures/dict.pyi]
44+
[out]
45+
MypyFile:1(
46+
ImportFrom:1(mypy_extensions, [TypedDict])
47+
ClassDef:2(
48+
A
49+
BaseTypeExpr(
50+
NameExpr(TypedDict [mypy_extensions.TypedDict]))
51+
ExpressionStmt:3(
52+
StrExpr(foo))
53+
AssignmentStmt:4(
54+
NameExpr(x)
55+
TempNode:-1(
56+
Any)
57+
str?)))

0 commit comments

Comments
 (0)