Skip to content
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.

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

Drop runtime error in PEP 705 implementation #333

Merged
merged 1 commit into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Unreleased

- Drop runtime error when a mutable `TypedDict` key overrides a read-only
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is back to front I think @JelleZijlstra ? May be confusing

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, thanks

one. Type checkers should still flag this as an error. Patch by Jelle
Zijlstra.
- Speedup `issubclass()` checks against simple runtime-checkable protocols by
around 6% (backporting https://github.com/python/cpython/pull/112717, by Alex
Waygood).
Expand Down
7 changes: 3 additions & 4 deletions src/test_typing_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4142,13 +4142,12 @@ class Child2(Base2):
self.assertEqual(Child1.__readonly_keys__, frozenset({'a'}))
self.assertEqual(Child1.__mutable_keys__, frozenset({'b'}))

def test_cannot_make_mutable_key_readonly(self):
def test_make_mutable_key_readonly(self):
class Base(TypedDict):
a: int

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

def test_can_make_readonly_key_mutable(self):
class Base(TypedDict):
Expand Down
5 changes: 0 additions & 5 deletions src/typing_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -942,11 +942,6 @@ def __new__(cls, name, bases, ns, *, total=True):
else:
optional_keys.add(annotation_key)
if ReadOnly in qualifiers:
if annotation_key in mutable_keys:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you not want to replace this with a mutable_keys.discard? I think that would be symmetric with how this code handles other "illegal" overrides, e.g. with Required.

raise TypeError(
f"Cannot override mutable key {annotation_key!r}"
" with read-only key"
)
readonly_keys.add(annotation_key)
else:
mutable_keys.add(annotation_key)
Expand Down
Loading