GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Implement support for closed TypedDicts (PEP 728) by alicederyn · Pull Request #21382 · python/mypy

Implement support for closed TypedDicts (PEP 728) #21382

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
ilevkivskyi merged 43 commits into python:master from alicederyn:closed.keyword
Jun 2, 2026

Uh oh!

There was an error while loading. Please reload this page.

Merged

Implement support for closed TypedDicts (PEP 728) #21382

Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
fa59235
Support nested special forms in functional syntax
alicederyn Apr 29, 2026
c06fef7
Subtypes fix: required keys must remain required
alicederyn Apr 22, 2026
4a82d3c
Meet fix: mutable keys must remain mutable
alicederyn Apr 22, 2026
ca099f4
Improve TypedDict meet
alicederyn Apr 22, 2026
f79ba70
Join fix: readonly keys must remain readonly
alicederyn Apr 22, 2026
2ed7992
Improve TypedDict join
alicederyn Apr 22, 2026
ab1a151
Expand TypedDict subclassing tests
alicederyn May 1, 2026
e626c19
Support type refinement in TypedDict subclasses
alicederyn May 1, 2026
d8c619d
Fix TypeAnalyser to retain readonly keys
alicederyn Apr 30, 2026
e7372e4
Narrow on explicitly uninhabited keys
alicederyn Apr 30, 2026
c7115c1
Support parsing TypedDict closed keyword
alicederyn Apr 23, 2026
bce4b17
Implement closed support in TypedDict subtyping
alicederyn Apr 27, 2026
90cb21f
Implement closed support in subclassing
alicederyn Apr 24, 2026
4106525
Implement closed support in join logic
alicederyn Apr 28, 2026
6cc0d63
Implement closed support in meet logic
alicederyn Apr 29, 2026
5bfcc43
Implement closed support in narrowing
alicederyn Apr 30, 2026
d7449df
Implement TypeVar support in narrowing
alicederyn Apr 30, 2026
2621704
Implement closed support in astdiff
alicederyn Apr 30, 2026
34a13de
Implement closed support in stubgen
alicederyn Apr 30, 2026
87ede99
Implement closed support in serialization
alicederyn Apr 30, 2026
bbdd827
Implement closed support in TypeAnalyser
alicederyn Apr 30, 2026
c1ff8b3
Implement closed support in unpacking
alicederyn Apr 30, 2026
ca5a13f
Implement closed support in expandtype
alicederyn May 1, 2026
4d3a7b0
Implement closed support in get method
alicederyn May 1, 2026
877e000
Merge branch 'master' into closed.keyword
alicederyn May 19, 2026
6f3291b
Fix bug with join and Any
alicederyn May 19, 2026
776eb10
Fix bug with meet and Any
alicederyn May 19, 2026
05d3a4b
Refactor common logic into zipall
alicederyn May 19, 2026
022a501
Fix assumption that missing items are ReadOnly
alicederyn May 19, 2026
bd05e88
Allow adding Never items to closed subtypes
alicederyn May 19, 2026
d102505
Reword not-required compatibility error messages
alicederyn May 19, 2026
9279a01
Test case from no error to error
alicederyn May 19, 2026
2d1333c
Delay subtype validation to TypeChecker
alicederyn May 18, 2026
4ee7856
Add closed keyword to TypedDict docs
alicederyn May 20, 2026
8bcc243
Store validation information in TypeInfo
alicederyn May 26, 2026
77e8a59
Link TypedDict branch narrowing to test cases
alicederyn May 26, 2026
4261abb
Remove references to 'MRO'
alicederyn May 26, 2026
8d92a10
Extend subclass tests with subtype checks
alicederyn May 26, 2026
37481a2
Merge branch 'master' into closed.keyword
alicederyn May 26, 2026
42ec16f
Manually wrap strings/comments
alicederyn Jun 1, 2026
5cbb687
Remove unnecessary extra newline
alicederyn Jun 1, 2026
31b5045
Document new TypedDict inheritance rules
alicederyn Jun 1, 2026
fbd2818
Merge branch 'master' into closed.keyword
alicederyn Jun 2, 2026
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view
Prev Previous commit
Next Next commit
Implement closed support in TypedDict subtyping
Continue implementing PEP 728 support with updates to the subtyping
logic. Drop the previous use of 'names_are_wider_than', which will
be difficult to extend to cover `extra_items`, and instead use zipall
to check for key addition/removal alongside the other key-based checks.
  • Loading branch information

Uh oh!

There was an error while loading. Please reload this page.

@alicederyn
alicederyn committed May 3, 2026
commit bce4b17eb6a381b8e2d3ec27b227dc033e373cb3

Some comments aren't visible on the classic Files Changed page.

There are no files selected for viewing

17 changes: 13 additions & 4 deletions 17 mypy/subtypes.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -921,16 +921,25 @@ def visit_typeddict_type(self, left: TypedDictType) -> bool:
elif isinstance(right, TypedDictType):
if left == right:
return True # Fast path
if not left.names_are_wider_than(right):

# A closed type must remain closed
if right.is_closed and not left.is_closed:
return False

# Perform fast key-based checks before recursing into value types
for name in right.items:
for name, l_type, r_type in left.zipall(right):
l_required = name in left.required_keys
r_required = name in right.required_keys
l_mutable = name not in left.readonly_keys
r_mutable = name not in right.readonly_keys
l_mutable = l_type is not None and name not in left.readonly_keys
r_mutable = r_type is not None and name not in right.readonly_keys

# New keys cannot be added to a closed supertype
if r_type is None and right.is_closed:
return False
# Keys cannot be dropped in an open subtype
# as this would implicitly widen the type constraint
if l_type is None and not left.is_closed:
return False
# Required keys must remain required
if r_required and not l_required:
return False
Expand Down
3 changes: 0 additions & 3 deletions 3 mypy/types.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -3102,9 +3102,6 @@ def copy_modified(
items, required_keys, readonly_keys, is_closed, fallback, self.line, self.column
)

def names_are_wider_than(self, other: TypedDictType) -> bool:
return len(other.items.keys() - self.items.keys()) == 0

def zip(self, right: TypedDictType) -> Iterable[tuple[str, Type, Type]]:
left = self
for item_name, left_item_type in left.items.items():
Expand Down
67 changes: 67 additions & 0 deletions 67 test-data/unit/check-typeddict.test
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -5128,6 +5128,73 @@ B = TypedDict('B', {'x': int}, total=True, total=False) # E: Repeated keyword a
[builtins fixtures/dict.pyi]
[typing fixtures/typing-typeddict.pyi]

[case testTypedDictSubtypingClosedMustRemainClosed]
from typing import TypedDict
A = TypedDict('A', {'x': int}, closed=True)
B = TypedDict('B', {'x': int}, closed=True)
C = TypedDict('C', {'x': int}, closed=False)
D = TypedDict('D', {'x': int}, closed=False)
def f(x: A) -> None: pass
def g(x: C) -> None: pass
a: A
b: B
c: C
d: D
f(b)
f(d) # E: Argument 1 to "f" has incompatible type "D"; expected "A"
g(d)
g(b)
[builtins fixtures/dict.pyi]
[typing fixtures/typing-typeddict.pyi]

[case testTypedDictSubtypingCannotAddKeysToAClosedSupertype]
from typing import TypedDict
A = TypedDict('A', {'x': int}, closed=True)
B = TypedDict('B', {'x': int, 'y': int}, closed=True)
C = TypedDict('C', {'x': int})
D = TypedDict('D', {'x': int, 'y': int})
def f(x: A) -> None: pass
def g(x: C) -> None: pass
b: B
d: D
f(b) # E: Argument 1 to "f" has incompatible type "B"; expected "A"
g(d)
[builtins fixtures/dict.pyi]
[typing fixtures/typing-typeddict.pyi]

[case testTypedDictSubtypingCanDropOptionalReadonlyKeysInAClosedSubtype]
from typing import ReadOnly, TypedDict
# Optional, readonly, closed: permitted
A = TypedDict('A', {'x': int, 'y': ReadOnly[int]}, total=False, closed=True)
B = TypedDict('B', {'x': int}, total=False, closed=True)
b: B
def f_a(x: A) -> None: pass
f_a(b)

# Optional, readonly, open: not permitted
C = TypedDict('C', {'x': int, 'y': ReadOnly[int]}, total=False)
D = TypedDict('D', {'x': int}, total=False)
d: D
def f_c(x: C) -> None: pass
f_c(d) # E: Argument 1 to "f_c" has incompatible type "D"; expected "C"

# Optional, mutable, closed: not permitted
E = TypedDict('E', {'x': int, 'y': int}, total=False, closed=True)
F = TypedDict('F', {'x': int}, total=False, closed=True)
f: F
def f_e(x: E) -> None: pass
f_e(f) # E: Argument 1 to "f_e" has incompatible type "F"; expected "E"

# Required, readonly, closed: not permitted
G = TypedDict('G', {'x': int, 'y': ReadOnly[int]}, closed=True)
H = TypedDict('H', {'x': int}, closed=True)
h: H
def f_g(x: G) -> None: pass
f_g(h) # E: Argument 1 to "f_g" has incompatible type "H"; expected "G"

[builtins fixtures/dict.pyi]
[typing fixtures/typing-typeddict.pyi]


[case testTypedDictFinalAndClassVar]
from typing import TypedDict, Final, ClassVar
Expand Down
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.