Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
69 commits
Select commit Hold shift + click to select a range
786a436
First docs work
JelleZijlstra May 13, 2023
c1f266e
More words
JelleZijlstra May 15, 2023
040c4fd
Merge remote-tracking branch 'upstream/main' into pep695docs
JelleZijlstra May 19, 2023
e3dfb27
ast changes, fix build
JelleZijlstra May 19, 2023
f5b1d86
the big PEP list
JelleZijlstra May 19, 2023
5798631
dis
JelleZijlstra May 19, 2023
0f76679
default roles
JelleZijlstra May 19, 2023
cb76021
TODOs in compound_stmts
JelleZijlstra May 19, 2023
12c1249
minor
JelleZijlstra May 19, 2023
94a4da2
Remove broken link
JelleZijlstra May 19, 2023
815a021
update dis.dis docs
JelleZijlstra May 19, 2023
17ac785
TypeAlias is deprecated
JelleZijlstra May 19, 2023
1311399
Type aliases should now use the type statement
JelleZijlstra May 19, 2023
123752c
syntax
JelleZijlstra May 19, 2023
9d6e0c0
Merge branch 'main' into pep695docs
JelleZijlstra May 19, 2023
e955a45
Expand typing.rst
JelleZijlstra May 20, 2023
d7f2b55
Finish the type statement
JelleZijlstra May 20, 2023
53c4916
What's New
JelleZijlstra May 20, 2023
edec8c3
Merge remote-tracking branch 'upstream/main' into pep695docs
JelleZijlstra May 20, 2023
bc1c1d2
typo
JelleZijlstra May 20, 2023
b1a843c
FQNs in ast.rst
JelleZijlstra May 20, 2023
e690aff
Merge remote-tracking branch 'upstream/main' into pep695docs
JelleZijlstra May 21, 2023
cc5b51c
Document TypeVar constructor signature, discuss variance
JelleZijlstra May 21, 2023
b0e1ce3
TypeVarTuple signature
JelleZijlstra May 21, 2023
382ff2c
Fix sig
JelleZijlstra May 21, 2023
db94e4f
Document TypeVar attributes, fix indentation
JelleZijlstra May 21, 2023
18b9d63
Fix TypeVarTuple indentation
JelleZijlstra May 21, 2023
b7ba811
Merge branch 'main' into pep695docs
AlexWaygood May 22, 2023
fd1d0a8
s/typeparams/type_params/
JelleZijlstra May 22, 2023
62ba887
Apply suggestions from code review
JelleZijlstra May 22, 2023
200eba1
Reword long dis sentence
JelleZijlstra May 22, 2023
adaf874
Manual changes from Alex review
JelleZijlstra May 22, 2023
4481d6d
Merge remote-tracking branch 'upstream/main' into pep695docs
JelleZijlstra May 22, 2023
365ae7b
Improve ast.TypeAlias text
JelleZijlstra May 22, 2023
96e5d16
Document __type_params__
JelleZijlstra May 22, 2023
0204d54
Add to datamodel.rst
JelleZijlstra May 22, 2023
f426d39
Wording change
JelleZijlstra May 22, 2023
60d7783
Update Doc/library/typing.rst
JelleZijlstra May 22, 2023
63d4d84
Apply suggestions from code review
JelleZijlstra May 22, 2023
fd6751a
fix indentation
JelleZijlstra May 22, 2023
499dae1
Link to generic functions
JelleZijlstra May 22, 2023
b4172c9
Move up variance paragraph
JelleZijlstra May 22, 2023
e9bc4a8
Merge remote-tracking branch 'upstream/main' into pep695docs
JelleZijlstra May 23, 2023
0d37675
Some Alex comments
JelleZijlstra May 23, 2023
c94ee7c
Fix alignment
JelleZijlstra May 23, 2023
1630342
doctest maybe
JelleZijlstra May 23, 2023
aade45a
More Alex suggestions
JelleZijlstra May 23, 2023
57b9992
Feedback from Carl
JelleZijlstra May 23, 2023
7953d2c
Section on lazy eval
JelleZijlstra May 23, 2023
9128c72
More cross-refs
JelleZijlstra May 23, 2023
0511b6a
Can't have nice things
JelleZijlstra May 23, 2023
4fd3f02
Respond to some feedback
JelleZijlstra May 24, 2023
fff0df4
Undo unnecessary typing.rst changes
JelleZijlstra May 24, 2023
ba2a103
Merge remote-tracking branch 'upstream/main' into pep695docs
JelleZijlstra May 24, 2023
1c6807d
more words
JelleZijlstra May 24, 2023
6386475
or
JelleZijlstra May 24, 2023
cd4206c
Update Doc/reference/compound_stmts.rst
JelleZijlstra May 24, 2023
e307177
Merge branch 'main' into pep695docs
AlexWaygood May 25, 2023
d7e1c6c
Apply suggestions from code review
JelleZijlstra May 26, 2023
f878970
More feedback from Alex
JelleZijlstra May 26, 2023
a4bd32a
fixes
JelleZijlstra May 26, 2023
ee8f952
Merge remote-tracking branch 'upstream/main' into pep695docs
JelleZijlstra May 26, 2023
7a709d7
Rewrite and extend some docstrings
JelleZijlstra May 26, 2023
0cfeb92
More comments
JelleZijlstra May 26, 2023
f978766
Docstring fixes
JelleZijlstra May 26, 2023
bba092d
tables
JelleZijlstra May 26, 2023
57ddbef
more fixes
JelleZijlstra May 26, 2023
67acd98
Merge remote-tracking branch 'upstream/main' into pep695docs
JelleZijlstra May 26, 2023
988fc7c
Merge branch 'main' into pep695docs
AlexWaygood May 26, 2023
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
Prev Previous commit
Next Next commit
Some Alex comments
  • Loading branch information
JelleZijlstra committed May 23, 2023
commit 0d37675705e0e6a2e32a94131d49f18777016eac
41 changes: 21 additions & 20 deletions Doc/library/typing.rst
Comment thread
JelleZijlstra marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -1377,16 +1377,10 @@ without the dedicated syntax, as documented below.

Type variable.

Usage::

T = TypeVar('T') # Can be anything
S = TypeVar('S', bound=str) # Can be any subtype of str
A = TypeVar('A', str, bytes) # Must be exactly str or bytes

The syntax for :ref:`generic functions <generic-functions>`,
The preferred way to construct a type variable is via the dedicated syntax
for :ref:`generic functions <generic-functions>`,
:ref:`generic classes <generic-classes>`, and
:ref:`generic type aliases <generic-type-aliases>` can be used to
create type variables::
:ref:`generic type aliases <generic-type-aliases>`::

class Sequence[T]: # T is a TypeVar
...
Expand All @@ -1401,9 +1395,16 @@ without the dedicated syntax, as documented below.
class StrOrBytesSequence[A: (str, bytes)]: # A is a TypeVar constrained to str or bytes
...

However, if desired, reusable type variables can also be constructed manually, like so::

T = TypeVar('T') # Can be anything
S = TypeVar('S', bound=str) # Can be any subtype of str
A = TypeVar('A', str, bytes) # Must be exactly str or bytes

Type variables exist primarily for the benefit of static type
checkers. They serve as the parameters for generic types as well
as for generic function definitions. See :class:`Generic` for more
as for generic function and type alias definitions.
See :class:`Generic` for more
information on generic types. Generic functions work as follows::

def repeat[T](x: T, n: int) -> Sequence[T]:
Expand All @@ -1428,8 +1429,9 @@ without the dedicated syntax, as documented below.
through the :ref:`type parameter syntax <type-params>` and when
Comment thread
JelleZijlstra marked this conversation as resolved.
Outdated
``infer_variance=True`` is passed.
Manually created type variables may be explicitly marked covariant or contravariant by passing
``covariant=True`` or ``contravariant=True``. See :pep:`484` and :pep:`695` for more
details. By default, type variables are invariant.
``covariant=True`` or ``contravariant=True``.
By default, manually created type variables are invariant.
See :pep:`484` and :pep:`695` for more details.

Bound type variables and constrained type variables have different
semantics in several important ways. Using a *bound* type variable means
Expand All @@ -1449,13 +1451,13 @@ without the dedicated syntax, as documented below.
Type variables can be bound to concrete types, abstract types (ABCs or
protocols), and even unions of types::

U = TypeVar('U', bound=str|bytes) # Can be any subtype of the union str|bytes
V = TypeVar('V', bound=SupportsAbs) # Can be anything with an __abs__ method

# Can be anything with an __abs__ method
def print_abs[V: SupportsAbs](arg: V) -> None:
def print_abs[T: SupportsAbs](arg: T) -> None:
print("Absolute value:", abs(arg))

U = TypeVar('U', bound=str|bytes) # Can be any subtype of the union str|bytes
V = TypeVar('V', bound=SupportsAbs) # Can be anything with an __abs__ method

.. _typing-constrained-typevar:

Using a *constrained* type variable, however, means that the ``TypeVar``
Expand All @@ -1469,20 +1471,19 @@ without the dedicated syntax, as documented below.

c = concatenate('one', b'two') # error: type variable 'A' can be either str or bytes in a function call, but not both

At runtime, ``isinstance(x, T)`` will raise :exc:`TypeError`. In general,
:func:`isinstance` and :func:`issubclass` should not be used with types.
At runtime, ``isinstance(x, T)`` will raise :exc:`TypeError`.

.. attribute:: __name__

The name of the type variable.

.. attribute:: __covariant__

Whether the type variable is covariant.
Whether the type var has been explicitly marked as covariant.

.. attribute:: __contravariant__

Whether the type variable is contravariant.
Whether the type var has been explicitly marked as contravariant.

.. attribute:: __infer_variance__

Expand Down
50 changes: 25 additions & 25 deletions Objects/typevarobject.c
Comment thread
AlexWaygood marked this conversation as resolved.
Comment thread
JelleZijlstra marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -443,42 +443,42 @@ static PyMethodDef typevar_methods[] = {
PyDoc_STRVAR(typevar_doc,
"Type variable.\n\
\n\
Usage::\n\
The preferred way to construct a type variable is via the dedicated syntax\n\
for :ref:`generic functions <generic-functions>`,\n\
:ref:`generic classes <generic-classes>`, and\n\
:ref:`generic type aliases <generic-type-aliases>`::\n\
Comment thread
JelleZijlstra marked this conversation as resolved.
Outdated
\n\
T = TypeVar('T') # Can be anything\n\
S = TypeVar('S', bound=str) # Can be any subtype of str\n\
A = TypeVar('A', str, bytes) # Must be str or bytes\n\
class Sequence[T]: # T is a TypeVar\n\
...\n\
Comment thread
JelleZijlstra marked this conversation as resolved.
Outdated
\n\
The syntax for generic functions, classes, and type aliases can\n\
be used to create type variables::\n\
This syntax can also be used to create bound and constrained type\n\
variables::\n\
\n\
class Sequence[T]: # T is a TypeVar\n\
...\n\
class StrSequence[S: str]: # S is a TypeVar bound to str\n\
...\n\
Comment thread
JelleZijlstra marked this conversation as resolved.
Outdated
\n\
Type variables exist primarily for the benefit of static type\n\
checkers. They serve as the parameters for generic types as well\n\
as for generic function definitions. See class Generic for more\n\
information on generic types. Generic functions work as follows:\n\
\n\

ghost May 26, 2023

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't think two line breaks are necessary here

Suggested change
\n\

ghost May 26, 2023

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, fixed.

Also thanks for all the thorough reviews! I am going to merge this now so I can stop worrying about conflicts.

ghost May 26, 2023

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, it's time :)

def repeat[T](x: T, n: int) -> Sequence[T]:\n\
'''Return a list containing n references to x.'''\n\
return [x]*n\n\
class StrOrBytesSequence[A: (str, bytes)]: # A is a TypeVar constrained to str or bytes\n\
...\n\
Comment thread
JelleZijlstra marked this conversation as resolved.
Outdated
\n\
def print_capitalized[S: str](x: S) -> S:\n\
'''Print x capitalized, and return x.'''\n\
print(x.capitalize())\n\
return x\n\
However, if desired, reusable type variables can also be constructed\n\
manually, like so::\n\
\n\
def concatenate[A: (str, bytes)](x: A, y: A) -> A:\n\
'''Add two strings or bytes objects together.'''\n\
return x + y\n\
T = TypeVar('T') # Can be anything\n\
S = TypeVar('S', bound=str) # Can be any subtype of str\n\
A = TypeVar('A', str, bytes) # Must be exactly str or bytes\n\
\n\
Type variables exist primarily for the benefit of static type\n\
checkers. They serve as the parameters for generic types as well\n\
as for generic function and type alias definitions.\n\
\n\
The variance of type variables is inferred by type checkers when they are created\n\
through the type parameter syntax and when\n\
``infer_variance=True`` is passed.\n\
Manually created type variables may be explicitly marked covariant or contravariant by passing\n\
``covariant=True`` or ``contravariant=True``. See PEP 484 and PEP 695 for more\n\
details. By default, type variables are invariant.\n\
Manually created type variables may be explicitly marked covariant or\n\
contravariant by passing ``covariant=True`` or ``contravariant=True``.\n\
By default, manually created type variables are invariant. See PEP 484\n\
and PEP 695 for more details.\n\
");

static PyType_Slot typevar_slots[] = {
Expand Down