Skip to content
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

PEP 718: More rationale, use-cases and expansion on monomorphisation #3631

Merged
merged 5 commits into from
Feb 17, 2024
Merged
Changes from 2 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
52 changes: 49 additions & 3 deletions peps/pep-0718.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,16 @@ Abstract
This PEP proposes making function objects subscriptable for typing purposes. Doing so
gives developers explicit control over the types produced by the type checker where
bi-directional inference (which allows for the types of parameters of anonymous
functions to be inferred) and other methods than specialisation are insufficient.
functions to be inferred) and other methods than specialisation are insufficient. It
also brings functions in line with regular classes in their ability to be
subscriptable.

Motivation
----------

Unknown Types
^^^^^^^^^^^^^

Currently, it is not possible to infer the type parameters to generic functions in
certain situations:

Expand Down Expand Up @@ -62,6 +67,23 @@ If function objects were subscriptable, however, a more specific type could be g

reveal_type(factory[int](lambda x: "Hello World" * x)) # type is Foo[int]

Undecidable Inference
^^^^^^^^^^^^^^^^^^^^^

There are even cases where subclass relations make type inference impossible. However,
if you can specialise the function type checkers can infer a meaningful type.

.. code-block:: python

def foo(x: Sequence[T] | T) -> list[T]: ...
Gobot1234 marked this conversation as resolved.
Show resolved Hide resolved

reveal_type(foo[bytes](b"hello"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is maybe a better fit for the discussion, but I find this example disturbing, because it seems I could also write foo[int](b"hello"), match the Sequence part of the Union, and now the type checker would infer that the function returns list[bytes] instead of list[int]. Isn't that inherently unsound?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I do see your point here but isn't the current situation still unsound because the choosing of the type from the type checker is still up to its logic?


Currently, type checkers will treat unions as ordered and pick the first matching type.
Gobot1234 marked this conversation as resolved.
Show resolved Hide resolved

Unsolvable Type Parameters
^^^^^^^^^^^^^^^^^^^^^^^^^^

Currently, with unspecialised literals, it is not possible to determine a type for
situations similar to:

Expand Down Expand Up @@ -93,9 +115,26 @@ Due to this, specialising the function and using it as a new factory is fine
make_int_list = make_list[int]
reveal_type(make_int_list()) # type is list[int]

Monomorphisation and Reification
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

This proposal also opens the door to
`monomorphisation <https://en.wikipedia.org/wiki/Monomorphization>`_ and
`reified types <https://en.wikipedia.org/wiki/Reification_(computer_science)>`_
`reified types <https://en.wikipedia.org/wiki/Reification_(computer_science)>`_.

This would allow for a functionality which anecdotally has been requested many times.

*Please note this feature is not being proposed by the PEP, but may be implemented in
the future.*

The syntax for such a feature may look something like:

.. code-block:: python

def foo[T]():
return T.__value__

assert foo[int]() is int

Rationale
---------
Expand Down Expand Up @@ -158,7 +197,14 @@ The following code snippet would fail at runtime without this change as
def bar[U]():
return Foo[int]()

assert bar[str]().__orig_class__ is Foo[int]
assert bar[str]().__orig_class__ == Foo[int]

Interactions with `@typing.overload`
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Gobot1234 marked this conversation as resolved.
Show resolved Hide resolved
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.

Backwards Compatibility
-----------------------
Expand Down
Loading