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
More words
  • Loading branch information
JelleZijlstra committed May 15, 2023
commit c1f266e919e04eb43ab281da3134972dca37efeb
77 changes: 31 additions & 46 deletions Doc/reference/compound_stmts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1634,6 +1634,16 @@ generic over a type variable. This information is primarily used by static
type checkers, and at runtime, generic objects behave much like their
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
Comment thread
JelleZijlstra marked this conversation as resolved.
Outdated
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>`,
represented with the pseudo-keyword ``def'``) that
Comment thread
JelleZijlstra marked this conversation as resolved.
Outdated
wraps the creation of the generic object.

Generic functions, classes, and type aliases have a :attr:`__type_params__`
Comment thread
AlexWaygood marked this conversation as resolved.
Outdated
attribute listing their type parameters.

Expand Down Expand Up @@ -1681,42 +1691,6 @@ The following example indicates the full set of allowed type parameter declarati
*d: SimpleTypeVarTuple,
): ...

.. _type-scopes:

Type scopes
-----------

Type parameter lists and :keyword:`type` statements introduce *type scopes*,
which behave mostly like function scopes, but with some exceptions. Below,
the semantics of type parameter lists are explained using the pseudo-keyword
`def'` to indicate a type scope.

Type scopes are used in the following contexts:

* Type parameter lists for generic type aliases
* Type parameter lists for generic functions. The function's annotations are
executed within the type scope, but its defaults and decorators are not.
* Type parameter lists for generic classes. The class's base classes and
keyword arguments are executed within the type scope, but its decorators are not.
* The bounds and constraints for type variables.
* The value of type aliases.

Type scopes differ from function scopes in the following ways:

* If a type scope is immediately within a class scope, or within another
type scope that is immediately within a class scope, names defined in the
class scope can be accessed from within the type scope. (By contrast, regular
functions defined within classes cannot access names defined in the class scope.)
* Expressions in type scopes cannot contain :keyword:`yield`, ``yield from``,
:keyword:`await`, or :token:`:= <~python-grammar:expression>` expressions. (These
expressions are allowed in other scopes contained within the type scope.)
* Names defined in type scopes cannot be rebound with :keyword:`nonlocal`
statements in inner scopes. This includes only type parameters, as no other
syntactic elements that can appear within type scopes can introduce new names.
* While type scopes internally have a name, that name is not reflected in the
:attr:`__qualname__` of objects defined within the scope. Instead, the :attr:`!__qualname__`
of such objects is as if the object was defined in the enclosing scope.

.. _generic-functions:

Generic functions
Expand All @@ -1735,13 +1709,13 @@ This syntax is equivalent to::
return func
func = TYPE_PARAMS_OF_func()

Here `def'` indicates a :ref:`type scope <type-scopes>`. (Two other
Here ``def'`` indicates an :ref:`annotation scope <annotation-scopes>`. (Two other
liberties are taken in the translation: the ``__type_params__``
attribute of generic functions is not writable from Python code;
and the syntax does not go through attribute access on the :mod:`typing`
module, but creates an instance of :data:`typing.TypeVar` directly.)

The annotations of generic functions are defined within the type scope
The annotations of generic functions are defined within the annotation scope
used for declaring the type parameters, but the function's defaults and
decorators are not.

Expand All @@ -1752,15 +1726,16 @@ as well as for additional flavors of type parameters::
def func[T: int, *Ts, **P](*args: *Ts, arg: Callable[P, T] = some_default):
...

This is equivalent to::
Except for the lazy evaluation of the ``TypeVar`` bound, this is equivalent to::
Comment thread
JelleZijlstra marked this conversation as resolved.
Outdated

DEFAULT_OF_arg = some_default

def' TYPE_PARAMS_OF_func():

def' BOUND_OF_T():
return int
T = typing.TypeVar("T", evaluate_bound=BOUND_OF_T)
# In reality, BOUND_OF_T() is evaluated only on demand.
T = typing.TypeVar("T", bound=BOUND_OF_T())

Ts = typing.TypeVarTuple("Ts")
P = typing.ParamSpec("P")
Expand All @@ -1772,10 +1747,6 @@ This is equivalent to::
return func
func = decorator(TYPE_PARAMS_OF_func())

The Python constructor of :data:`typing.TypeVar` does not take an
``evaluate_bound`` argument. It is used in the translation to indicate
that the bound is lazily evaluated.

.. _generic-classes:

Generic classes
Expand All @@ -1795,7 +1766,7 @@ This syntax is equivalent to::
return Bag
Bag = TYPE_PARAMS_OF_Bag()

Here again `def'` indicates a :ref:`type scope <type-scopes>`.
Here again ``def'`` indicates an :ref:`annotation scope <annotation-scopes>`.

Generic classes implicitly inherit from :data:`typing.Generic`.
The base classes and keyword arguments of generic classes are
Expand All @@ -1821,7 +1792,21 @@ This is equivalent to::
Generic type aliases
--------------------

TODO(jelle)
The :keyword:`type` statement can also be used to create a generic type alias:

type ListOrSet[T] = list[T] | set[T]

Except for the lazy evaluation of the value, this is equivalent to:

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

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()



.. rubric:: Footnotes
Expand Down
43 changes: 42 additions & 1 deletion Doc/reference/executionmodel.rst
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ The namespace of the class definition becomes the attribute dictionary of
the class. The scope of names defined in a class block is limited to the
class block; it does not extend to the code blocks of methods. This includes
comprehensions and generator expressions since they are implemented using a
function scope, but it does not include :ref:`type scopes <type-scopes>`,
function scope, but it does not include :ref:`annotation scopes <annotation-scopes>`,
which have access to their enclosing class scopes.
This means that the following will fail::

Expand All @@ -184,6 +184,47 @@ However, the following will succeed::

print(A.Alias.__value__) # <type 'A.Nested'>

.. _annotation-scopes:

Annotation scopes
-----------------

:ref:`Type parameter lists <type-params>`` and :keyword:`type` statements
introduce *annotation scopes*, which behave mostly like function scopes,
but with some exceptions discussed below. :term:`Annotations <annotation>`
currently do not use annotation scopes, but they are expected to use
annotation scopes in Python 3.13 when :pep:`649` is implemented.
Comment thread
AlexWaygood marked this conversation as resolved.

Annotation scopes are used in the following contexts:

* Type parameter lists for generic type aliases
* Type parameter lists for generic functions. The function's annotations are
executed within the type scope, but its defaults and decorators are not.
* Type parameter lists for generic classes. The class's base classes and
keyword arguments are executed within the type scope, but its decorators are not.
* The bounds and constraints for type variables.
* The value of type aliases.

Annotation scopes differ from function scopes in the following ways:

* If an annotation scope is immediately within a class scope, or within another
annotation scope that is immediately within a class scope, names defined in the
class scope can be accessed from within the annotation scope, as if the code in the
annotation scope was executing directly within the class scope. (By contrast, regular
functions defined within classes cannot access names defined in the class scope.)
Comment thread
AlexWaygood marked this conversation as resolved.
Outdated
* Expressions in annotation scopes cannot contain :keyword:`yield`, ``yield from <expr>``,
:keyword:`await`, or :token:`:= <~python-grammar:expression>` expressions. (These
Comment thread
AlexWaygood marked this conversation as resolved.
Outdated
expressions are allowed in other scopes contained within the type scope.)
Comment thread
JelleZijlstra marked this conversation as resolved.
Outdated
* Names defined in annotation scopes cannot be rebound with :keyword:`nonlocal`
statements in inner scopes. This includes only type parameters, as no other
syntactic elements that can appear within type scopes can introduce new names.
Comment thread
JelleZijlstra marked this conversation as resolved.
Outdated
* While annotation scopes internally have a name, that name is not reflected in the
Comment thread
JelleZijlstra marked this conversation as resolved.
Outdated
:attr:`__qualname__` of objects defined within the scope. Instead, the :attr:`!__qualname__`
of such objects is as if the object was defined in the enclosing scope.
Comment thread
JelleZijlstra marked this conversation as resolved.
Outdated

.. versionadded:: 3.12
Annotation scopes were introduced in Python 3.12 as part of :pep:`695`.

.. _restrict_exec:

Builtins and restricted execution
Expand Down