-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
PEP 718: Specify binding, parametrisation and overload interactions #4649
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
Changes from 4 commits
a33030f
475bd8e
bd36a62
1ba67e9
4d33729
7e2dd28
6ac59eb
c6b4588
382eb92
3baa5be
8311622
75ca1ad
109fddd
9e581a4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -201,10 +201,56 @@ The following code snippet would fail at runtime without this change as | |
| Interactions with ``@typing.overload`` | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
|
||
| Overloaded functions should work much the same as already, since they have no effect on | ||
| the runtime type. The only change is that more situations will be decidable and the | ||
| behaviour/overload can be specified by the developer rather than leaving it to ordering | ||
| of overloads/unions. | ||
| Overloaded functions should work much the same as they already do, since they do not | ||
| affect the runtime type. Explicit specialisation will restrict the set of available | ||
| overloads. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's worth spelling out in detail how this would work. What I'd expect is that if the function is subscripted, only those overloads are considered for which the subscription may succeed. So if you have an overload |
||
|
|
||
| .. code-block:: python | ||
|
|
||
| @overload | ||
| def make[T](x: T) -> T: ... | ||
| @overload | ||
| def make(x: str, y: str) -> tuple[int, int]: ... | ||
|
|
||
| reveal_type(make[int](1)) # type is int | ||
| reveal_type(make[int]("foo", "bar")) # Invalid: no overload for `make[int](x: str, y: str)` found, a similar overload exists but explicit specialisation prevented its use | ||
|
|
||
| Functions Parameterized by ``TypeVarTuple``\ s | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| Currently, type checkers disallow the use of multiple ``TypeVarTuple``\s in their | ||
| generic parameters; however, it is currently valid to have a function as such: | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| def foo[*T, *U](bar: Bar[*T], baz: Baz[*U]): ... | ||
| def spam[*T](bar: Bar[*T]): ... | ||
|
|
||
| This PEP does not allow functions like ``foo`` to be subscripted, for the same reason | ||
| as defined in :pep:`PEP 646<646#multiple-type-variable-tuples-not-allowed>`. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That reason is so simple to explain that I'd just repeat it here. Also make it explicit that
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
|
|
||
| .. code-block:: python | ||
|
|
||
| foo[int, str, bool, complex](Bar(), Baz()) # Invalid: cannot determine which parameters are passed to *T and *U. Explicitly parameterise the instances individually | ||
| spam[int, str, bool, complex](Bar()) # OK | ||
|
|
||
| Binding Rules | ||
| ^^^^^^^^^^^^^ | ||
| Method subscription (including ``classmethods``, ``staticmethods``, etc.) should only | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is covered by the casual "etc." here? In a spec it need to specified, not left as a guess for the reader based on an example.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think i will plainly remove the "etc". "@Property" is not in the same box, and other decorators seem irrelevant for the matter. |
||
| have access tos their function's type parameter and not the enclosing class's. | ||
|
Gobot1234 marked this conversation as resolved.
Outdated
|
||
| Subscription should follow the rules specified in :pep:`PEP 696<696#binding-rules>`; | ||
| methods should bind type parameters on attribute access. | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| class C[T]: | ||
| def method[U](self, x: T, y: U): ... | ||
| @classmethod | ||
| def cls[U](cls, x: T, y: U): ... | ||
|
|
||
| C[int].method[str](0, "") # OK | ||
| C[int].cls[str](0, "") # OK | ||
| C.cls[int, str](0, "") # Invalid: too many type parameters | ||
| C.cls[str](0, "") # OK, T is ideally bound to int here though this is open for type checkers to decide | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What else should type checkers do? Infer
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I assume @JelleZijlstra meant "What else could type checkers do?" I guess another option would be
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think U is matched to str, and once we clear that out, the behaviour of T is not for this PEP to decide, the decision is already made by some typecheckers, so no change: I will make a pr to change it to:
|
||
|
|
||
| Backwards Compatibility | ||
| ----------------------- | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess this is about the runtime behavior of calling overloaded functions?