Skip to content

Commit 60f0e69

Browse files
authored
PEP 747: Explicitly define "valid type expression". (#4484)
When I was reading the PEP, it was unclear to me whether the rule on what expressions can implicitly evaluate to TypeForm applied to the rest of the PEP. This PR suggests pulling that rule out into its own section and linking to it the first time "valid type expression" is used, to make it clear that this definition applies throughout.
1 parent 914a826 commit 60f0e69

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

peps/pep-0747.rst

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ expression, and it represents the type described by that type expression.
183183

184184
``TypeForm`` is a special form that, when used in a type expression, describes
185185
a set of type form objects. It accepts a single type argument, which must be a
186-
valid type expression. ``TypeForm[T]`` describes the set of all type form
186+
:ref:`valid type expression <valid-type-expressions>`. ``TypeForm[T]`` describes the set of all type form
187187
objects that represent the type ``T`` or types that are
188188
:term:`assignable to <typing:assignable>` ``T``. For example,
189189
``TypeForm[str | None]`` describes the set of all type form objects
@@ -218,9 +218,7 @@ equivalent to ``TypeForm[Any]``.
218218
Implicit ``TypeForm`` Evaluation
219219
--------------------------------
220220

221-
When a static type checker encounters an expression that follows all of the
222-
syntactic, semantic and contextual rules for a type expression as detailed
223-
in the typing spec, the evaluated type of this expression should be assignable
221+
When a static type checker encounters a valid type expression, the evaluated type of this expression should be assignable
224222
to ``TypeForm[T]`` if the type it describes is assignable to ``T``.
225223

226224
For example, if a static type checker encounters the expression ``str | None``,
@@ -248,6 +246,11 @@ be assignable to ``TypeForm``::
248246

249247
v5: TypeForm[set[str]] = "set[str]" # OK
250248

249+
.. _valid-type-expressions:
250+
251+
Valid Type Expressions
252+
----------------------
253+
251254
The typing spec defines syntactic rules for type expressions in the form of a
252255
:ref:`formal grammar <typing:expression-grammar>`. Semantic rules are specified
253256
as comments along with the grammar definition. Contextual requirements are detailed
@@ -256,8 +259,11 @@ type expressions. For example, the special form ``Self`` can be used in a
256259
type expression only within a class, and a type variable can be used within
257260
a type expression only when it is associated with a valid scope.
258261

259-
Expressions that violate one or more of the syntactic, semantic, or contextual
260-
rules for type expressions should not evaluate to a ``TypeForm`` type.::
262+
A valid type expression is an expression that follows all of the syntactic,
263+
semantic, and contextual rules for a type expression.
264+
265+
Expressions that are not valid type expressions should not evaluate to a
266+
``TypeForm`` type::
261267

262268
bad1: TypeForm = tuple() # Error: Call expression not allowed in type expression
263269
bad2: TypeForm = (1, 2) # Error: Tuple expression not allowed in type expression

0 commit comments

Comments
 (0)