@@ -26,58 +26,72 @@ Work in progress notes
26
26
precedent in ``abc ``, namely ``Callable ``, and ``Hashable ``.
27
27
However, attempts at implementing reasonable runtime behavior
28
28
suggests that it may be better to leave ``collections.abc `` alone.
29
+ **Proposed resolution: leave ``collections.abc`` alone. **
29
30
30
31
* ``Var('T') `` is put in ``collections.abc `` as a helper with generics
31
32
and covariance/contravariance in ABCs. May be renamed to ``TypeVar ``.
32
33
The co-/contravariance behavior requires more thinking.
34
+ **Proposed resolution: Use ``TypeVar``, with mypy's semantics mapped:
35
+ ``typevar('T', values=(str, bytes)`` becomes ``TypeVar('T', str, bytes)``. **
33
36
34
37
* ``AnyStr `` (and consequently ``IO ``) smells like basestring in
35
38
disguise, what is the use case for Python 3? Answer: There are
36
39
lots of things in the stdlib that are essentially overloaded as
37
40
str-in-str-out and bytes-in-bytes-out; ``AnyStr `` has exactly the
38
41
right behavior for this (unlike ``Union[str, bytes] ``).
42
+ **Proposed resolution: keep ``AnyStr``, ``IO`` and its subclasses,
43
+ in typing.py. **
39
44
40
- * ``Undefined `` smells like JavaScript, I left it out for now.
45
+ * ``Undefined `` smells like JavaScript, I (Łukasz) left it out for now.
41
46
However, mypy needs it. See https://github.com/ambv/typehinting/issues/20
47
+ **Help??? **
42
48
43
49
* The addition of ``Match `` and ``Pattern `` seems arbitrary and should
44
50
rather be fixed in ``re `` directly, with the types just imported in
45
51
``typing ``. If so, are there any other potentially useful types we'd
46
52
like? See https://github.com/ambv/typehinting/issues/23
53
+ **Proposed resolution: keep these two in typing.py for convenience. **
47
54
48
- * I left out ``overload `` because I don't understand its purpose. If we
55
+ * I (Łukasz) left out ``overload `` because I don't understand its purpose. If we
49
56
need generic dispatch, we should add it to ``functools ``.
50
57
Perhaps ``overload `` should only be allowed in stub modules?
51
58
See https://github.com/ambv/typehinting/issues/14
59
+ **Proposed resolution: support the syntax but only in stub files. **
52
60
53
- * I left out specifying protocols as they are an ABC implementation
61
+ * I (Łukasz) left out specifying protocols as they are an ABC implementation
54
62
detail from the perspective of this PEP. However, they should be
55
63
defined in a separate PEP because making ``__subclasshook__ `` less
56
64
dynamic when possible (which is often) would make ABCs much better
57
65
behaved in static analysis contexts.
58
66
See https://github.com/ambv/typehinting/issues/11
67
+ **Help??? **
59
68
60
69
* Having the last thing in mind, ``IO ``, ``BinaryIO `` and ``TextIO ``
61
70
would simply be new abstract base classes, usable with or without type
62
- hinting
71
+ hinting.
72
+ **Proposed resolution: ``IO`` is generic over ``AnyStr``, the other two
73
+ are concrete (subclassing ``IO[bytes]`` and ``IO[str]`` respectively). **
63
74
64
- * the current implementation of ``*IO `` is quite un-ducktyped (specifies
75
+ * The current implementation of ``*IO `` is quite un-ducktyped (specifies
65
76
both reading and writing as one protocol)
77
+ **Help??? **
66
78
67
- * I left out ``cast `` because it can be more consistently expressed as::
79
+ * I (Łukasz) left out ``cast `` because it can be more consistently expressed as::
68
80
69
81
bad_typed_list = [1, 2, 3] # type: list
70
82
...
71
83
well_typed_list = bad_typed_list # type: List[int]
72
84
73
85
See https://github.com/ambv/typehinting/issues/15
86
+ **Proposed resolution: add ``cast(type, value)`` back in.**
74
87
75
- * I removed ``mypy `` from the listed "existing approaches" since we
76
- basically describe what mypy is
88
+ * I (Łukasz) removed ``mypy `` from the listed "existing approaches" since we
89
+ basically describe what mypy is. ** Proposed resolution: fine. **
77
90
78
- * I thought if introducing optional contravariance/invariance to ``Var ``
91
+ * I (Łukasz) thought if introducing optional contravariance/invariance to ``Var ``
79
92
has merit but I'm undecided; definitely complicates the type checker.
80
93
See https://github.com/ambv/typehinting/issues/2
94
+ **Help??? **
81
95
82
96
83
97
Changes to MyPy coming from this proposal
@@ -90,10 +104,12 @@ Changes to MyPy coming from this proposal
90
104
https://github.com/JukkaL/mypy/issues/539 and
91
105
https://github.com/ambv/typehinting/issues/1 and
92
106
https://github.com/ambv/typehinting/issues/2 and maybe others.
93
-
107
+ ** Proposed resolution: ``TypeVar`` (see above). **
94
108
95
109
* ``Union `` behaves differently, it holds the defined types in order
96
110
to be able to actually respond to issubclass.
111
+ **Proposed resolution: This doesn't affect mypy; I do want this for
112
+ typing.py (also for generic types). **
97
113
98
114
* ``typing.Function `` becomes ``typing.Callable ``, which is equivalent
99
115
to ``collections.abc.Callable ``. (Has now been implemented in mypy.)
@@ -108,23 +124,29 @@ Open issues
108
124
* State of the art: should we list decorator-based approaches
109
125
(PyContracts?) and docstring-based approaches?
110
126
111
- * should we recommend the use of ABCs over builtin types when possible?
127
+ * Should we recommend the use of ABCs over builtin types when possible?
112
128
113
- * should we provide type shortcuts so that MutableMapping is not 3.5x
129
+ * Should we provide type shortcuts so that MutableMapping is not 3.5x
114
130
longer to type than dict?
115
131
116
- * is multiple dispatch using type hints in scope for this PEP?
132
+ * Is multiple dispatch using type hints in scope for this PEP?
133
+ **Proposed resolution: let's wait, allow @overload only in stubs. **
117
134
118
- * should we mention platform- or version-specific typing? Guido mentioned
135
+ * Should we mention platform- or version-specific typing? Guido mentioned
119
136
Windows vs. POSIX during the Skype meeting.
137
+ **Proposed resolution: let typing.py export a few specific constants;
138
+ PY3, PY2, PY3_x (for x in 2, 3, 4, 5). **
120
139
121
- * it would be useful to have a ``Subclass[] `` factory that would be
140
+ * It would be useful to have a ``Subclass[] `` factory that would be
122
141
equivalent to "any class that has the all the following classes in its
123
142
MRO". Maybe we'd want to rename union to ``Any[] `` and this new
124
143
proposed type to ``All[] ``? See https://github.com/ambv/typehinting/issues/18
144
+ **Proposed resolution: let's just have ``Union[T1, T2, ...]`` and ``Any``. **
125
145
126
146
* Callable signature definition should be explained and support all
127
- valid function signatures
147
+ valid function signatures.
148
+ **Proposed resolution: ``Callable[[T1, T2, ...], Tr]``. **
128
149
129
150
* What about all other collections available in the ``collections ``
130
151
module?
152
+ **Proposed resolution: make Generic types retain their argument type(s). **
0 commit comments