During the typing meetup from a week or two ago, I mentioned that we (the
mypy team) were planning on implementing Literal types and would have a
draft of the proposed semantics ready soon. Well, here it is:
It basically combines and elaborates on the discussion from
https://github.com/python/mypy/issues/3062, so hopefully there shouldn't be
too many surprises.
This is probably a little premature in that we've only just barely started
the PoC work in mypy: we'll almost certainly need to tweak some things as
we work through the implementation.
But we're reasonably happy with the proposed semantics and thought it would
be nice to just publish it now so that people can start giving feedback.
(Also, I guess we wanted an excuse to try using this shiny new mailing
For the current code I am writing, this proposal will fulfill my
number one wish, which is exhaustiveness matching for enumerations.
Where previously I used e.g.
Started = 'STARTED'
Finished = 'FINISHED'
def example(status: Status) -> int:
if status is Status.Started:
elif status is Status.Finished:
assert False, 'Impossible'
I could now use
Status2 = Literal['STARTED', 'FINISHED']
def example(status: Status2) -> int:
if status == 'STARTED':
elif status 'FINISHED':
where `assert_never` is as discussed in
Of course, I would prefer to keep using Enums. That would require the
type checker to consider `Status` similarly to
`Literal[Status.Started, Status.Finished]`. This is what TypeScript
does with its "const enums". The PEP does not discuss this particular