Skip to content

PEP 747: Explicitly define "valid type expression". #4484

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
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
18 changes: 12 additions & 6 deletions peps/pep-0747.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <valid-type-expressions>`. ``TypeForm[T]`` describes the set of all type form
objects that represent the type ``T`` or types that are
:term:`assignable to <typing:assignable>` ``T``. For example,
``TypeForm[str | None]`` describes the set of all type form objects
Expand Down Expand Up @@ -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``,
Expand Down Expand Up @@ -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 <typing:expression-grammar>`. Semantic rules are specified
as comments along with the grammar definition. Contextual requirements are detailed
Expand All @@ -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
Expand Down