8000 gh-103921: Document PEP 695 by JelleZijlstra · Pull Request #104642 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content
10000

gh-103921: Document PEP 695 #104642

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 69 commits into from
May 26, 2023
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
Respond to some feedback
  • Loading branch information
JelleZijlstra committed May 24, 2023
commit 4fd3f02182e4bf89c21ac65dd6c7789cd7936246
2 changes: 1 addition & 1 deletion Doc/library/typing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1361,7 +1361,7 @@ without the dedicated syntax, as documented below.
Here the brackets after the function name indicate a
:ref:`generic function <generic-functions>`.

For backwards compatibility reasons, generic classes can also be
For backwards compatibility, generic classes can also be
declared by explicitly inheriting from
``Generic``. In this case, the type parameters must be declared
separately::
Expand Down
31 changes: 15 additions & 16 deletions Doc/reference/compound_stmts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1651,7 +1651,7 @@ non-generic counterparts.
Type parameters are declared in square brackets (``[]``) immediately
after the name of the function, class, or type alias. The type parameters
are accessible within the scope of the generic object, but not elsewhere.
Thus, after a declaration ``def func[T](): ...``, the name ``T`` is not in
Thus, after a declaration ``def func[T](): pass``, the name ``T`` is not in
the module scope. Below, the semantics of generic objects are described
with more precision. The scope of type parameters is modeled with a special
function (technically, an :ref:`annotation scope <annotation-scopes>`) that
Expand All @@ -1663,7 +1663,7 @@ attribute listing their type parameters.
Type parameters come in three kinds:

* :data:`typing.TypeVar`, introduced by a plain name (e.g., ``T``). Semantically, this
stands for a single type.
represents a single type to a type checker.
* :data:`typing.TypeVarTuple`, introduced by a name prefixed with a single
asterisk (e.g., ``*Ts``). Semantically, this stands for a tuple of any
number of types.
Expand All @@ -1672,8 +1672,7 @@ Type parameters come in three kinds:

:data:`typing.TypeVar` declarations can define *bounds* and *constraints* with
a colon (``:``) followed by an expression. A single expression after the colon
indicates a bound (e.g. ``T: int``). The expression should be a type (though this
is not enforced at runtime, only by static type checkers) and semantically, this means
indicates a bound (e.g. ``T: int``). Semantically, this means
that the :data:`!typing.TypeVar` can only represent types that are a subtype of
this bound. A parenthesized tuple of expressions after the colon indicates a
set of constraints (e.g. ``T: (str, bytes)``). Each member of the tuple should be a
Expand Down Expand Up @@ -1715,16 +1714,16 @@ Generic functions are declared as follows::

This syntax is equivalent to::

def' TYPE_PARAMS_OF_func():
annotation-def TYPE_PARAMS_OF_func():
T = typing.TypeVar("T")
def func(arg: T): ...
func.__type_params__ = (T,)
return func
func = TYPE_PARAMS_OF_func()

Here ``def'`` indicates an :ref:`annotation scope <annotation-scopes>`. (Two
other liberties are taken in the translation: no function is actually bound
to the name ``TYPE_PARAMS_OF_func``, and the syntax does not go through
Here ``annotation-def`` indicates an :ref:`annotation scope <annotation-scopes>`,
which is not actually bound to any name at runtime. (One
other liberties are taken in the translation: the syntax does not go through
attribute access on the :mod:`typing` module, but creates an instance of
:data:`typing.TypeVar` directly.)

Expand All @@ -1744,9 +1743,9 @@ Except for the :ref:`lazy evaluation <lazy-evaluation>` of the

DEFAULT_OF_arg = some_default

def' TYPE_PARAMS_OF_func():
annotation-def TYPE_PARAMS_OF_func():

def' BOUND_OF_T():
annotation-def BOUND_OF_T():
return int
# In reality, BOUND_OF_T() is evaluated only on demand.
T = typing.TypeVar("T", bound=BOUND_OF_T())
Expand Down Expand Up @@ -1775,15 +1774,15 @@ Generic classes are declared as follows::

This syntax is equivalent to::

def' TYPE_PARAMS_OF_Bag():
annotation-def TYPE_PARAMS_OF_Bag():
T = typing.TypeVar("T")
class Bag(typing.Generic[T]):
__type_params__ = (T,)
...
return Bag
Bag = TYPE_PARAMS_OF_Bag()

Here again ``def'`` (not a real keyword) indicates an
Here again ``annotation-def`` (not a real keyword) indicates an
:ref:`annotation scope <annotation-scopes>`, and the name
``TYPE_PARAMS_OF_Bag`` is not actually bound at runtime.

Expand All @@ -1798,7 +1797,7 @@ by this example::

This is equivalent to::

def' TYPE_PARAMS_OF_Bag():
annotation-def TYPE_PARAMS_OF_Bag():
T = typing.TypeVar("T")
class Bag(Base[T], typing.Generic[T], arg=T):
__type_params__ = (T,)
Expand All @@ -1818,16 +1817,16 @@ The :keyword:`type` statement can also be used to create a generic type alias::
Except for the :ref:`lazy evaluation <lazy-evaluation>` of the value,
this is equivalent to::

def' TYPE_PARAMS_OF_ListOrSet():
annotation-def TYPE_PARAMS_OF_ListOrSet():
T = typing.TypeVar("T")

def' VALUE_OF_ListOrSet():
annotation-def VALUE_OF_ListOrSet():
return list[T] | set[T]
# In reality, the value is lazily evaluated
return typing.TypeAliasType("ListOrSet", VALUE_OF_ListOrSet(), type_params=(T,))
ListOrSet = TYPE_PARAMS_OF_ListOrSet()

Here, ``def'`` (not a real keyword) indicates an
Here, ``annotation-def`` (not a real keyword) indicates an
:ref:`annotation scope <annotation-scopes>`. The capitalized names
like ``TYPE_PARAMS_OF_ListOrSet`` are not actually bound at runtime.

Expand Down
4 changes: 2 additions & 2 deletions Doc/reference/simple_stmts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1033,11 +1033,11 @@ For example, the following statement creates a type alias::

This code is roughly equivalent to::

def' VALUE_OF_Point():
annotation-def VALUE_OF_Point():
return tuple[float, float]
Point = typing.TypeAliasType("Point", VALUE_OF_Point())

``def'`` indicates an :ref:`annotation scope <annotation-scopes>`, which behaves
``annotation-def`` indicates an :ref:`annotation scope <annotation-scopes>`, which behaves
mostly like a function, but with several small differences. The value of the
type alias is evaluated in the annotation scope. It is not evaluated when the
type alias is created, but only when the value is accessed through the type alias's
Expand Down
0