@@ -21,6 +21,12 @@ Abstract
21
21
This PEP introduces the concept of type defaults for TypeVars, which act
22
22
as defaults for a type parameter when none is specified.
23
23
24
+ Default type argument support is available in some popular languages
25
+ such as C++, TypeScript, and Rust. A survey of type parameter syntax in
26
+ some common languages has been conducted by the author of :pep: `695 `
27
+ and can be found in its
28
+ :pep: `Appendix A <695#appendix-a-survey-of-type-parameter-syntax >`.
29
+
24
30
Motivation
25
31
----------
26
32
@@ -201,8 +207,8 @@ Constraints
201
207
~~~~~~~~~~~
202
208
203
209
For constrained ``TypeVar ``\ s, the default needs to be one of the
204
- constraints. It would be an error even if it is a subtype of one of the
205
- constraints.
210
+ constraints. A type checker should generate an error even if it is a
211
+ subtype of one of the constraints.
206
212
207
213
.. code :: py
208
214
@@ -213,8 +219,24 @@ Function Defaults
213
219
~~~~~~~~~~~~~~~~~
214
220
215
221
The ``TypeVar ``\ 's default should also be compatible with the
216
- parameter's runtime default if present. But they are erased to
217
- not have defaults when called.
222
+ parameter's runtime default if present, if they aren't a type checker
223
+ should generate an error.
224
+
225
+ .. code :: py
226
+
227
+ def foo (x : DefaultIntT = 12345 ): ... # Valid
228
+ def foo (x : DefaultIntT = " bar" ): ... # Invalid: expected an int for default, got str
229
+
230
+ The defaults are erased when called so, they act like they have no default.
231
+
232
+ .. code :: py
233
+
234
+ def baz (y : DefaultIntT) -> DefaultIntT: ...
235
+ # equivalent to
236
+ def baz (y : T) -> T: ...
237
+
238
+ reveal_type(bar(67890 )) # type is int
239
+ reveal_type(bar(" hello" )) # type is str
218
240
219
241
Implementation
220
242
--------------
@@ -387,6 +409,9 @@ convenient, could have a ``TypeVar`` with no default follow a
387
409
class Foo (Generic[T, U]):
388
410
...
389
411
412
+ This would have also been a breaking change for a small number of cases
413
+ where the code relied on ``Any `` being the implicit default.
414
+
390
415
Acknowledgements
391
416
----------------
392
417
0 commit comments