-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
PEP 677: Runtime Behavior Specification #2237
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
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b25eec4
PEP 677: Add Detailed Runtime Behavior Spec
stroxler 89c313f
Rework: flatten the API some, use python pseudocode rather than prose
stroxler 2cdf37d
Change Ellipses to builtins.Ellipsis
stroxler d46ec9c
Update pep-0677.rst
stroxler e8c71b8
Switch to an inspect.Signature-like API
stroxler c1df8f5
Apply suggestions from code review
stroxler 6af28c3
Changes in response to code review comments
stroxler d7b87de
Add missing
stroxler b6aef56
Fix markdown-style code block
stroxler 9ecdc43
Add missing annotations
stroxler File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -635,24 +635,92 @@ callable types and ``=>`` for lambdas. | |
| Runtime Behavior | ||
| ---------------- | ||
|
|
||
| Our tentative plan is that: | ||
| The new AST nodes need to evaluate to runtime types, and we have two goals for the | ||
| behavior of these runtime types: | ||
|
|
||
| - The ``__repr__`` will show an arrow syntax literal. | ||
| - We will provide a new API where the runtime data structure can be | ||
| accessed in the same manner as the AST data structure. | ||
| - We will ensure that we provide an API that is backward-compatible | ||
| with ``typing.Callable`` and ``typing.Concatenate``, specifically | ||
| the behavior of ``__args__`` and ``__parameters__``. | ||
| - They should expose a structured API as similar as possible to the AST where | ||
| the parameter and return types have descriptive names. | ||
| - They should also expose an API that is fully backward-compatible with | ||
stroxler marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ``typing.Callable`` | ||
|
|
||
| Because these details are still under debate we are currently | ||
| maintaining `a separate doc | ||
| <https://docs.google.com/document/d/15nmTDA_39Lo-EULQQwdwYx_Q1IYX4dD5WPnHbFG71Lk/edit>`_ | ||
| with details about the new builtins, the evaluation model, how to | ||
| provide both a backward-compatible and more structured API, and | ||
| possible alternatives to the current plan. | ||
| Evaluation and Structured API | ||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
|
||
| We intend to create new builtin types to which the new AST nodes will | ||
| evaluate, exposing them in the ``types`` module. | ||
|
|
||
| The ``CallableType`` and ``AsyncCallableType`` AST nodes should evaluate | ||
| to ``types.CallableType`` and ``types.AsyncCallableType``. Each of these | ||
stroxler marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| should have fields: | ||
|
|
||
| - ``args`` containing an evaluated arguments list (see below) | ||
| - ``returns`` containing the evaluated return type | ||
stroxler marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| The arguments list AST nodes should evaluate as follows: | ||
| - ``AnyArguments`` evaluates to ``types.CallableAnyArguments`` | ||
|
|
||
| - This type has no fields | ||
|
|
||
| - ``ArgumentsList`` evaluates to ``types.CallableARgumentsList``, having | ||
stroxler marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| - A field ``posonlyargs`` which will be a tuple of all parameter types. | ||
stroxler marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| - ``Concatenation`` evaluates to ``types.CallableConcatenation``, having | ||
|
|
||
| - A field ``posonlyargs`` which will be a tuple of the normal parameter types. | ||
| - A field ``param_spec`` which will be the final ``ParamSpec`` argument. | ||
|
|
||
| Backward-Compatible API | ||
| ~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
|
||
| To get backward compatibility with the existing ``types.Callable`` API, | ||
| which relies on fields ``__args__`` and ``__parameters__``, we define them | ||
| as follows: | ||
|
|
||
| - For ``types.CallableType``: | ||
|
|
||
| - ``__args__`` should be the tuple result of appending the return type to the | ||
| result of fetching ``__args__`` on ``args`` (see below). | ||
| - ``__parameters__`` should be the result of concatenating ``__parameters__`` | ||
| of the arguments list with ``__parameters__`` of the return type. | ||
stroxler marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| - ``types.AsyncCallableType`` should work the same, except that its return | ||
| value should automatically be wrapped in a ``typing.Awaitable`` when | ||
| evaluating ``__args__``. | ||
stroxler marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| - For ``types.CallableAnyAguments``: | ||
|
|
||
| - ``__args__`` should evaluate to ``(Ellipses,)``. | ||
|
||
| - ``__parameters__`` should evaluate to ``()``. | ||
stroxler marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| - For ``types.CallableArgumentsList``: | ||
|
|
||
| - ``__args__`` should evaluate to ``posonlyargs``. | ||
| - ``__parameters__`` should be the result of concatenating the | ||
| ``__parameters__`` of each type in ``posonlyargs``. | ||
|
|
||
| - For ``types.CallableConcatenation``: | ||
|
|
||
| - ``__args__`` should be the tuple result of appending ``param_spec`` to | ||
| ``posonlyargs``. | ||
| - ``__parameters__`` should be the result of concatinating the | ||
| ``__parameters__`` of each type in ``posonlyargs``, and also. | ||
|
|
||
|
|
||
|
|
||
| Behavior of other methods | ||
| ~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
|
||
| - The ``__eq__`` method should treat equivalent ``typing.Callable`` | ||
| values as equal to values constructed using the builtin syntax. | ||
| - The ``__repr__`` method of ``types.CallableType`` and | ||
| ``types.AsyncCallableType`` should produce a representation in arrow syntax. | ||
|
|
||
| - The argument list types should produce a placeholder for ``__repr__`` | ||
| rather than a pretty tuple-like value because they cannot be evaluated | ||
| except inside of a full arrow type literal. | ||
stroxler marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| - But we can pretty-print them in their ``__str__`` method and use that | ||
| in the ``__repr__`` method of ``types.CallableType``. | ||
stroxler marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| Once the plan is finalized we will include a full specification of | ||
| runtime behavior in this section of the PEP. | ||
|
|
||
| Rejected Alternatives | ||
| ===================== | ||
|
|
@@ -1033,10 +1101,11 @@ Open Issues | |
| Details of the Runtime API | ||
| -------------------------- | ||
|
|
||
| Once we have finalized all details of the runtime behavior, we | ||
| will need to add a full specification of the behavior to the | ||
| `Runtime Behavior`_ section of this PEP as well as include that | ||
| behavior in our reference implementation. | ||
| We have attempted to provide a complete behavior specification in | ||
| the `Runtime Behavior`_ section of this PEP. | ||
|
|
||
| But there are probably more details that we will not realize we | ||
| need to define until we build a full reference implementation. | ||
|
|
||
| Optimizing ``SyntaxError`` messages | ||
| ----------------------------------- | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.