Skip to content

Commit ba8ae29

Browse files
authored
stubgen: Fix missing total from TypedDict class (#15208)
Fixes #15195
1 parent 7832e1f commit ba8ae29

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

mypy/stubgen.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,6 +1002,7 @@ def visit_class_def(self, o: ClassDef) -> None:
10021002
def get_base_types(self, cdef: ClassDef) -> list[str]:
10031003
"""Get list of base classes for a class."""
10041004
base_types: list[str] = []
1005+
p = AliasPrinter(self)
10051006
for base in cdef.base_type_exprs:
10061007
if isinstance(base, NameExpr):
10071008
if base.name != "object":
@@ -1010,7 +1011,6 @@ def get_base_types(self, cdef: ClassDef) -> list[str]:
10101011
modname = get_qualified_name(base.expr)
10111012
base_types.append(f"{modname}.{base.name}")
10121013
elif isinstance(base, IndexExpr):
1013-
p = AliasPrinter(self)
10141014
base_types.append(base.accept(p))
10151015
elif isinstance(base, CallExpr):
10161016
# namedtuple(typename, fields), NamedTuple(typename, fields) calls can
@@ -1036,13 +1036,16 @@ def get_base_types(self, cdef: ClassDef) -> list[str]:
10361036
# Invalid namedtuple() call, cannot determine fields
10371037
base_types.append("Incomplete")
10381038
elif self.is_typed_namedtuple(base):
1039-
p = AliasPrinter(self)
10401039
base_types.append(base.accept(p))
10411040
else:
10421041
# At this point, we don't know what the base class is, so we
10431042
# just use Incomplete as the base class.
10441043
base_types.append("Incomplete")
10451044
self.import_tracker.require_name("Incomplete")
1045+
for name, value in cdef.keywords.items():
1046+
if name == "metaclass":
1047+
continue # handled separately
1048+
base_types.append(f"{name}={value.accept(p)}")
10461049
return base_types
10471050

10481051
def visit_block(self, o: Block) -> None:

test-data/unit/stubgen.test

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2932,6 +2932,18 @@ class Y(TypedDict, total=False):
29322932
a: int
29332933
b: str
29342934

2935+
[case testTypeddictClassWithKeyword]
2936+
from typing import TypedDict
2937+
class MyDict(TypedDict, total=False):
2938+
foo: str
2939+
bar: int
2940+
[out]
2941+
from typing import TypedDict
2942+
2943+
class MyDict(TypedDict, total=False):
2944+
foo: str
2945+
bar: int
2946+
29352947
[case testTypeddictKeywordSyntax]
29362948
from typing import TypedDict
29372949

0 commit comments

Comments
 (0)