Skip to content

Commit

Permalink
Merge branch '3.13' into backport-9b31a2d-3.13
Browse files Browse the repository at this point in the history
  • Loading branch information
serhiy-storchaka authored Oct 8, 2024
2 parents 2d34c78 + 761c3b2 commit 04f4178
Show file tree
Hide file tree
Showing 14 changed files with 428 additions and 111 deletions.
188 changes: 144 additions & 44 deletions Doc/library/importlib.metadata.rst
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ You can also get a :ref:`distribution's version number <version>`, list its
:ref:`requirements`.


.. exception:: PackageNotFoundError

Subclass of :class:`ModuleNotFoundError` raised by several functions in this
module when queried for a distribution package which is not installed in the
current Python environment.


Functional API
==============

Expand All @@ -111,31 +118,53 @@ This package provides the following functionality via its public API.
Entry points
------------

The ``entry_points()`` function returns a collection of entry points.
Entry points are represented by ``EntryPoint`` instances;
each ``EntryPoint`` has a ``.name``, ``.group``, and ``.value`` attributes and
a ``.load()`` method to resolve the value. There are also ``.module``,
``.attr``, and ``.extras`` attributes for getting the components of the
``.value`` attribute.
.. function:: entry_points(**select_params)

Returns a :class:`EntryPoints` instance describing entry points for the
current environment. Any given keyword parameters are passed to the
:meth:`!select` method for comparison to the attributes of
the individual entry point definitions.

Note: it is not currently possible to query for entry points based on
their :attr:`!EntryPoint.dist` attribute (as different :class:`!Distribution`
instances do not currently compare equal, even if they have the same attributes)

.. class:: EntryPoints

Details of a collection of installed entry points.

Also provides a ``.groups`` attribute that reports all identifed entry
point groups, and a ``.names`` attribute that reports all identified entry
point names.

.. class:: EntryPoint

Details of an installed entry point.

Each :class:`!EntryPoint` instance has ``.name``, ``.group``, and ``.value``
attributes and a ``.load()`` method to resolve the value. There are also
``.module``, ``.attr``, and ``.extras`` attributes for getting the
components of the ``.value`` attribute, and ``.dist`` for obtaining
information regarding the distribution package that provides the entry point.

Query all entry points::

>>> eps = entry_points() # doctest: +SKIP

The ``entry_points()`` function returns an ``EntryPoints`` object,
a collection of all ``EntryPoint`` objects with ``names`` and ``groups``
The :func:`!entry_points` function returns a :class:`!EntryPoints` object,
a collection of all :class:`!EntryPoint` objects with ``names`` and ``groups``
attributes for convenience::

>>> sorted(eps.groups) # doctest: +SKIP
['console_scripts', 'distutils.commands', 'distutils.setup_keywords', 'egg_info.writers', 'setuptools.installation']

``EntryPoints`` has a ``select`` method to select entry points
:class:`!EntryPoints` has a :meth:`!select` method to select entry points
matching specific properties. Select entry points in the
``console_scripts`` group::

>>> scripts = eps.select(group='console_scripts') # doctest: +SKIP

Equivalently, since ``entry_points`` passes keyword arguments
Equivalently, since :func:`!entry_points` passes keyword arguments
through to select::

>>> scripts = entry_points(group='console_scripts') # doctest: +SKIP
Expand Down Expand Up @@ -189,31 +218,41 @@ for more information on entry points, their definition, and usage.
Distribution metadata
---------------------

Every `Distribution Package <https://packaging.python.org/en/latest/glossary/#term-Distribution-Package>`_ includes some metadata,
which you can extract using the
``metadata()`` function::
.. function:: metadata(distribution_name)

Return the distribution metadata corresponding to the named
distribution package as a :class:`PackageMetadata` instance.

Raises :exc:`PackageNotFoundError` if the named distribution
package is not installed in the current Python environment.

.. class:: PackageMetadata

A concrete implementation of the
`PackageMetadata protocol <https://importlib-metadata.readthedocs.io/en/latest/api.html#importlib_metadata.PackageMetadata>`_.

In addition to providing the defined protocol methods and attributes, subscripting
the instance is equivalent to calling the :meth:`!get` method.

Every `Distribution Package <https://packaging.python.org/en/latest/glossary/#term-Distribution-Package>`_
includes some metadata, which you can extract using the :func:`!metadata` function::

>>> wheel_metadata = metadata('wheel') # doctest: +SKIP

The keys of the returned data structure, a ``PackageMetadata``,
name the metadata keywords, and
The keys of the returned data structure name the metadata keywords, and
the values are returned unparsed from the distribution metadata::

>>> wheel_metadata['Requires-Python'] # doctest: +SKIP
'>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*'

``PackageMetadata`` also presents a ``json`` attribute that returns
:class:`PackageMetadata` also presents a :attr:`!json` attribute that returns
all the metadata in a JSON-compatible form per :PEP:`566`::

>>> wheel_metadata.json['requires_python']
'>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*'

.. note::

The actual type of the object returned by ``metadata()`` is an
implementation detail and should be accessed only through the interface
described by the
`PackageMetadata protocol <https://importlib-metadata.readthedocs.io/en/latest/api.html#importlib_metadata.PackageMetadata>`_.
The full set of available metadata is not described here.
See the PyPA `Core metadata specification <https://packaging.python.org/en/latest/specifications/core-metadata/#core-metadata>`_ for additional details.

.. versionchanged:: 3.10
The ``Description`` is now included in the metadata when presented
Expand All @@ -227,7 +266,15 @@ all the metadata in a JSON-compatible form per :PEP:`566`::
Distribution versions
---------------------

The ``version()`` function is the quickest way to get a
.. function:: version(distribution_name)

Return the installed distribution package version for the named
distribution package.

Raises :exc:`PackageNotFoundError` if the named distribution
package is not installed in the current Python environment.

The :func:`!version` function is the quickest way to get a
`Distribution Package <https://packaging.python.org/en/latest/glossary/#term-Distribution-Package>`_'s version
number, as a string::

Expand All @@ -240,12 +287,28 @@ number, as a string::
Distribution files
------------------

You can also get the full set of files contained within a distribution. The
``files()`` function takes a `Distribution Package <https://packaging.python.org/en/latest/glossary/#term-Distribution-Package>`_ name
and returns all of the
files installed by this distribution. Each file object returned is a
``PackagePath``, a :class:`pathlib.PurePath` derived object with additional ``dist``,
``size``, and ``hash`` properties as indicated by the metadata. For example::
.. function:: files(distribution_name)

Return the full set of files contained within the named
distribution package.

Raises :exc:`PackageNotFoundError` if the named distribution
package is not installed in the current Python environment.

Returns :const:`None` if the distribution is found but the installation
database records reporting the files associated with the distribuion package
are missing.

.. class:: PackagePath

A :class:`pathlib.PurePath` derived object with additional ``dist``,
``size``, and ``hash`` properties corresponding to the distribution
package's installation metadata for that file.

The :func:`!files` function takes a
`Distribution Package <https://packaging.python.org/en/latest/glossary/#term-Distribution-Package>`_
name and returns all of the files installed by this distribution. Each file is reported
as a :class:`PackagePath` instance. For example::

>>> util = [p for p in files('wheel') if 'util.py' in str(p)][0] # doctest: +SKIP
>>> util # doctest: +SKIP
Expand All @@ -268,16 +331,16 @@ Once you have the file, you can also read its contents::
return s.encode('utf-8')
return s

You can also use the ``locate`` method to get a the absolute path to the
file::
You can also use the :meth:`!locate` method to get the absolute
path to the file::

>>> util.locate() # doctest: +SKIP
PosixPath('/home/gustav/example/lib/site-packages/wheel/util.py')

In the case where the metadata file listing files
(RECORD or SOURCES.txt) is missing, ``files()`` will
return ``None``. The caller may wish to wrap calls to
``files()`` in `always_iterable
(``RECORD`` or ``SOURCES.txt``) is missing, :func:`!files` will
return :const:`None`. The caller may wish to wrap calls to
:func:`!files` in `always_iterable
<https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.always_iterable>`_
or otherwise guard against this condition if the target
distribution is not known to have the metadata present.
Expand All @@ -287,8 +350,16 @@ distribution is not known to have the metadata present.
Distribution requirements
-------------------------

.. function:: requires(distribution_name)

Return the declared dependency specifiers for the named
distribution package.

Raises :exc:`PackageNotFoundError` if the named distribution
package is not installed in the current Python environment.

To get the full set of requirements for a `Distribution Package <https://packaging.python.org/en/latest/glossary/#term-Distribution-Package>`_,
use the ``requires()``
use the :func:`!requires`
function::

>>> requires('wheel') # doctest: +SKIP
Expand All @@ -301,6 +372,16 @@ function::
Mapping import to distribution packages
---------------------------------------

.. function:: packages_distributions()

Return a mapping from the top level module and import package
names found via :attr:`sys.meta_path` to the names of the distribution
packages (if any) that provide the corresponding files.

To allow for namespace packages (which may have members provided by
multiple distribution packages), each top level import name maps to a
list of distribution names rather than mapping directly to a single name.

A convenience method to resolve the `Distribution Package <https://packaging.python.org/en/latest/glossary/#term-Distribution-Package>`_
name (or names, in the case of a namespace package)
that provide each importable top-level
Expand All @@ -320,23 +401,42 @@ function is not reliable with such installs.
Distributions
=============

While the above API is the most common and convenient usage, you can get all
of that information from the ``Distribution`` class. A ``Distribution`` is an
abstract object that represents the metadata for
a Python `Distribution Package <https://packaging.python.org/en/latest/glossary/#term-Distribution-Package>`_. You can
get the ``Distribution`` instance::
.. function:: distribution(distribution_name)

Return a :class:`Distribution` instance describing the named
distribution package.

Raises :exc:`PackageNotFoundError` if the named distribution
package is not installed in the current Python environment.

.. class:: Distribution

Details of an installed distribution package.

Note: different :class:`!Distribution` instances do not currently compare
equal, even if they relate to the same installed distribution and
accordingly have the same attributes.

While the module level API described above is the most common and convenient usage,
you can get all of that information from the :class:`!Distribution` class.
:class:`!Distribution` is an abstract object that represents the metadata for
a Python `Distribution Package <https://packaging.python.org/en/latest/glossary/#term-Distribution-Package>`_.
You can get the concreate :class:`!Distribution` subclass instance for an installed
distribution package by calling the :func:`distribution` function::

>>> from importlib.metadata import distribution # doctest: +SKIP
>>> dist = distribution('wheel') # doctest: +SKIP
>>> type(dist) # doctest: +SKIP
<class 'importlib.metadata.PathDistribution'>

Thus, an alternative way to get the version number is through the
``Distribution`` instance::
:class:`!Distribution` instance::

>>> dist.version # doctest: +SKIP
'0.32.3'

There are all kinds of additional metadata available on the ``Distribution``
instance::
There are all kinds of additional metadata available on :class:`!Distribution`
instances::

>>> dist.metadata['Requires-Python'] # doctest: +SKIP
'>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*'
Expand All @@ -350,7 +450,7 @@ metadata::
'file:///path/to/wheel-0.32.3.editable-py3-none-any.whl'

The full set of available metadata is not described here.
See the `Core metadata specifications <https://packaging.python.org/en/latest/specifications/core-metadata/#core-metadata>`_ for additional details.
See the PyPA `Core metadata specification <https://packaging.python.org/en/latest/specifications/core-metadata/#core-metadata>`_ for additional details.

.. versionadded:: 3.13
The ``.origin`` property was added.
Expand Down
16 changes: 15 additions & 1 deletion Doc/library/itertools.rst
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ loops that truncate the stream.
If *start* is zero or ``None``, iteration starts at zero. Otherwise,
elements from the iterable are skipped until *start* is reached.

If *stop* is ``None``, iteration continues until the iterator is
If *stop* is ``None``, iteration continues until the iterable is
exhausted, if at all. Otherwise, it stops at the specified position.

If *step* is ``None``, the step defaults to one. Elements are returned
Expand Down Expand Up @@ -503,6 +503,10 @@ loops that truncate the stream.
yield element
next_i += step

If the input is an iterator, then fully consuming the *islice*
advances the input iterator by ``max(start, stop)`` steps regardless
of the *step* value.


.. function:: pairwise(iterable)

Expand Down Expand Up @@ -601,6 +605,8 @@ loops that truncate the stream.
# product('ABCD', 'xy') → Ax Ay Bx By Cx Cy Dx Dy
# product(range(2), repeat=3) → 000 001 010 011 100 101 110 111

if repeat < 0:
raise ValueError('repeat argument cannot be negative')
pools = [tuple(pool) for pool in iterables] * repeat

result = [[]]
Expand Down Expand Up @@ -684,6 +690,8 @@ loops that truncate the stream.
Roughly equivalent to::

def tee(iterable, n=2):
if n < 0:
raise ValueError('n must be >= 0')
iterator = iter(iterable)
shared_link = [None, None]
return tuple(_tee(iterator, shared_link) for _ in range(n))
Expand All @@ -703,6 +711,12 @@ loops that truncate the stream.
used anywhere else; otherwise, the *iterable* could get advanced without
the tee objects being informed.

When the input *iterable* is already a tee iterator object, all
members of the return tuple are constructed as if they had been
produced by the upstream :func:`tee` call. This "flattening step"
allows nested :func:`tee` calls to share the same underlying data
chain and to have a single update step rather than a chain of calls.

``tee`` iterators are not threadsafe. A :exc:`RuntimeError` may be
raised when simultaneously using iterators returned by the same :func:`tee`
call, even if the original *iterable* is threadsafe.
Expand Down
2 changes: 2 additions & 0 deletions Doc/reference/expressions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1809,6 +1809,8 @@ returns a boolean value regardless of the type of its argument
single: named expression
pair: assignment; expression

.. _assignment-expressions:

Assignment expressions
======================

Expand Down
4 changes: 2 additions & 2 deletions Doc/tools/extensions/pyspecific.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,8 @@ def run(self):
# Support for building "topic help" for pydoc

pydoc_topic_labels = [
'assert', 'assignment', 'async', 'atom-identifiers', 'atom-literals',
'attribute-access', 'attribute-references', 'augassign', 'await',
'assert', 'assignment', 'assignment-expressions', 'async', 'atom-identifiers',
'atom-literals', 'attribute-access', 'attribute-references', 'augassign', 'await',
'binary', 'bitwise', 'bltin-code-objects', 'bltin-ellipsis-object',
'bltin-null-object', 'bltin-type-objects', 'booleans',
'break', 'callable-types', 'calls', 'class', 'comparisons', 'compound',
Expand Down
Loading

0 comments on commit 04f4178

Please sign in to comment.