Skip to content

Commit 94f87e8

Browse files
Updated docstring and error messages
1 parent acacb0b commit 94f87e8

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

Lib/typing.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3091,10 +3091,10 @@ def __new__(cls, name, bases, ns, total=True, closed=None,
30913091
"""
30923092
for base in bases:
30933093
if type(base) is not _TypedDictMeta and base is not Generic:
3094-
raise TypeError('cannot inherit from both a TypedDict type '
3094+
raise TypeError('Cannot inherit from both a TypedDict type '
30953095
'and a non-TypedDict base class')
30963096
if closed is not None and extra_items is not NoExtraItems:
3097-
raise TypeError(f"Cannot combine closed={closed!r} and extra_items")
3097+
raise TypeError(f"Cannot use both closed and extra_items")
30983098

30993099
if any(issubclass(b, Generic) for b in bases):
31003100
generic_base = (Generic,)
@@ -3274,6 +3274,26 @@ class DatabaseUser(TypedDict):
32743274
id: ReadOnly[int] # the "id" key must not be modified
32753275
username: str # the "username" key can be changed
32763276
3277+
The *closed* argument controls whether the TypedDict allows additional
3278+
non-required items during inheritance and assignability checks.
3279+
If closed=True, the TypedDict is closed to additional items::
3280+
3281+
Point2D = TypedDict('Point2D', {'x': int, 'y': int}, closed=True)
3282+
class Point3D(Point2D):
3283+
z: int # Type checker error
3284+
3285+
Passing closed=False explicitly requests TypedDict's default open behavior.
3286+
If closed is not provided, the behavior is inherited from the superclass.
3287+
3288+
The *extra_items* argument can instead be used to specify the type of
3289+
additional non-required keys::
3290+
3291+
Point2D = TypedDict('Point2D', {'x': int, 'y': int}, extra_items=int)
3292+
class Point3D(Point2D):
3293+
z: int # OK
3294+
label: str # Type checker error
3295+
3296+
See PEP 728 for more information about closed and extra_items.
32773297
"""
32783298
ns = {'__annotations__': dict(fields)}
32793299
module = _caller()

0 commit comments

Comments
 (0)