3.1. Type Annotation Scalar

  • Also known as: "type annotations", "type hints", "gradual typing"

  • Types are not required, and never will be

  • Good IDE will give you hints

  • Types are used extensively in system libraries

  • More and more books and documentations use types

  • Introduced in Python 3.5

  • Since Python 3.5: PEP 484 -- Type Hints

  • Since Python 3.6: PEP 526 -- Syntax for Variable Annotations

  • Since Python 3.8: PEP 544 -- Protocols: Structural subtyping (static duck typing)

  • Since Python 3.9: PEP 585 -- Type Hinting Generics In Standard Collections

  • Since Python 3.10: PEP 604 -- Allow writing union types as X | Y

  • To type check use: mypy, pyre-check, pytypes

Types are not required, and never will be. -- Guido van Rossum, Python initiator, core developer, former BDFL

It should be emphasized that Python will remain a dynamically typed language, and the authors have no desire to ever make type hints mandatory, even by convention. -- Python Software Foundation

../../_images/typeannotation-timeline.png

Figure 3.3. Timeline of changes to type annotations from Python 3.0 to now 1

3.1.1. Int

  • Used to inform static type checker that the variable should be int

>>> data: int = 0
>>> data: int = 1
>>> data: int = -1

3.1.2. Float

  • Used to inform static type checker that the variable should be float

>>> data: float = 0.0
>>> data: float = 1.23
>>> data: float = -1.23

3.1.3. Str

  • Used to inform static type checker that the variable should be str

>>> data: str = ''
>>> data: str = 'Mark Watney'

3.1.4. Bool

  • Used to inform static type checker that the variable should be bool

>>> data: bool = True
>>> data: bool = False

3.1.5. None

  • Used to inform static type checker that the variable should be None

>>> data: None = None

3.1.6. Union

  • Used to inform static type checker that the variable should either X or Y

  • Since Python 3.10: PEP 604 -- Allow writing union types as X | Y

  • int | str == str | int

>>> data: int | float = 1337
>>> data: int | float = 1.337

Result of this expression would then be valid in isinstance() and issubclass():

>>> isinstance(1337, int|float)
True

3.1.7. Optional

  • Used to inform static type checker that the variable should be X or None

  • int | None == None | int

>>> number: int | None = 1337
>>> number: int | None = None

Result of this expression would then be valid in isinstance() and issubclass():

>>> isinstance(1337, int|None)
True

3.1.8. Aliases

  • Used to make types more readable

>>> number = int | float
>>>
>>> age: number = 10
>>> age: number = 10.5

3.1.9. Final

  • Used to inform static type checker the value should not change

  • Used to define constants

  • Since Python 3.8: PEP 591 -- Adding a final qualifier to typing

>>> from typing import Final
>>>
>>>
>>> m: Final[int] = 1
>>> km: Final[int] = 1000 * m
>>> from typing import Final
>>>
>>>
>>> second: Final[int] = 1
>>> minute: Final[int] = 60 * second
>>> hour: Final[int] = 60 * minute
>>> day: Final[int] = 24 * hour

3.1.10. Errors

  • Types are not Enforced

  • This code will run without any problems

  • Types are not required, and never will be

  • Although mypy or pyre-check will throw error

>>> name: int = 'Mark Watney'

3.1.11. Use Case - 0x01

>>> firstname: str = 'Melissa'
>>> lastname: str = 'Lewis'
>>> age: int | None = None

3.1.12. Further Reading

3.1.13. References

1

Briggs, J. Type Annotations in Python. Year: 2021. Retrieved: 2022-04-08. URL: https://towardsdatascience.com/type-annotations-in-python-d90990b172dc