Skip to content

Commit d6c50f5

Browse files
Drop runtime error in PEP 705 implementation (#333)
See https://discuss.python.org/t/pep-705-read-only-typeddict-items/37867/10
1 parent ff530f5 commit d6c50f5

File tree

3 files changed

+6
-9
lines changed

3 files changed

+6
-9
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Unreleased
22

3+
- Drop runtime error when a mutable `TypedDict` key overrides a read-only
4+
one. Type checkers should still flag this as an error. Patch by Jelle
5+
Zijlstra.
36
- Speedup `issubclass()` checks against simple runtime-checkable protocols by
47
around 6% (backporting https://github.com/python/cpython/pull/112717, by Alex
58
Waygood).

src/test_typing_extensions.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4143,13 +4143,12 @@ class Child2(Base2):
41434143
self.assertEqual(Child1.__readonly_keys__, frozenset({'a'}))
41444144
self.assertEqual(Child1.__mutable_keys__, frozenset({'b'}))
41454145

4146-
def test_cannot_make_mutable_key_readonly(self):
4146+
def test_make_mutable_key_readonly(self):
41474147
class Base(TypedDict):
41484148
a: int
41494149

4150-
with self.assertRaises(TypeError):
4151-
class Child(Base):
4152-
a: ReadOnly[int]
4150+
class Child(Base):
4151+
a: ReadOnly[int] # type checker error, but allowed at runtime
41534152

41544153
def test_can_make_readonly_key_mutable(self):
41554154
class Base(TypedDict):

src/typing_extensions.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -942,11 +942,6 @@ def __new__(cls, name, bases, ns, *, total=True):
942942
else:
943943
optional_keys.add(annotation_key)
944944
if ReadOnly in qualifiers:
945-
if annotation_key in mutable_keys:
946-
raise TypeError(
947-
f"Cannot override mutable key {annotation_key!r}"
948-
" with read-only key"
949-
)
950945
readonly_keys.add(annotation_key)
951946
else:
952947
mutable_keys.add(annotation_key)

0 commit comments

Comments
 (0)