Skip to content

Commit

Permalink
Merge branch 'main' into pythongh-117337-deprecate-glob0-glob1
Browse files Browse the repository at this point in the history
  • Loading branch information
barneygale committed Apr 1, 2024
2 parents 69b4606 + c741ad3 commit f9074ff
Show file tree
Hide file tree
Showing 56 changed files with 1,153 additions and 668 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/project-updater.yml
Expand Up @@ -23,7 +23,7 @@ jobs:
- { project: 32, label: sprint }

steps:
- uses: actions/add-to-project@v0.6.0
- uses: actions/add-to-project@v1.0.0
with:
project-url: https://github.com/orgs/python/projects/${{ matrix.project }}
github-token: ${{ secrets.ADD_TO_PROJECT_PAT }}
Expand Down
61 changes: 48 additions & 13 deletions Doc/library/asyncio-task.rst
Expand Up @@ -867,19 +867,50 @@ Waiting Primitives

.. function:: as_completed(aws, *, timeout=None)

Run :ref:`awaitable objects <asyncio-awaitables>` in the *aws*
iterable concurrently. Return an iterator of coroutines.
Each coroutine returned can be awaited to get the earliest next
result from the iterable of the remaining awaitables.

Raises :exc:`TimeoutError` if the timeout occurs before
all Futures are done.

Example::

for coro in as_completed(aws):
earliest_result = await coro
# ...
Run :ref:`awaitable objects <asyncio-awaitables>` in the *aws* iterable
concurrently. The returned object can be iterated to obtain the results
of the awaitables as they finish.

The object returned by ``as_completed()`` can be iterated as an
:term:`asynchronous iterator` or a plain :term:`iterator`. When asynchronous
iteration is used, the originally-supplied awaitables are yielded if they
are tasks or futures. This makes it easy to correlate previously-scheduled
tasks with their results. Example::

ipv4_connect = create_task(open_connection("127.0.0.1", 80))
ipv6_connect = create_task(open_connection("::1", 80))
tasks = [ipv4_connect, ipv6_connect]

async for earliest_connect in as_completed(tasks):
# earliest_connect is done. The result can be obtained by
# awaiting it or calling earliest_connect.result()
reader, writer = await earliest_connect

if earliest_connect is ipv6_connect:
print("IPv6 connection established.")
else:
print("IPv4 connection established.")

During asynchronous iteration, implicitly-created tasks will be yielded for
supplied awaitables that aren't tasks or futures.

When used as a plain iterator, each iteration yields a new coroutine that
returns the result or raises the exception of the next completed awaitable.
This pattern is compatible with Python versions older than 3.13::

ipv4_connect = create_task(open_connection("127.0.0.1", 80))
ipv6_connect = create_task(open_connection("::1", 80))
tasks = [ipv4_connect, ipv6_connect]

for next_connect in as_completed(tasks):
# next_connect is not one of the original task objects. It must be
# awaited to obtain the result value or raise the exception of the
# awaitable that finishes next.
reader, writer = await next_connect

A :exc:`TimeoutError` is raised if the timeout occurs before all awaitables
are done. This is raised by the ``async for`` loop during asynchronous
iteration or by the coroutines yielded during plain iteration.

.. versionchanged:: 3.10
Removed the *loop* parameter.
Expand All @@ -891,6 +922,10 @@ Waiting Primitives
.. versionchanged:: 3.12
Added support for generators yielding tasks.

.. versionchanged:: 3.13
The result can now be used as either an :term:`asynchronous iterator`
or as a plain :term:`iterator` (previously it was only a plain iterator).


Running in Threads
==================
Expand Down
4 changes: 2 additions & 2 deletions Doc/library/pathlib.rst
Expand Up @@ -303,10 +303,10 @@ Methods and properties

Pure paths provide the following methods and properties:

.. attribute:: PurePath.pathmod
.. attribute:: PurePath.parser

The implementation of the :mod:`os.path` module used for low-level path
operations: either :mod:`posixpath` or :mod:`ntpath`.
parsing and joining: either :mod:`posixpath` or :mod:`ntpath`.

.. versionadded:: 3.13

Expand Down
8 changes: 7 additions & 1 deletion Doc/library/stdtypes.rst
Expand Up @@ -2339,7 +2339,13 @@ String objects have one unique built-in operation: the ``%`` operator (modulo).
This is also known as the string *formatting* or *interpolation* operator.
Given ``format % values`` (where *format* is a string), ``%`` conversion
specifications in *format* are replaced with zero or more elements of *values*.
The effect is similar to using the :c:func:`sprintf` in the C language.
The effect is similar to using the :c:func:`sprintf` function in the C language.
For example:

.. doctest::

>>> print('%s has %d quote types.' % ('Python', 2))
Python has 2 quote types.

If *format* requires a single argument, *values* may be a single non-tuple
object. [5]_ Otherwise, *values* must be a tuple with exactly the number of
Expand Down
88 changes: 15 additions & 73 deletions Doc/library/typing.rst
Expand Up @@ -23,27 +23,25 @@

--------------

This module provides runtime support for type hints. For the original
specification of the typing system, see :pep:`484`. For a simplified
introduction to type hints, see :pep:`483`.
This module provides runtime support for type hints.

Consider the function below::

The function below takes and returns a string and is annotated as follows::
def moon_weight(earth_weight: float) -> str:
return f'On the moon, you would weigh {earth_weight * 0.166} kilograms.'

def greeting(name: str) -> str:
return 'Hello ' + name
The function ``moon_weight`` takes an argument expected to be an instance of :class:`float`,
as indicated by the *type hint* ``earth_weight: float``. The function is expected to
return an instance of :class:`str`, as indicated by the ``-> str`` hint.

In the function ``greeting``, the argument ``name`` is expected to be of type
:class:`str` and the return type :class:`str`. Subtypes are accepted as
arguments.
While type hints can be simple classes like :class:`float` or :class:`str`,
they can also be more complex. The :mod:`typing` module provides a vocabulary of
more advanced type hints.

New features are frequently added to the ``typing`` module.
The `typing_extensions <https://pypi.org/project/typing-extensions/>`_ package
provides backports of these new features to older versions of Python.

For a summary of deprecated features and a deprecation timeline, please see
`Deprecation Timeline of Major Features`_.

.. seealso::

`"Typing cheat sheet" <https://mypy.readthedocs.io/en/stable/cheat_sheet_py3.html>`_
Expand All @@ -61,67 +59,11 @@ For a summary of deprecated features and a deprecation timeline, please see

.. _relevant-peps:

Relevant PEPs
=============

Since the initial introduction of type hints in :pep:`484` and :pep:`483`, a
number of PEPs have modified and enhanced Python's framework for type
annotations:

.. raw:: html

<details>
<summary><a style="cursor:pointer;">The full list of PEPs</a></summary>

* :pep:`526`: Syntax for Variable Annotations
*Introducing* syntax for annotating variables outside of function
definitions, and :data:`ClassVar`
* :pep:`544`: Protocols: Structural subtyping (static duck typing)
*Introducing* :class:`Protocol` and the
:func:`@runtime_checkable<runtime_checkable>` decorator
* :pep:`585`: Type Hinting Generics In Standard Collections
*Introducing* :class:`types.GenericAlias` and the ability to use standard
library classes as :ref:`generic types<types-genericalias>`
* :pep:`586`: Literal Types
*Introducing* :data:`Literal`
* :pep:`589`: TypedDict: Type Hints for Dictionaries with a Fixed Set of Keys
*Introducing* :class:`TypedDict`
* :pep:`591`: Adding a final qualifier to typing
*Introducing* :data:`Final` and the :func:`@final<final>` decorator
* :pep:`593`: Flexible function and variable annotations
*Introducing* :data:`Annotated`
* :pep:`604`: Allow writing union types as ``X | Y``
*Introducing* :data:`types.UnionType` and the ability to use
the binary-or operator ``|`` to signify a
:ref:`union of types<types-union>`
* :pep:`612`: Parameter Specification Variables
*Introducing* :class:`ParamSpec` and :data:`Concatenate`
* :pep:`613`: Explicit Type Aliases
*Introducing* :data:`TypeAlias`
* :pep:`646`: Variadic Generics
*Introducing* :data:`TypeVarTuple`
* :pep:`647`: User-Defined Type Guards
*Introducing* :data:`TypeGuard`
* :pep:`655`: Marking individual TypedDict items as required or potentially missing
*Introducing* :data:`Required` and :data:`NotRequired`
* :pep:`673`: Self type
*Introducing* :data:`Self`
* :pep:`675`: Arbitrary Literal String Type
*Introducing* :data:`LiteralString`
* :pep:`681`: Data Class Transforms
*Introducing* the :func:`@dataclass_transform<dataclass_transform>` decorator
* :pep:`692`: Using ``TypedDict`` for more precise ``**kwargs`` typing
*Introducing* a new way of typing ``**kwargs`` with :data:`Unpack` and
:data:`TypedDict`
* :pep:`695`: Type Parameter Syntax
*Introducing* builtin syntax for creating generic functions, classes, and type aliases.
* :pep:`698`: Adding an override decorator to typing
*Introducing* the :func:`@override<override>` decorator

.. raw:: html

</details>
<br>
Specification for the Python Type System
========================================

The canonical, up-to-date specification of the Python type system can be
found at `"Specification for the Python type system" <https://typing.readthedocs.io/en/latest/spec/index.html>`_.

.. _type-aliases:

Expand Down
11 changes: 11 additions & 0 deletions Doc/whatsnew/3.13.rst
Expand Up @@ -289,6 +289,13 @@ asyncio
forcefully close an asyncio server.
(Contributed by Pierre Ossman in :gh:`113538`.)

* :func:`asyncio.as_completed` now returns an object that is both an
:term:`asynchronous iterator` and a plain :term:`iterator` of awaitables.
The awaitables yielded by asynchronous iteration include original task or
future objects that were passed in, making it easier to associate results
with the tasks being completed.
(Contributed by Justin Arthur in :gh:`77714`.)

base64
------

Expand Down Expand Up @@ -524,6 +531,10 @@ pathlib
shell-style wildcards, including the recursive wildcard "``**``".
(Contributed by Barney Gale in :gh:`73435`.)

* Add :attr:`pathlib.PurePath.parser` class attribute that stores the
implementation of :mod:`os.path` used for low-level path parsing and
joining: either ``posixpath`` or ``ntpath``.

* Add *follow_symlinks* keyword-only argument to :meth:`pathlib.Path.glob`,
:meth:`~pathlib.Path.rglob`, :meth:`~pathlib.Path.is_file`,
:meth:`~pathlib.Path.is_dir`, :meth:`~pathlib.Path.owner`,
Expand Down
48 changes: 48 additions & 0 deletions Include/internal/pycore_cell.h
@@ -0,0 +1,48 @@
#ifndef Py_INTERNAL_CELL_H
#define Py_INTERNAL_CELL_H

#include "pycore_critical_section.h"

#ifdef __cplusplus
extern "C" {
#endif

#ifndef Py_BUILD_CORE
# error "this header requires Py_BUILD_CORE define"
#endif

// Sets the cell contents to `value` and return previous contents. Steals a
// reference to `value`.
static inline PyObject *
PyCell_SwapTakeRef(PyCellObject *cell, PyObject *value)
{
PyObject *old_value;
Py_BEGIN_CRITICAL_SECTION(cell);
old_value = cell->ob_ref;
cell->ob_ref = value;
Py_END_CRITICAL_SECTION();
return old_value;
}

static inline void
PyCell_SetTakeRef(PyCellObject *cell, PyObject *value)
{
PyObject *old_value = PyCell_SwapTakeRef(cell, value);
Py_XDECREF(old_value);
}

// Gets the cell contents. Returns a new reference.
static inline PyObject *
PyCell_GetRef(PyCellObject *cell)
{
PyObject *res;
Py_BEGIN_CRITICAL_SECTION(cell);
res = Py_XNewRef(cell->ob_ref);
Py_END_CRITICAL_SECTION();
return res;
}

#ifdef __cplusplus
}
#endif
#endif /* !Py_INTERNAL_CELL_H */

0 comments on commit f9074ff

Please sign in to comment.