Declaring literal variables

You must explicitly add an annotation to a variable to declare that it hasa literal type:

  1. a: Literal[19] = 19
  2. reveal_type(a) # Revealed type is 'Literal[19]'

In order to preserve backwards-compatibility, variables without this annotationare not assumed to be literals:

  1. b = 19
  2. reveal_type(b) # Revealed type is 'int'

If you find repeating the value of the variable in the type hint to be tedious,you can instead change the variable to be Final (see Final names, methods and classes):

  1. from typing import Final, Literal
  2.  
  3. def expects_literal(x: Literal[19]) -> None: pass
  4.  
  5. c: Final = 19
  6.  
  7. reveal_type(c) # Revealed type is 'Literal[19]?'
  8. expects_literal(c) # ...and this type checks!

If you do not provide an explicit type in the Final, the type of c becomescontext-sensitive: mypy will basically try “substituting” the original assignedvalue whenever it’s used before performing type checking. This is why the revealedtype of c is Literal[19]?: the question mark at the end reflects thiscontext-sensitive nature.

For example, mypy will type check the above program almost as if it were written like so:

  1. from typing import Final, Literal
  2.  
  3. def expects_literal(x: Literal[19]) -> None: pass
  4.  
  5. reveal_type(19)
  6. expects_literal(19)

This means that while changing a variable to be Final is not quite the same thingas adding an explicit Literal[…] annotation, it often leads to the same effectin practice.

The main cases where the behavior of context-sensitive vs true literal types differ arewhen you try using those types in places that are not explicitly expecting a Literal[…].For example, compare and contrast what happens when you try appending these types to a list:

  1. from typing import Final, Literal
  2.  
  3. a: Final = 19
  4. b: Literal[19] = 19
  5.  
  6. # Mypy will chose to infer List[int] here.
  7. list_of_ints = []
  8. list_of_ints.append(a)
  9. reveal_type(list_of_ints) # Revealed type is 'List[int]'
  10.  
  11. # But if the variable you're appending is an explicit Literal, mypy
  12. # will infer List[Literal[19]].
  13. list_of_lits = []
  14. list_of_lits.append(b)
  15. reveal_type(list_of_lits) # Revealed type is 'List[Literal[19]]'