From da3bbabaa4379dc9e826723838827628fb0df1f2 Mon Sep 17 00:00:00 2001 From: Rebecca Chen Date: Sun, 6 Jul 2025 15:33:49 -0700 Subject: [PATCH] PEP 747: Explicitly define "valid type expression". 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. --- peps/pep-0747.rst | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/peps/pep-0747.rst b/peps/pep-0747.rst index af779076a0b..9bccabfef2b 100644 --- a/peps/pep-0747.rst +++ b/peps/pep-0747.rst @@ -183,7 +183,7 @@ expression, and it represents the type described by that type expression. ``TypeForm`` is a special form that, when used in a type expression, describes a set of type form objects. It accepts a single type argument, which must be a -valid type expression. ``TypeForm[T]`` describes the set of all type form +:ref:`valid type expression `. ``TypeForm[T]`` describes the set of all type form objects that represent the type ``T`` or types that are :term:`assignable to ` ``T``. For example, ``TypeForm[str | None]`` describes the set of all type form objects @@ -218,9 +218,7 @@ equivalent to ``TypeForm[Any]``. Implicit ``TypeForm`` Evaluation -------------------------------- -When a static type checker encounters an expression that follows all of the -syntactic, semantic and contextual rules for a type expression as detailed -in the typing spec, the evaluated type of this expression should be assignable +When a static type checker encounters a valid type expression, the evaluated type of this expression should be assignable to ``TypeForm[T]`` if the type it describes is assignable to ``T``. For example, if a static type checker encounters the expression ``str | None``, @@ -248,6 +246,11 @@ be assignable to ``TypeForm``:: v5: TypeForm[set[str]] = "set[str]" # OK +.. _valid-type-expressions: + +Valid Type Expressions +---------------------- + The typing spec defines syntactic rules for type expressions in the form of a :ref:`formal grammar `. Semantic rules are specified 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 type expression only within a class, and a type variable can be used within a type expression only when it is associated with a valid scope. -Expressions that violate one or more of the syntactic, semantic, or contextual -rules for type expressions should not evaluate to a ``TypeForm`` type.:: +A valid type expression is an expression that follows all of the syntactic, +semantic, and contextual rules for a type expression. + +Expressions that are not valid type expressions should not evaluate to a +``TypeForm`` type:: bad1: TypeForm = tuple() # Error: Call expression not allowed in type expression bad2: TypeForm = (1, 2) # Error: Tuple expression not allowed in type expression