roam/20201007142751-python_typing.org

67 lines
6.4 KiB
Org Mode
Raw Permalink Normal View History

2021-07-29 22:51:04 +00:00
:PROPERTIES:
:ID: 5cb1f706-0162-4e6d-9cd8-dc6af3ae68cc
:END:
2020-10-08 21:22:51 +00:00
#+title: Python Typing
2021-08-06 17:21:42 +00:00
[[id:cda9c620-fec5-4549-b979-22fc06819d77][Python]] does not require nor enforce strict typing, and instead takes a dynamic
2021-07-29 22:51:04 +00:00
approach using [[id:c50025bd-6942-4a78-b448-ea6fda2925fd][Duck Typing]].
2020-10-08 21:22:51 +00:00
2021-07-29 22:51:04 +00:00
[[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]]).
2020-10-08 21:22:51 +00:00
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
2020-10-14 13:26:40 +00:00
- Per [[https://www.python.org/dev/peps/pep-0285/][PEP-285]], Python's =boolean= inherits from =int=, hence =True + True = 2=.
2020-10-09 05:56:12 +00:00
* Relevant PEPs
2023-09-29 15:46:20 +00:00
| PEP | Name | Python Version |
|------+-----------------------------------------------------------------------+----------------|
| [[https://www.python.org/dev/peps/pep-3107/][3107]] | Function Annotations | 3.0 |
| [[https://www.python.org/dev/peps/pep-0443/][443]] | Single-dispatch generic functions | 3.4 |
| [[https://www.python.org/dev/peps/pep-0482/][482]] | Literature Overview for Type Hints | N/A |
| [[https://www.python.org/dev/peps/pep-0483/][483]] | The Theory of Type Hints | N/A |
| [[https://www.python.org/dev/peps/pep-0484/][484]] | Type Hints | 3.5 |
| [[https://www.python.org/dev/peps/pep-0526/][526]] | Syntax for Variable Annotations | 3.6 |
| [[https://www.python.org/dev/peps/pep-0544/][544]] | Protocols: Structural subtyping (static duck typing) | 3.8 |
| [[https://www.python.org/dev/peps/pep-0557/][557]] | Data Classes | 3.7 |
| [[https://www.python.org/dev/peps/pep-0560/][560]] | Core support for typing module and generic types | 3.7 |
| [[https://www.python.org/dev/peps/pep-0561/][561]] | Distributing and Packaging Type Information | 3.7 |
| [[https://www.python.org/dev/peps/pep-0563/][563]] | Postponed Evaluation of Annotations | 3.7 |
| [[https://www.python.org/dev/peps/pep-0585/][585]] | Type Hinting Generics In Standard Collections | 3.9 |
| [[https://www.python.org/dev/peps/pep-0586/][586]] | Literal Types | 3.8 |
| [[https://www.python.org/dev/peps/pep-0589/][589]] | TypedDict: Type Hints for Dictionaries with a Fixed Set of Keys | 3.8 |
| [[https://www.python.org/dev/peps/pep-0591/][591]] | Adding a final qualifier to typing | 3.8 |
| [[https://www.python.org/dev/peps/pep-0593/][593]] | Flexible function and variable annotations | 3.9 |
| [[https://www.python.org/dev/peps/pep-0604/][604]] | Allow writing union types as X Y | 3.10 |
| [[https://www.python.org/dev/peps/pep-0612/][612]] | Parameter Specification Variables | 3.10 |
| [[https://www.python.org/dev/peps/pep-0613/][613]] | Explicit Type Aliases | 3.10 |
| [[https://www.python.org/dev/peps/pep-0634/][634]] | Structural Pattern Matching: Specification | 3.10 |
| [[https://www.python.org/dev/peps/pep-0635/][635]] | Structural Pattern Matching: Motivation and Rationale | 3.10 |
| [[https://www.python.org/dev/peps/pep-0636/][636]] | Structural Pattern Matching: Tutorial | 3.10 |
| [[https://www.python.org/dev/peps/pep-0638/][638]] | Syntactic Macros | |
| [[https://www.python.org/dev/peps/pep-0640/][640]] | Unused variable syntax | 3.10 |
| [[https://www.python.org/dev/peps/pep-0646/][646]] | Variadic Generics | 3.11 |
| [[https://www.python.org/dev/peps/pep-0647/][647]] | User-Defined Type Guards | 3.10 |
2023-10-25 14:27:21 +00:00
| [[https://peps.python.org/pep-0649/][649]] | Deferred Evaluation Of Annotations Using Descriptors | 3.13 |
2023-09-29 15:46:20 +00:00
| [[https://www.python.org/dev/peps/pep-0654/][654]] | Exception Groups and except* | 3.11 |
| [[https://peps.python.org/pep-0655/][655]] | Marking individual TypedDict items as required or potentially-missing | 3.11 |
| [[https://peps.python.org/pep-0673/][673]] | Self Type | 3.11 |
| [[https://peps.python.org/pep-0675/][675]] | Arbitrary Literal String Type | 3.11 |
| [[https://peps.python.org/pep-0692/][692]] | Using TypedDict for more precise **kwargs typing | 3.12 |
| [[https://peps.python.org/pep-0695/][695]] | Type Parameter Syntax | 3.12 |
| [[https://peps.python.org/pep-0698/][698]] | Override Decorator for Static Typing | 3.12 |