3
3
The ` typing ` module has various aliases to other stdlib classes. These are a legacy feature, but
4
4
still need to be supported by a type checker.
5
5
6
- ## Currently unsupported
6
+ ## Correspondence
7
7
8
- Support for most of these symbols is currently a TODO :
8
+ All of the following symbols can be mapped one-to-one with the actual type :
9
9
10
10
``` py
11
11
import typing
12
12
13
13
def f (
14
- a : typing.List,
15
- b : typing.List[int ],
16
- c : typing.Dict,
17
- d : typing.Dict[int , str ],
18
- e : typing.DefaultDict,
19
- f : typing.DefaultDict[str , int ],
20
- g : typing.Set,
21
- h : typing.Set[int ],
22
- i : typing.FrozenSet,
23
- j : typing.FrozenSet[str ],
24
- k : typing.OrderedDict,
25
- l : typing.OrderedDict[int , str ],
26
- m : typing.Counter,
27
- n : typing.Counter[int ],
14
+ list_bare : typing.List,
15
+ list_parametrized : typing.List[int ],
16
+ dict_bare : typing.Dict,
17
+ dict_parametrized : typing.Dict[int , str ],
18
+ set_bare : typing.Set,
19
+ set_parametrized : typing.Set[int ],
20
+ frozen_set_bare : typing.FrozenSet,
21
+ frozen_set_parametrized : typing.FrozenSet[str ],
22
+ chain_map_bare : typing.ChainMap,
23
+ chain_map_parametrized : typing.ChainMap[int ],
24
+ counter_bare : typing.Counter,
25
+ counter_parametrized : typing.Counter[int ],
26
+ default_dict_bare : typing.DefaultDict,
27
+ default_dict_parametrized : typing.DefaultDict[str , int ],
28
+ deque_bare : typing.Deque,
29
+ deque_parametrized : typing.Deque[str ],
30
+ ordered_dict_bare : typing.OrderedDict,
31
+ ordered_dict_parametrized : typing.OrderedDict[int , str ],
28
32
):
29
- reveal_type(a) # revealed: @Todo(Unsupported or invalid type in a type expression)
30
- reveal_type(b) # revealed: @Todo(typing.List alias)
31
- reveal_type(c) # revealed: @Todo(Unsupported or invalid type in a type expression)
32
- reveal_type(d) # revealed: @Todo(typing.Dict alias)
33
- reveal_type(e) # revealed: @Todo(Unsupported or invalid type in a type expression)
34
- reveal_type(f) # revealed: @Todo(typing.DefaultDict[] alias)
35
- reveal_type(g) # revealed: @Todo(Unsupported or invalid type in a type expression)
36
- reveal_type(h) # revealed: @Todo(typing.Set alias)
37
- reveal_type(i) # revealed: @Todo(Unsupported or invalid type in a type expression)
38
- reveal_type(j) # revealed: @Todo(typing.FrozenSet alias)
39
- reveal_type(k) # revealed: @Todo(Unsupported or invalid type in a type expression)
40
- reveal_type(l) # revealed: @Todo(typing.OrderedDict alias)
41
- reveal_type(m) # revealed: @Todo(Unsupported or invalid type in a type expression)
42
- reveal_type(n) # revealed: @Todo(typing.Counter[] alias)
33
+ reveal_type(list_bare) # revealed: list
34
+ reveal_type(list_parametrized) # revealed: list
35
+
36
+ reveal_type(dict_bare) # revealed: dict
37
+ reveal_type(dict_parametrized) # revealed: dict
38
+
39
+ reveal_type(set_bare) # revealed: set
40
+ reveal_type(set_parametrized) # revealed: set
41
+
42
+ reveal_type(frozen_set_bare) # revealed: frozenset
43
+ reveal_type(frozen_set_parametrized) # revealed: frozenset
44
+
45
+ reveal_type(chain_map_bare) # revealed: ChainMap
46
+ reveal_type(chain_map_parametrized) # revealed: ChainMap
47
+
48
+ reveal_type(counter_bare) # revealed: Counter
49
+ reveal_type(counter_parametrized) # revealed: Counter
50
+
51
+ reveal_type(default_dict_bare) # revealed: defaultdict
52
+ reveal_type(default_dict_parametrized) # revealed: defaultdict
53
+
54
+ reveal_type(deque_bare) # revealed: deque
55
+ reveal_type(deque_parametrized) # revealed: deque
56
+
57
+ reveal_type(ordered_dict_bare) # revealed: OrderedDict
58
+ reveal_type(ordered_dict_parametrized) # revealed: OrderedDict
43
59
```
44
60
45
61
## Inheritance
@@ -49,35 +65,63 @@ The aliases can be inherited from. Some of these are still partially or wholly T
49
65
``` py
50
66
import typing
51
67
52
- class A (typing .Dict ): ...
68
+ # ###################
69
+ # ## Built-ins
70
+
71
+ class ListSubclass (typing .List ): ...
53
72
54
73
# TODO : should have `Generic`, should not have `Unknown`
55
- reveal_type(A.__mro__ ) # revealed: tuple[Literal[A], Literal[dict], Unknown, Literal[object]]
74
+ # revealed: tuple[Literal[ListSubclass], Literal[list], Unknown, Literal[object]]
75
+ reveal_type(ListSubclass.__mro__ )
56
76
57
- class B (typing .List ): ...
77
+ class DictSubclass (typing .Dict ): ...
58
78
59
79
# TODO : should have `Generic`, should not have `Unknown`
60
- reveal_type(B.__mro__ ) # revealed: tuple[Literal[B], Literal[list], Unknown, Literal[object]]
80
+ # revealed: tuple[Literal[DictSubclass], Literal[dict], Unknown, Literal[object]]
81
+ reveal_type(DictSubclass.__mro__ )
61
82
62
- class C (typing .Set ): ...
83
+ class SetSubclass (typing .Set ): ...
63
84
64
85
# TODO : should have `Generic`, should not have `Unknown`
65
- reveal_type(C.__mro__ ) # revealed: tuple[Literal[C], Literal[set], Unknown, Literal[object]]
86
+ # revealed: tuple[Literal[SetSubclass], Literal[set], Unknown, Literal[object]]
87
+ reveal_type(SetSubclass.__mro__ )
66
88
67
- class D (typing .FrozenSet ): ...
89
+ class FrozenSetSubclass (typing .FrozenSet ): ...
68
90
69
91
# TODO : should have `Generic`, should not have `Unknown`
70
- reveal_type(D.__mro__ ) # revealed: tuple[Literal[D], Literal[frozenset], Unknown, Literal[object]]
92
+ # revealed: tuple[Literal[FrozenSetSubclass], Literal[frozenset], Unknown, Literal[object]]
93
+ reveal_type(FrozenSetSubclass.__mro__ )
94
+
95
+ # ###################
96
+ # ## `collections`
97
+
98
+ class ChainMapSubclass (typing .ChainMap ): ...
99
+
100
+ # TODO : Should be (ChainMapSubclass, ChainMap, MutableMapping, Mapping, Collection, Sized, Iterable, Container, Generic, object)
101
+ # revealed: tuple[Literal[ChainMapSubclass], Literal[ChainMap], Unknown, Literal[object]]
102
+ reveal_type(ChainMapSubclass.__mro__ )
103
+
104
+ class CounterSubclass (typing .Counter ): ...
105
+
106
+ # TODO : Should be (CounterSubclass, Counter, dict, MutableMapping, Mapping, Collection, Sized, Iterable, Container, Generic, object)
107
+ # revealed: tuple[Literal[CounterSubclass], Literal[Counter], Unknown, Literal[object]]
108
+ reveal_type(CounterSubclass.__mro__ )
71
109
72
- class E (typing .DefaultDict ): ...
110
+ class DefaultDictSubclass (typing .DefaultDict ): ...
73
111
74
- reveal_type(E.__mro__ ) # revealed: tuple[Literal[E], @Todo(Support for more typing aliases as base classes), Literal[object]]
112
+ # TODO : Should be (DefaultDictSubclass, defaultdict, dict, MutableMapping, Mapping, Collection, Sized, Iterable, Container, Generic, object)
113
+ # revealed: tuple[Literal[DefaultDictSubclass], Literal[defaultdict], Unknown, Literal[object]]
114
+ reveal_type(DefaultDictSubclass.__mro__ )
75
115
76
- class F (typing .OrderedDict ): ...
116
+ class DequeSubclass (typing .Deque ): ...
77
117
78
- reveal_type(F.__mro__ ) # revealed: tuple[Literal[F], @Todo(Support for more typing aliases as base classes), Literal[object]]
118
+ # TODO : Should be (DequeSubclass, deque, MutableSequence, Sequence, Reversible, Collection, Sized, Iterable, Container, Generic, object)
119
+ # revealed: tuple[Literal[DequeSubclass], Literal[deque], Unknown, Literal[object]]
120
+ reveal_type(DequeSubclass.__mro__ )
79
121
80
- class G (typing .Counter ): ...
122
+ class OrderedDictSubclass (typing .OrderedDict ): ...
81
123
82
- reveal_type(G.__mro__ ) # revealed: tuple[Literal[G], @Todo(Support for more typing aliases as base classes), Literal[object]]
124
+ # TODO : Should be (OrderedDictSubclass, OrderedDict, dict, MutableMapping, Mapping, Collection, Sized, Iterable, Container, Generic, object)
125
+ # revealed: tuple[Literal[OrderedDictSubclass], Literal[OrderedDict], Unknown, Literal[object]]
126
+ reveal_type(OrderedDictSubclass.__mro__ )
83
127
```
0 commit comments