Limitations
Mypy will not understand expressions that use variables of type Literal[..]
on a deep level. For example, if you have a variable a
of type Literal[3]
and another variable b
of type Literal[5]
, mypy will infer thata + b
has type int
, not type Literal[8]
.
The basic rule is that literal types are treated as just regular subtypes ofwhatever type the parameter has. For example, Literal[3]
is treated as asubtype of int
and so will inherit all of int
’s methods directly. Thismeans that Literal[3].add
accepts the same arguments and has the samereturn type as int.add
.