Skip to content

Commit aa7a28f

Browse files
committed
Write a bunch more about functions
1 parent bddf241 commit aa7a28f

1 file changed

Lines changed: 66 additions & 10 deletions

File tree

pep.rst

Lines changed: 66 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,7 @@ as the ``Init`` field of the ``Member``.
718718
Annotated
719719
'''''''''
720720

721-
This could maybe be dropped?
721+
TODO: This could maybe be dropped if it doesn't seem implementable?
722722

723723
Libraries like FastAPI use annotations heavily, and we would like to
724724
be able to use annotations to drive type-level computation decision
@@ -758,28 +758,34 @@ values too?
758758
.. _generic-callable:
759759

760760
Generic Callable
761-
''''''''''''''''
761+
""""""""""""""""
762762

763763
* ``GenericCallable[Vs, lambda <vs>: Ty]``: A generic callable. ``Vs`` are a tuple
764764
type of unbound type variables and ``Ty`` should be a ``Callable``,
765765
``staticmethod``, or ``classmethod`` that has access to the
766766
variables in ``Vs`` via the bound variables in ``<vs>``.
767767

768-
(LAMBDA PART NOT IMPLEMENTED YET)
768+
For now, we restrict the use of ``GenericCallable`` to
769+
the type argument of ``Member`` (that is, to disallow its use for
770+
locals, parameter types, return types, nested inside other types,
771+
etc).
769772

770-
This is kind of unsatisfying but we at least need some way to return
771-
existing generic methods and put them back into a new protocol and we
772-
need to be able to delay evaluation of conditionals/iteration at
773-
runtime.
773+
(This is a little unsatisfying. Rationale discussed :ref:`below
774+
<generic-callable-rationale>`.)
774775

775-
The unbound type variable tuple is so that bounds and defaults and
776-
``TypeVarTuple``-ness can be specified, though maybe we want to come
777-
up with a new approach.
776+
777+
(LAMBDA PART NOT IMPLEMENTED YET)
778778

779779
TODO: Decide if we have any mechanisms to inspect/destruct
780780
``GenericCallable``. Maybe can fetch the variable information and
781781
maybe can apply it to concrete types?
782782

783+
Overloaded function types
784+
"""""""""""""""""""""""""
785+
786+
* ``Overloaded[*Callables]`` - An overloaded function type, with the
787+
underlying types in order.
788+
783789
String manipulation
784790
'''''''''''''''''''
785791

@@ -1172,6 +1178,56 @@ I am proposing a fully new extended callable syntax because:
11721178
is a non starter)
11731179

11741180

1181+
.. _generic-callable-rationale:
1182+
1183+
Generic Callable
1184+
----------------
1185+
1186+
Consider a method with the following signature::
1187+
1188+
def process[T](self, x: T) -> T if IsSub[T, list] else list[T]:
1189+
...
1190+
1191+
The type of the method is generic, and the generic is bound at the
1192+
**method**, not the class. We need a way to represent such a generic
1193+
function both as a programmer might write it for a ``NewProtocol``.
1194+
1195+
One option that is somewhat appealing but doesn't work would be to use
1196+
unbound type variables and let them be generalized::
1197+
1198+
type Foo = NewProtocol[
1199+
Member[
1200+
Literal["process"],
1201+
Callable[[T], set[T] if IsSub[T, int] else T]
1202+
]
1203+
]
1204+
1205+
The problem is that this is basically incompatible with runtime
1206+
evaluation support, since evaluating the alias ``Foo`` will need to
1207+
evalaute the ``IsSub``, and so we will lose one side of the
1208+
conditional at least. Similar problems will happen when evaluating
1209+
``Members`` on a class with generic functions. By wrapping the body
1210+
in a lambda, we can delay evaluation in both of these cases. (The
1211+
``Members`` case of delaying evaluation works quite nicely for
1212+
functions with explicit generic annotations. For old-style generics,
1213+
we'll probably have to try to evaluate it and then raise an error when
1214+
we encounter a variable.)
1215+
1216+
1217+
The reason we suggest restricting the use of ``GenericCallable`` to
1218+
the type argument of ``Member`` is because full impredicative
1219+
polymorphism (where you can have generic type binding nested inside
1220+
types and you can instantiate type variables with other generic types)
1221+
is an big can of worms when combined with type inference (TODO: CITE).
1222+
While it would be nice to support, we don't want to
1223+
open that can of worms now.
1224+
1225+
1226+
The unbound type variable tuple is so that bounds and defaults and
1227+
``TypeVarTuple``-ness can be specified, though maybe we want to come
1228+
up with a new approach.
1229+
1230+
11751231
Backwards Compatibility
11761232
=======================
11771233

0 commit comments

Comments
 (0)