Skip to content

PEP 696: Changes from Pyright implementation #2844

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Oct 30, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 10 additions & 20 deletions pep-0696.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -348,31 +349,20 @@ 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
--------------

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:

Expand Down