:PROPERTIES: :ID: 5cb1f706-0162-4e6d-9cd8-dc6af3ae68cc :END: #+title: Python Typing Python does not require nor enforce strict typing, and instead takes a dynamic approach using [[id:c50025bd-6942-4a78-b448-ea6fda2925fd][Duck Typing]]. [[id:59b10eea-ac54-4452-ae6c-b5e003bb0c22][Gradual Typing]] support for Python is provided via type hints as described in [[https://www.python.org/dev/peps/pep-0484/][PEP-484]] (which covers [[id:b458dd6c-6627-4e6b-b0ec-b6dbd030a83a][Nominal Typing]]) and expanded upon in [[https://www.python.org/dev/peps/pep-0544/][PEP-544]] (which covers [[id:c16eb203-290f-4644-8f18-725ffad15e92][Structural Typing]]). Variables, function arguments, and return values can have their types annotated, e.g. "variable x has type integer" or "function y takes a string and returns a list of dictionaries". While these are optional and not checked at run-time, they serve as documentation and can be used by various type checkers to ensure program correctness. * Available Type Checkers - Mypy :: http://mypy-lang.org/ - Pyright (Microsoft) :: https://github.com/Microsoft/pyright - Pyre (Facebook) :: https://pyre-check.org/ - Pytype (Google) :: https://google.github.io/pytype/ * Limitations - Type hints do not guarantee purity. - Type hints are not checked at run-time. * Fun facts - Per [[https://www.python.org/dev/peps/pep-0285/][PEP-285]], Python's =boolean= inherits from =int=, hence =True + True = 2=. * Relevant PEPs - [[https://www.python.org/dev/peps/pep-3107/][PEP 3107 -- Function Annotations]] - [[https://www.python.org/dev/peps/pep-0443/][PEP 443 -- Single-dispatch generic functions]] - [[https://www.python.org/dev/peps/pep-0482/][PEP 482 -- Literature Overview for Type Hints]] - [[https://www.python.org/dev/peps/pep-0483/][PEP 483 -- The Theory of Type Hints]] - [[https://www.python.org/dev/peps/pep-0484/][PEP 484 -- Type Hints]] - [[https://www.python.org/dev/peps/pep-0526/][PEP 526 -- Syntax for Variable Annotations]] - [[https://www.python.org/dev/peps/pep-0544/][PEP 544 -- Protocols: Structural subtyping (static duck typing)]] - [[https://www.python.org/dev/peps/pep-0557/][PEP 557 -- Data Classes]] - [[https://www.python.org/dev/peps/pep-0560/][PEP 560 -- Core support for typing module and generic types]] - [[https://www.python.org/dev/peps/pep-0561/][PEP 561 -- Distributing and Packaging Type Information]] - [[https://www.python.org/dev/peps/pep-0563/][PEP 563 -- Postponed Evaluation of Annotations]] - [[https://www.python.org/dev/peps/pep-0585/][PEP 585 -- Type Hinting Generics In Standard Collections]] - [[https://www.python.org/dev/peps/pep-0586/][PEP 586 -- Literal Types]] - [[https://www.python.org/dev/peps/pep-0589/][PEP 589 -- TypedDict: Type Hints for Dictionaries with a Fixed Set of Keys]] - [[https://www.python.org/dev/peps/pep-0591/][PEP 591 -- Adding a final qualifier to typing]] - [[https://www.python.org/dev/peps/pep-0593/][PEP 593 -- Flexible function and variable annotations]] - [[https://www.python.org/dev/peps/pep-0604/][PEP 604 -- Allow writing union types as X | Y]] - [[https://www.python.org/dev/peps/pep-0612/][PEP 612 -- Parameter Specification Variables]] - [[https://www.python.org/dev/peps/pep-0613/][PEP 613 -- Explicit Type Aliases]] - [[https://www.python.org/dev/peps/pep-0634/][PEP 634 -- Structural Pattern Matching: Specification]] - [[https://www.python.org/dev/peps/pep-0635/][PEP 635 -- Structural Pattern Matching: Motivation and Rationale]] - [[https://www.python.org/dev/peps/pep-0636/][PEP 636 -- Structural Pattern Matching: Tutorial]] - [[https://www.python.org/dev/peps/pep-0638/][PEP 638 -- Syntactic Macros]] - [[https://www.python.org/dev/peps/pep-0640/][PEP 640 -- Unused variable syntax]] - [[https://www.python.org/dev/peps/pep-0646/][PEP 646 -- Variadic Generics]] - [[https://www.python.org/dev/peps/pep-0647/][PEP 647 -- User-Defined Type Guards]]