diff --git a/pep-0696.rst b/pep-0696.rst index 757e2a9e43f..938aa06ec19 100644 --- a/pep-0696.rst +++ b/pep-0696.rst @@ -140,7 +140,7 @@ future, this might be possible (see `Interaction with PEP ``ParamSpec`` defaults are defined using the same syntax as ``TypeVar`` \ s but use a ``list`` or ``tuple`` of types or an ellipsis -literal "``...``". +literal "``...``" or another in-scope ``ParamSpec`` (see :ref:`scoping-rules`). .. code-block:: py @@ -155,7 +155,8 @@ literal "``...``". ''''''''''''''''''''''''' ``TypeVarTuple`` defaults are defined using the same syntax as -``TypeVar`` \ s but use an unpacked tuple of types instead of a single type. +``TypeVar`` \ s but use an unpacked tuple of types instead of a single type +or another in-scope ``TypeVarTuple`` (see :ref:`scoping-rules`). .. code-block:: py @@ -348,18 +349,9 @@ subtype of one of the constraints. Function Defaults ''''''''''''''''' -``TypeVarLike``\ s currently can only be used where a parameter can go unsolved. - -.. code-block:: py - - def foo(a: DefaultStrT | None = None) -> DefaultStrT: ... - - reveal_type(foo(1)) # type is int - reveal_type(foo()) # type is str - -If they are used where the parameter type is known, the defaults -should just be ignored and a type checker can emit a warning. - +``TypeVarLike``\ s currently are not supported in the signatures of +functions as ensuring the ``default`` is returned in every code path +where the ``TypeVarLike`` can go unsolved is too hard to implement. Implementation -------------- @@ -367,12 +359,10 @@ Implementation At runtime, this would involve the following changes to the ``typing`` module. -- a new class ``_DefaultMixin`` needs to be added which is a superclass - of ``TypeVar``, ``ParamSpec``, and ``TypeVarTuple``. - - - the type passed to default would be available as a ``__default__`` - attribute and a sentinel would need to be added that can be - checked against to see if it was supplied. +- The classes ``TypeVar``, ``ParamSpec``, and ``TypeVarTuple`` should + expose the type passed to ``default``. This would be available as + a ``__default__`` attribute, which would be ``None`` if no argument + is passed and ``NoneType`` if ``default=None``. The following changes would be required to both ``GenericAlias``\ es: