8000 PEP 747: TypeExpr: Type Hint for a Type Expression by davidfstr · Pull Request #3798 · python/peps · GitHub
[go: up one dir, main page]

Skip to content

PEP 747: TypeExpr: Type Hint for a Type Expression #3798

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 47 commits into from
Jun 16, 2024
Merged
Changes from 1 commit
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
564484a
PEP 755: TypeExpr: Type Hint for a Type Expression
davidfstr May 27, 2024
93aa917
Update .github/CODEOWNERS
davidfstr May 27, 2024
4ed4602
Update Created header
davidfstr May 27, 2024
0bbbd9c
Post-History: Recognize discuss.python.org post links without trailin…
davidfstr May 28, 2024
836a89a
Include only draft 1 and 2 links in Post-History. Remove Discussions-…
davidfstr May 28, 2024
6d5de2b
Change PEP number: 755 -> 747
davidfstr May 28, 2024
5737d06
Rewrite some PEP links using :pep:`...`
davidfstr May 28, 2024
9bbd9fc
Add code formatting in many places
davidfstr May 28, 2024
4352ac2
Formatting
davidfstr Jun 1, 2024
38ca3a1
Clarify statement about ForwardRefs
davidfstr Jun 1, 2024
3014728
Formatting
davidfstr Jun 1, 2024
2552bd0
Wordsmithing
davidfstr Jun 1, 2024
1c6a7af
PEP 12: Extract Intersphinx section. Add example of Intersphinx refer…
davidfstr May 31, 2024
433b557
Rewrite links to Typing specification to use Intersphinx references
davidfstr May 31, 2024
c997363
Change Optional[str] example in introductions to (str | None)
davidfstr May 31, 2024
c967db2
Revert "Post-History: Recognize discuss.python.org post links without…
davidfstr May 31, 2024
5e548ac
Alter draft 2 URL to non-canonical form to pass lint check
davidfstr May 31, 2024
f78c53b
Using TypeExprs: Clarify that TypeExpr is itself a type expression
davidfstr May 31, 2024
ad0d357
Remove some details about how TypeExpr is implemented at runtime
davidfstr May 31, 2024
a12fbb1
Affected signatures in the standard library: Elide the full signature…
davidfstr May 31, 2024
732ebf0
Clarify that class objects can usually, but not always, be instantiated
davidfstr May 31, 2024
3105326
Stringified TypeExprs: Clarify that behavior follows from TypeExpr be…
davidfstr Jun 1, 2024
fb83901
Clarify that (Any | None) is not the same as (Any)
davidfstr Jun 1, 2024
403a29d
Reformat Annotated link to be all on one line
davidfstr Jun 1, 2024
56ba5fe
Literal: Remove low-value example
davidfstr Jun 1, 2024
7c5911d
Non-Universal Type Expressions: Leave behaviors undefined
davidfstr Jun 1, 2024
e9cc932
"constrain" -> "parameterized by"
davidfstr Jun 1, 2024
cd51ef1
Shape -> MyTypedDict
davidfstr Jun 1, 2024
69ba1ee
"must" -> "already must"
davidfstr Jun 1, 2024
8a678fe
"constrain" -> "parameterized by" (Take 2)
davidfstr Jun 5, 2024
11a1e88
Add Rationale section, explaining what a type expression is and why i…
davidfstr Jun 5, 2024
f21077f
Non-Universal TypeExprs: Remove explanations of where Self, TypeGuard…
davidfstr Jun 5, 2024
9e58966
Static vs. Runtime Representations of TypeExprs: Streamline
davidfstr Jun 5, 2024
bf0ed38
Subtyping: Fix missing formatting for "object"
davidfstr Jun 5, 2024
098192b
Interactions with Annotated[]: Remove section
davidfstr Jun 5, 2024
2394391
How to Teach This: Rewrite section, integrating some prior 'Interacti…
davidfstr Jun 5, 2024
e035ecd
Revert "PEP 12: Extract Intersphinx section. Add example of Intersphi…
davidfstr Jun 6, 2024
dab1a63
Mention that dataclasses.make_dataclass() specially handles InitVar
davidfstr Jun 6, 2024
d3b7ec3
Explicit/Implicit TypeExpr Values: Add sections
davidfstr Jun 7, 2024
1c49cb6
Non-Universal TypeExprs: Inline section to initial 'TypeExpr Values' …
davidfstr Jun 7, 2024
64f510f
Wordsmith
davidfstr Jun 7, 2024
916e90a
Backwards Compatibility: Mention new rules for recognizing type expre…
davidfstr Jun 8, 2024
7e9b446
Motivation: Optionals -> unions
davidfstr Jun 8, 2024
f2b4818
Remove mentions to the is-consistent-with relationship, which is bein…
davidfstr Jun 8, 2024
4c2d5ab
Support pattern matching on type expressions: Clarify examples use a …
davidfstr Jun 8, 2024
ecafabc
Grammar
davidfstr Jun 8, 2024
bcc127e
Apply minor feedback
davidfstr Jun 16, 2024
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
Add Rationale section, explaining what a type expression is and why i…
…t's used
  • Loading branch information
davidfstr committed Jun 5, 2024
commit 11a1e88eb7c52cb200c95e23d189bcf0bfd33218
57 changes: 44 additions & 13 deletions peps/pep-0747.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ This PEP makes no Python grammar changes. Correct usage of
type checkers and need not be enforced by Python itself at runtime.


.. _motivation:

Motivation
==========

Expand Down Expand Up @@ -190,20 +192,11 @@ annotation expressions as input using plain ``object``\ s which would
.. _typing_inspect: https://github.com/ilevkivskyi/typing_inspect?tab=readme-ov-file#readme


Specification
=============

A ``TypeExpr`` represents a :ref:`type expression <typing:type-expression>`
such as ``str | None``, ``dict[str, int]``, or ``MyTypedDict``.
A ``TypeExpr`` type is written as
``TypeExpr[T]`` where ``T`` is a type or a type variable. It can also be
written without brackets as just ``TypeExpr``, in which case a type
checker should apply its usual type inference mechanisms to determine
the type of its argument, possibly ``Any``.
Rationale
=========

Type expressions include more values than
:ref:`annotation expressions <typing:annotation-expression>` but
fewer values than *class objects* (``type[C]``):
Before this PEP existed there were already a few definitions in use to describe
different kinds of type annotations:

::

Expand All @@ -220,6 +213,44 @@ fewer values than *class objects* (``type[C]``):
| Object | = object
+----------------------------------+

- :ref:`Class objects <typing:type-brackets>`,
spelled as ``type[C]``, support ``isinstance`` checks and are callable.

- Examples: ``int``, ``str``, ``MyClass``

- :ref:`Type expressions <typing:type-expression>`
include any type annotation which describes a type.

- Examples: ``list[int]``, ``MyTypedDict``, ``int | str``,
``Literal['square']``, any class object

- :ref:`Annotation expressions <typing:annotation-expression>`
include any type annotation, including those only valid in specific contexts.

- Examples: ``Final[int]``, ``Required[str]``, ``ClassVar[str]``,
any type expression

``TypeExpr`` aligns with an existing definition from the above list -
*type expression* - to avoid introducing yet another subset of type annotations
that users of Python typing need to think about.

``TypeExpr`` aligns with *type expression* specifically
because a type expression is already used to parameterize type variables,
which are used in combination with ``TypeIs`` and ``TypeGuard`` to enable
the compelling examples mentioned in :ref:`Motivation <motivation>`.


Specification
=============

A ``TypeExpr`` represents a :ref:`type expression <typing:type-expression>`
such as ``str | None``, ``dict[str, int]``, or ``MyTypedDict``.
A ``TypeExpr`` type is written as
``TypeExpr[T]`` where ``T`` is a type or a type variable. It can also be
written without brackets as just ``TypeExpr``, in which case a type
checker should apply its usual type inference mechanisms to determine
the type of its argument, possibly ``Any``.


Using TypeExprs
---------------
Expand Down
0