Skip to content

Commit a0195a9

Browse files
committed
Respond to Eric's comments
1 parent 1b037e9 commit a0195a9

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

pep-0696.rst

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ Abstract
2121
This PEP introduces the concept of type defaults for TypeVars, which act
2222
as defaults for a type parameter when none is specified.
2323

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+
2430
Motivation
2531
----------
2632

@@ -201,8 +207,8 @@ Constraints
201207
~~~~~~~~~~~
202208

203209
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.
206212

207213
.. code:: py
208214
@@ -213,8 +219,24 @@ Function Defaults
213219
~~~~~~~~~~~~~~~~~
214220

215221
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
218240
219241
Implementation
220242
--------------
@@ -387,6 +409,9 @@ convenient, could have a ``TypeVar`` with no default follow a
387409
class Foo(Generic[T, U]):
388410
...
389411
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+
390415
Acknowledgements
391416
----------------
392417

0 commit comments

Comments
 (0)