Skip to content
Prev Previous commit
Next Next commit
double backtick fixes
  • Loading branch information
jorenham committed Aug 2, 2021
commit a717746c7e0eba04452a19e2632d744af92a0eed
28 changes: 14 additions & 14 deletions pep-9999.rst
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ All of these issues can be solved by overloading the ``+`` and ``-``
prefix operators of ``typing.TypeVar`` instances, so that ``+T`` is an
alias for a covariant type variable, and ``-T`` for a contravariant
one. These variance operators should only be used within the parameter
list of `typing.Generic` and `typing.Protocol` when subclassing them.
list of ``typing.Generic`` and ``typing.Protocol`` when subclassing them.

This syntax is practically identical to type parameter variance in the
Scala programming language [1]_.
Expand All @@ -96,11 +96,11 @@ Specification
=============

The new type variable variance syntax should be used within the generic
parameter list of `typing.Generic` or `typing.Protocol` base classes,
parameter list of ``typing.Generic`` or ``typing.Protocol`` base classes,
and only there. It is not allowed to be used on type variables that
are already marked as covariant or contravariant, either with
`covariant=True`, `contravariant=True` or with the new `+T` or `-T`
syntax.
``covariant=True``, ``contravariant=True`` or with the new ``+T`` or
``-T`` syntax.

Simplified Syntax
-----------------
Expand All @@ -117,7 +117,7 @@ Instead of specifying the variance on the typevar itself::
def __le__(self, other: T_contra): ...


The new syntax uses the `+` and `-` prefix operators to specify
The new syntax uses the ``+`` and ``-`` prefix operators to specify
covariant and contravariant generic types inline::

T = typing.TypeVar('T')
Expand All @@ -133,7 +133,7 @@ Differences with current syntax
-------------------------------

The new typevar operators return a transparent wrapper around the
original type variable, which can be accessed with the `__orig__`
original type variable, which can be accessed with the ``__orig__``
Comment thread
jorenham marked this conversation as resolved.
Outdated
attribute on the returned wrapper. e.g.::

(+T).__orig__ is T
Expand All @@ -144,12 +144,12 @@ attribute on the returned wrapper. e.g.::
(+T).__bound__ is T.__bound__


Thus, type variables defined with `covariant=True` and
`contravariant=True`, are not equivalent to `+T` and `-T`.
Thus, type variables defined with ``covariant=True`` and
``contravariant=True``, are not equivalent to ``+T`` and ``-T``.


`+T` and `-T` are not valid type annotations, and should only be used
within the generic type parameter list of generic classes. e.g.::
``+T`` and ``-T`` are not valid type annotations, and should only be
used within the generic type parameter list of generic classes. e.g.::

class Spam(typing.Generic[+KT]): ...
class Eggs(typing.Protocol[KT, +VT]): ...
Expand All @@ -171,20 +171,20 @@ For more detauls about discussions, see links below:

- `Discussion in python/typing <https://github.com/python/typing/issues/813>`_

1. Using `T_co = +TypeVar('T_co')` instead of `T_co = TypeVar('T_co', covariant=True)`
1. Using ``T_co = +TypeVar('T_co')`` instead of ``T_co = TypeVar('T_co', covariant=True)``
--------------------------------------------------------------------------------------

PROS:

- This requires minimal changes to the syntax
- Replaces the need to type `covariant=True` or `contravariant=True`
- Replaces the need to type ``covariant=True`` or ``contravariant=True``
with a concise operator.


CONS:

- The `+` and `-` copy the type variable, but type variables should
be unique.
- The ``+`` and ``-`` copy the type variable, but type variables
should be unique.
- It is not obvious what to do with the name of the type variable.
- Co- and contravariance are properties of the generic class, not of
the individual type variables.
Expand Down