From 0fbc8f5e4a705b8029904a8730507e46f6620722 Mon Sep 17 00:00:00 2001 From: Praise Tompane Date: Thu, 20 Mar 2025 19:40:11 +0200 Subject: [PATCH] docs: type annotations --- .spellcheck_exceptions_dictionary.txt | 5 ++- .../3_datamodel/{def.txt => 0_def.txt} | 0 .../1_objects_values_and_types/def.txt | 2 ++ 2_standard_library/0_def.txt | 19 +++++++++++- 2_standard_library/4_built_in_types/0_def.xt | 20 ++++++++++++ .../1_type_annotation_types.txt | 26 ++++++++++++++++ .../1_generic_alias_type/1_generic_alias.py | 12 ++++++++ .../1_generic_alias_type/2_functions.py | 29 ++++++++++++++++++ .../1_generic_alias_type/3_classes.py | 19 ++++++++++++ .../4_subscriptable_generic_alias.py | 25 +++++++++++++++ .../2_union_type/1_union_type.py | 17 ++++++++++ dictionary.dic | Bin 6272 -> 6592 bytes 12 files changed, 172 insertions(+), 2 deletions(-) rename 1_core_language/3_datamodel/{def.txt => 0_def.txt} (100%) create mode 100644 2_standard_library/4_built_in_types/0_def.xt create mode 100644 2_standard_library/4_built_in_types/13_type_annotation_types/1_type_annotation_types.txt create mode 100644 4_experiments/2_standard_library/4_built_in_types/13_type_annotation_types/1_generic_alias_type/1_generic_alias.py create mode 100644 4_experiments/2_standard_library/4_built_in_types/13_type_annotation_types/1_generic_alias_type/2_functions.py create mode 100644 4_experiments/2_standard_library/4_built_in_types/13_type_annotation_types/1_generic_alias_type/3_classes.py create mode 100644 4_experiments/2_standard_library/4_built_in_types/13_type_annotation_types/1_generic_alias_type/4_subscriptable_generic_alias.py create mode 100644 4_experiments/2_standard_library/4_built_in_types/13_type_annotation_types/2_union_type/1_union_type.py diff --git a/.spellcheck_exceptions_dictionary.txt b/.spellcheck_exceptions_dictionary.txt index 7e2e202..8afb3f4 100644 --- a/.spellcheck_exceptions_dictionary.txt +++ b/.spellcheck_exceptions_dictionary.txt @@ -73,7 +73,10 @@ iterable init metaclasses hasattr - +GenericAlias +classinfo +isinstance +issubclass # technology: FastAPI diff --git a/1_core_language/3_datamodel/def.txt b/1_core_language/3_datamodel/0_def.txt similarity index 100% rename from 1_core_language/3_datamodel/def.txt rename to 1_core_language/3_datamodel/0_def.txt diff --git a/1_core_language/3_datamodel/1_objects_values_and_types/def.txt b/1_core_language/3_datamodel/1_objects_values_and_types/def.txt index a851227..2c73023 100644 --- a/1_core_language/3_datamodel/1_objects_values_and_types/def.txt +++ b/1_core_language/3_datamodel/1_objects_values_and_types/def.txt @@ -21,6 +21,8 @@ def data model: C/Python API for defining types: https://docs.python.org/3.11/c-api/typeobj.html#c.PyTypeObject.tp_iter - key structures: - PyTypeObject + + - The Python Type System: https://typing.python.org/en/latest/spec/index.html References: Python Software Foundation. 2023. Objects, values and types. https://docs.python.org/3.12/reference/datamodel.html#objects-values-and-types \ No newline at end of file diff --git a/2_standard_library/0_def.txt b/2_standard_library/0_def.txt index 9a05760..018a5f9 100644 --- a/2_standard_library/0_def.txt +++ b/2_standard_library/0_def.txt @@ -1,2 +1,19 @@ def python standard library: - \ No newline at end of file + - formal: ??? + + - in words: ??? + + - plain english: ??? + + - intuition: ??? + + - properties: ??? + + - examples: ??? + + - use cases: ??? + + - proof: ??? + +References: ??? + diff --git a/2_standard_library/4_built_in_types/0_def.xt b/2_standard_library/4_built_in_types/0_def.xt new file mode 100644 index 0000000..1b11ee5 --- /dev/null +++ b/2_standard_library/4_built_in_types/0_def.xt @@ -0,0 +1,20 @@ +def built-in types: + - formal: ??? + + - in words: ??? + + - plain english: ??? + + - intuition: ??? + + - properties: + - all built-in type names in the STL https://docs.python.org/3/library/types.html#module-types + + - examples: ??? + + - use cases: ??? + + - proof: ??? + +References: ??? + diff --git a/2_standard_library/4_built_in_types/13_type_annotation_types/1_type_annotation_types.txt b/2_standard_library/4_built_in_types/13_type_annotation_types/1_type_annotation_types.txt new file mode 100644 index 0000000..dae832e --- /dev/null +++ b/2_standard_library/4_built_in_types/13_type_annotation_types/1_type_annotation_types.txt @@ -0,0 +1,26 @@ +def type_annotation_types + - formal: ??? + + - in words: core built-in types used for type annotations + + - plain english: ??? + + - intuition: ??? + + - properties: + - members: + - Generic Alias Type + - class: types.GenericAlias + - properties: + - not accepted as the 2nd parameter of `isinstance(object, classinfo)` or `issubclass(class, classinfo)` + - Union Type + + - examples: ??? + + - use cases: + - function type annotations + - class type annotations + - proof: ??? + +References: + - The Python Standard Library. 2025. Type Annotation Types. https://docs.python.org/3/library/stdtypes.html#type-annotation-types-generic-alias-union diff --git a/4_experiments/2_standard_library/4_built_in_types/13_type_annotation_types/1_generic_alias_type/1_generic_alias.py b/4_experiments/2_standard_library/4_built_in_types/13_type_annotation_types/1_generic_alias_type/1_generic_alias.py new file mode 100644 index 0000000..a4c9668 --- /dev/null +++ b/4_experiments/2_standard_library/4_built_in_types/13_type_annotation_types/1_generic_alias_type/1_generic_alias.py @@ -0,0 +1,12 @@ +from types import GenericAlias + +print("Creating GenericAlias by subscripting a generic type") +subscription_generic_alias_creation = list[int] +print(f"{subscription_generic_alias_creation=}\n") + +print("Creating GenericAlias directly") +direct_generic_alias_creation = GenericAlias(list, (int)) +print(f"{direct_generic_alias_creation=}\n") + + +assert subscription_generic_alias_creation == direct_generic_alias_creation diff --git a/4_experiments/2_standard_library/4_built_in_types/13_type_annotation_types/1_generic_alias_type/2_functions.py b/4_experiments/2_standard_library/4_built_in_types/13_type_annotation_types/1_generic_alias_type/2_functions.py new file mode 100644 index 0000000..2acc7fc --- /dev/null +++ b/4_experiments/2_standard_library/4_built_in_types/13_type_annotation_types/1_generic_alias_type/2_functions.py @@ -0,0 +1,29 @@ +from collections.abc import Sequence +from typing import TypeVar + + +def generic_function_type_parameter[T](values: Sequence[T]): + return values[0] + + +def generic_function_bound_type_parameter[T: int](values: Sequence[T]): + return values[3] + + +TypeVariable = TypeVar("TypeVariable") +def independently_defined_type_variable(values: Sequence[TypeVariable]): + return values[1] + + +BoundTypeVariable = TypeVar("BoundTypeVariable", bound=float) +def independently_defined_and_bound_type_variable(values: Sequence[BoundTypeVariable]): + return values[2] + + +if __name__ == "__main__": + values = [2, 4, 6, 8] + print(generic_function_type_parameter(values)) + print(independently_defined_type_variable(values)) + float_values = [2.0, 2.1, 4.5, 5.6] + print(independently_defined_and_bound_type_variable(float_values)) + print(generic_function_bound_type_parameter(values)) diff --git a/4_experiments/2_standard_library/4_built_in_types/13_type_annotation_types/1_generic_alias_type/3_classes.py b/4_experiments/2_standard_library/4_built_in_types/13_type_annotation_types/1_generic_alias_type/3_classes.py new file mode 100644 index 0000000..8a398f7 --- /dev/null +++ b/4_experiments/2_standard_library/4_built_in_types/13_type_annotation_types/1_generic_alias_type/3_classes.py @@ -0,0 +1,19 @@ +class IntToIntOrStringDictionary[int, T: (int | str)]: + + def __init__(self,) -> None: + self.values: dict[int, T] = {} + + def add(self, key: int, value: T): + self.values[key] = value + + def __repr__(self): + return ",".join(f"{k}:{v}" for k, v in self.values.items()) + + +if __name__ == "__main__": + v = IntToIntOrStringDictionary() + v.add(1, "a") + v.add(2, 1) + print((type(v))) + print((type(v.values))) + print(v) diff --git a/4_experiments/2_standard_library/4_built_in_types/13_type_annotation_types/1_generic_alias_type/4_subscriptable_generic_alias.py b/4_experiments/2_standard_library/4_built_in_types/13_type_annotation_types/1_generic_alias_type/4_subscriptable_generic_alias.py new file mode 100644 index 0000000..0223d63 --- /dev/null +++ b/4_experiments/2_standard_library/4_built_in_types/13_type_annotation_types/1_generic_alias_type/4_subscriptable_generic_alias.py @@ -0,0 +1,25 @@ +from typing import TypeVar + +""" +dict[int][int] fails, because dict[int] returns a GenericAlias, which is not subscriptable. +""" +try: + a = dict[int][int] + print(type(a)) +except TypeError as e: + print("Expected Error") + print(f"{e} \n") + + +print("subscriptable if TypeVar is used to create the Type Variable\n") +print("Notice the second type parameter is ~T") +T = TypeVar("T") +d = dict[int, T] + +print(d) + +print("Subscripting `d`` to create dict[int, str]\n") +dict_int_to_str = d[str] + +print(dict_int_to_str) +print(type(dict_int_to_str)) diff --git a/4_experiments/2_standard_library/4_built_in_types/13_type_annotation_types/2_union_type/1_union_type.py b/4_experiments/2_standard_library/4_built_in_types/13_type_annotation_types/2_union_type/1_union_type.py new file mode 100644 index 0000000..d3cbd41 --- /dev/null +++ b/4_experiments/2_standard_library/4_built_in_types/13_type_annotation_types/2_union_type/1_union_type.py @@ -0,0 +1,17 @@ +from types import UnionType +from typing import Optional +# clean syntac for typing.Union + +print("Type of int | str") +print(type(int | str)) + + +print("Optional type") +d = int | None +print(type(d)) + +print("Test equivalence to Optional[int]") +assert d == Optional[int] + + +print(isinstance(d, UnionType)) diff --git a/dictionary.dic b/dictionary.dic index f497c1b7e660105ac92eb6621754d41edd20cc61..66e5003119188cd430e74439591d652773c582f3 100644 GIT binary patch delta 3383 zcmYk8e{7U@9mhZPN`Lp-LV*IpR_3O3>Oh>6jcjc#x6ZrgIPQ6*bh6d99BpSyOYZ~rpbmw%{r3}V**6ug#9rf&P}6C=Vpiyx8aXir}g#z@+{$!%j^4m ze!M@Q?~mtuw{!W46?@mush)4{^G1PLb&1(4wPwf12DazRFgK*kqGH;tM)b{QiGkT1 z*qAh10GktL3q_tkBoefR*%BBd{sz%AyHTXhM?~U33R`2Ax1}1yDJBwl zvq-?lMFc+qaoa6oquDYsX0}|!u|lNKts?oii8S~bk%D)KG`dPe_bw6LCYWf>o3&_w z;A#;;hL%9+9_aC`;vNEPL=F_9nn;DmL^4N3gm%NA*=&!9&|VRteIh#JB05is=zLQof4@lnLD)wA zx5ZYo@95aHk?>s^2%HcJJPg}e%BRNaXO9m>?#Z+6Kf>0KjN40&3HyViZ|^z=Ry+fL z%Ck>7Chcy=!0r=So-HENe#|jxM;(1TEmHG>W6Z8NGOGgFJ*$NIh9GSV7&OvA3^peM z+aR*>Lt?to9uirmJtBff9OHJ(vCU41ZD~6PV;rwt;olx-1d@L`e8pK$c-prdckftn zY{>@4xb-=D_NrsjE;^>{e~!K_EGE7^X-i=;7PtIL4ZM`KI{MZoCKJ{#`kw8C9C-VN z$dd0Dxt)IC=-F{c-_AI$x7S3@&_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