We can't find the internet
Attempting to reconnect
Something went wrong!
Attempting to reconnect
← Interfaces with Protocols and ABCs step 11 of 26
Accept Three Event Types with a Read-Only TypedDict
Your app logs click, error and ping events, and each kind is a TypedDict.
One function, summarise, formats any of them. It reads name, seq and
detail, and never changes them. Its parameter type, EventLike, is too
strict. Submit the starter, and mypy rejects two of the three events:
L48:43 [list-item]
List item 1 has incompatible type "ErrorEvent"; expected "EventLike"
L48:50 [list-item]
List item 2 has incompatible type "PingEvent"; expected "EventLike"
A writable key must match exactly
Every key in a TypedDict is writable unless you say otherwise. So code that
receives an EventLike may assign event["name"] = "click". It may also
delete event["detail"], because detail is NotRequired, as in the
previous exercise. mypy accepts an event type only if both changes keep it
valid:
PingEventhasname: Literal["ping"]. Writing"click"throughEventLikewould leave aPingEventwhosenameis"click".ErrorEventhas a requireddetail. Deleting it throughEventLikewould leave anErrorEventwithout one.ClickEventmatches every key exactly, so it already fits. Its extraxandykeys do not matter.
You met this rule in the read-only protocol exercise earlier in this track.
A writable member must match exactly, but a read-only member can accept a
narrower type. TypedDict follows the same rule. ReadOnly marks a key
that code cannot assign or delete through this type. A read-only str key
accepts Literal["ping"]. A read-only NotRequired key also accepts a
required one, because mypy rejects deleting a read-only key.
What to write
Import ReadOnly from typing, and change only EventLike:
nameis a required, read-onlystr.seqis a required, read-onlyint.detailis a read-onlystrthat may be missing.
Keep ClickEvent, ErrorEvent, PingEvent, summarise and solve as
they are. The starter already returns the right strings, so strict mypy is
what this exercise grades. It must accept the line
events: list[EventLike] = [click, error, ping].
What the driver returns
For each pair from seqs and details, solve builds a click, an error
and a ping event, in that order, and returns their summaries. Each summary
is name#seq(detail). A missing detail shows as -, and an empty one
stays empty. For seqs=[2] and details=[""], the result is
["click#2(-)", "error#2()", "ping#2(pong)"]. Empty lists give [], and
lists of different lengths raise ValueError.
ReadOnly is a rule for the type checker only. At runtime each event is
still a plain dict, and code that holds it under another type can change
it.
Your code, saved here
Sign in to access your accepted solutions and saved draft history. You can keep reading the lesson without an account.
Sign in to your accountStuck?
Python reference solution
Sign in to track your attempts toward unlocking the reference solution.