Skip to content

Commit 38b744e

Browse files
Merge pull request #10 from praisetompane/13_type_annotation_types
docs: type annotations
2 parents 0915fdc + 0fbc8f5 commit 38b744e

12 files changed

Lines changed: 172 additions & 2 deletions

File tree

.spellcheck_exceptions_dictionary.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,10 @@ iterable
7373
init
7474
metaclasses
7575
hasattr
76-
76+
GenericAlias
77+
classinfo
78+
isinstance
79+
issubclass
7780

7881
# technology:
7982
FastAPI

1_core_language/3_datamodel/1_objects_values_and_types/def.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ def data model:
2121
C/Python API for defining types: https://docs.python.org/3.11/c-api/typeobj.html#c.PyTypeObject.tp_iter
2222
- key structures:
2323
- PyTypeObject
24+
25+
- The Python Type System: https://typing.python.org/en/latest/spec/index.html
2426

2527
References:
2628
Python Software Foundation. 2023. Objects, values and types. https://docs.python.org/3.12/reference/datamodel.html#objects-values-and-types

2_standard_library/0_def.txt

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,19 @@
11
def python standard library:
2-
2+
- formal: ???
3+
4+
- in words: ???
5+
6+
- plain english: ???
7+
8+
- intuition: ???
9+
10+
- properties: ???
11+
12+
- examples: ???
13+
14+
- use cases: ???
15+
16+
- proof: ???
17+
18+
References: ???
19+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
def built-in types:
2+
- formal: ???
3+
4+
- in words: ???
5+
6+
- plain english: ???
7+
8+
- intuition: ???
9+
10+
- properties:
11+
- all built-in type names in the STL https://docs.python.org/3/library/types.html#module-types
12+
13+
- examples: ???
14+
15+
- use cases: ???
16+
17+
- proof: ???
18+
19+
References: ???
20+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
def type_annotation_types
2+
- formal: ???
3+
4+
- in words: core built-in types used for type annotations
5+
6+
- plain english: ???
7+
8+
- intuition: ???
9+
10+
- properties:
11+
- members:
12+
- Generic Alias Type
13+
- class: types.GenericAlias
14+
- properties:
15+
- not accepted as the 2nd parameter of `isinstance(object, classinfo)` or `issubclass(class, classinfo)`
16+
- Union Type
17+
18+
- examples: ???
19+
20+
- use cases:
21+
- function type annotations
22+
- class type annotations
23+
- proof: ???
24+
25+
References:
26+
- The Python Standard Library. 2025. Type Annotation Types. https://docs.python.org/3/library/stdtypes.html#type-annotation-types-generic-alias-union
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from types import GenericAlias
2+
3+
print("Creating GenericAlias by subscripting a generic type")
4+
subscription_generic_alias_creation = list[int]
5+
print(f"{subscription_generic_alias_creation=}\n")
6+
7+
print("Creating GenericAlias directly")
8+
direct_generic_alias_creation = GenericAlias(list, (int))
9+
print(f"{direct_generic_alias_creation=}\n")
10+
11+
12+
assert subscription_generic_alias_creation == direct_generic_alias_creation
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from collections.abc import Sequence
2+
from typing import TypeVar
3+
4+
5+
def generic_function_type_parameter[T](values: Sequence[T]):
6+
return values[0]
7+
8+
9+
def generic_function_bound_type_parameter[T: int](values: Sequence[T]):
10+
return values[3]
11+
12+
13+
TypeVariable = TypeVar("TypeVariable")
14+
def independently_defined_type_variable(values: Sequence[TypeVariable]):
15+
return values[1]
16+
17+
18+
BoundTypeVariable = TypeVar("BoundTypeVariable", bound=float)
19+
def independently_defined_and_bound_type_variable(values: Sequence[BoundTypeVariable]):
20+
return values[2]
21+
22+
23+
if __name__ == "__main__":
24+
values = [2, 4, 6, 8]
25+
print(generic_function_type_parameter(values))
26+
print(independently_defined_type_variable(values))
27+
float_values = [2.0, 2.1, 4.5, 5.6]
28+
print(independently_defined_and_bound_type_variable(float_values))
29+
print(generic_function_bound_type_parameter(values))
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class IntToIntOrStringDictionary[int, T: (int | str)]:
2+
3+
def __init__(self,) -> None:
4+
self.values: dict[int, T] = {}
5+
6+
def add(self, key: int, value: T):
7+
self.values[key] = value
8+
9+
def __repr__(self):
10+
return ",".join(f"{k}:{v}" for k, v in self.values.items())
11+
12+
13+
if __name__ == "__main__":
14+
v = IntToIntOrStringDictionary()
15+
v.add(1, "a")
16+
v.add(2, 1)
17+
print((type(v)))
18+
print((type(v.values)))
19+
print(v)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from typing import TypeVar
2+
3+
"""
4+
dict[int][int] fails, because dict[int] returns a GenericAlias, which is not subscriptable.
5+
"""
6+
try:
7+
a = dict[int][int]
8+
print(type(a))
9+
except TypeError as e:
10+
print("Expected Error")
11+
print(f"{e} \n")
12+
13+
14+
print("subscriptable if TypeVar is used to create the Type Variable\n")
15+
print("Notice the second type parameter is ~T")
16+
T = TypeVar("T")
17+
d = dict[int, T]
18+
19+
print(d)
20+
21+
print("Subscripting `d`` to create dict[int, str]\n")
22+
dict_int_to_str = d[str]
23+
24+
print(dict_int_to_str)
25+
print(type(dict_int_to_str))

0 commit comments

Comments
 (0)