Skip to content

Commit

Permalink
Reformatted the migration docs to fit to 88 columns
Browse files Browse the repository at this point in the history
  • Loading branch information
agronholm committed Aug 30, 2023
1 parent c4f7afe commit 54a76c1
Showing 1 changed file with 43 additions and 35 deletions.
78 changes: 43 additions & 35 deletions docs/migration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ desired type as the second argument::

send, receive = create_memory_object_stream(100, int)

In 4.0, :class:`create_memory_object_stream() <create_memory_object_stream>` is a class masquerading as a function, so
you need to parametrize it::
In 4.0, :class:`create_memory_object_stream() <create_memory_object_stream>` is a class
masquerading as a function, so you need to parametrize it::

send, receive = create_memory_object_stream[int](100)

Expand Down Expand Up @@ -136,16 +136,17 @@ Make sure not to actually call the factory function!
Migrating from AnyIO 2 to AnyIO 3
=================================

AnyIO 3 changed some functions and methods in a way that needs some adaptation in your code.
All deprecated functions and methods will be removed in AnyIO 4.
AnyIO 3 changed some functions and methods in a way that needs some adaptation in your
code. All deprecated functions and methods will be removed in AnyIO 4.

Asynchronous functions converted to synchronous
-----------------------------------------------

AnyIO 3 changed several previously asynchronous functions and methods into regular ones for two
reasons:
AnyIO 3 changed several previously asynchronous functions and methods into regular ones
for two reasons:

#. to better serve use cases where synchronous callbacks are used by third party libraries
#. to better serve use cases where synchronous callbacks are used by third party
libraries
#. to better match the API of Trio_

The following functions and methods were changed:
Expand All @@ -162,19 +163,20 @@ The following functions and methods were changed:
* :meth:`Lock.release`
* :meth:`MemoryObjectReceiveStream.receive_nowait()
<.streams.memory.MemoryObjectReceiveStream.receive_nowait>`
* :meth:`MemoryObjectSendStream.send_nowait() <.streams.memory.MemoryObjectSendStream.send_nowait>`
* :meth:`MemoryObjectSendStream.send_nowait()
<.streams.memory.MemoryObjectSendStream.send_nowait>`
* :func:`open_signal_receiver`
* :meth:`Semaphore.release`

When migrating to AnyIO 3, simply remove the ``await`` from each call to these.

.. note:: For backwards compatibility reasons, :func:`current_time`,
:func:`current_effective_deadline` and :func:`get_running_tasks` return objects which are
awaitable versions of their original types (:class:`float` and :class:`list`,
respectively). These awaitable versions are subclasses of the original types so they
should behave as their originals, but if you absolutely need the pristine original types,
you can either use ``maybe_async`` or ``float()`` / ``list()`` on the returned
value as appropriate.
:func:`current_effective_deadline` and :func:`get_running_tasks` return objects which
are awaitable versions of their original types (:class:`float` and :class:`list`,
respectively). These awaitable versions are subclasses of the original types so they
should behave as their originals, but if you absolutely need the pristine original
types, you can either use ``maybe_async`` or ``float()`` / ``list()`` on the returned
value as appropriate.

The following async context managers changed to regular context managers:

Expand All @@ -185,14 +187,15 @@ The following async context managers changed to regular context managers:
When migrating, just change ``async with`` into a plain ``with``.

With the exception of
:meth:`MemoryObjectReceiveStream.receive_nowait() <.streams.memory.MemoryObjectReceiveStream.receive_nowait>`,
all of them can still be used like before – they will raise :exc:`DeprecationWarning` when used
this way on AnyIO 3, however.
:meth:`MemoryObjectReceiveStream.receive_nowait()
<.streams.memory.MemoryObjectReceiveStream.receive_nowait>`,
all of them can still be used like before – they will raise :exc:`DeprecationWarning`
when used this way on AnyIO 3, however.

If you're writing a library that needs to be compatible with both major releases, you will need
to use the compatibility functions added in AnyIO 2.2: ``maybe_async()`` and
``maybe_async_cm()``. These will let you safely use functions/methods and context managers
(respectively) regardless of which major release is currently installed.
If you're writing a library that needs to be compatible with both major releases, you
will need to use the compatibility functions added in AnyIO 2.2: ``maybe_async()`` and
``maybe_async_cm()``. These will let you safely use functions/methods and context
managers (respectively) regardless of which major release is currently installed.

Example 1 – setting an event::

Expand All @@ -217,12 +220,13 @@ Example 2 – opening a cancel scope::
Starting tasks
--------------

The ``TaskGroup.spawn()`` coroutine method has been deprecated in favor of the synchronous
method :meth:`.TaskGroup.start_soon` (which mirrors ``start_soon()`` in Trio's nurseries).
If you're fully migrating to AnyIO 3, simply switch to calling the new method (and remove the ``await``).
The ``TaskGroup.spawn()`` coroutine method has been deprecated in favor of the
synchronous method :meth:`.TaskGroup.start_soon` (which mirrors ``start_soon()`` in
Trio's nurseries). If you're fully migrating to AnyIO 3, simply switch to calling the
new method (and remove the ``await``).

If your code needs to work with both AnyIO 2 and 3, you can keep using ``TaskGroup.spawn()``
(until AnyIO 4) and suppress the deprecation warning::
If your code needs to work with both AnyIO 2 and 3, you can keep using
``TaskGroup.spawn()`` (until AnyIO 4) and suppress the deprecation warning::

import warnings

Expand All @@ -234,27 +238,30 @@ If your code needs to work with both AnyIO 2 and 3, you can keep using ``TaskGro
Blocking portal changes
-----------------------

AnyIO now **requires** :func:`.from_thread.start_blocking_portal` to be used as a context manager::
AnyIO now **requires** :func:`.from_thread.start_blocking_portal` to be used as a
context manager::

from anyio import sleep
from anyio.from_thread import start_blocking_portal

with start_blocking_portal() as portal:
portal.call(sleep, 1)

As with ``TaskGroup.spawn()``, the ``BlockingPortal.spawn_task()`` method has also been renamed
to :meth:`~from_thread.BlockingPortal.start_task_soon`, so as to be consistent with task groups.
As with ``TaskGroup.spawn()``, the ``BlockingPortal.spawn_task()`` method has also been
renamed to :meth:`~from_thread.BlockingPortal.start_task_soon`, so as to be consistent
with task groups.

The ``create_blocking_portal()`` factory function was also deprecated in favor of instantiating
:class:`~from_thread.BlockingPortal` directly.
The ``create_blocking_portal()`` factory function was also deprecated in favor of
instantiating :class:`~from_thread.BlockingPortal` directly.

For code requiring cross compatibility, catching the deprecation warning (as above) should work.
For code requiring cross compatibility, catching the deprecation warning (as above)
should work.

Synchronization primitives
--------------------------

Synchronization primitive factories (``create_event()`` etc.) were deprecated in favor of
instantiating the classes directly. So convert code like this::
Synchronization primitive factories (``create_event()`` etc.) were deprecated in favor
of instantiating the classes directly. So convert code like this::

from anyio import create_event

Expand Down Expand Up @@ -285,7 +292,8 @@ Threading functions moved

Threading functions were restructured to submodules, following the example of Trio:

* ``current_default_worker_thread_limiter`` → :func:`.to_thread.current_default_thread_limiter`
* ``current_default_worker_thread_limiter`` →
:func:`.to_thread.current_default_thread_limiter`
(NOTE: the function was renamed too!)
* ``run_sync_in_worker_thread()`` → :func:`.to_thread.run_sync`
* ``run_async_from_thread()`` → :func:`.from_thread.run`
Expand Down

0 comments on commit 54a76c1

Please sign in to comment.