Skip to content

Commit 415ed49

Browse files
committed
Merge branch 'main' into typewatch
* main: pythonGH-97002: Prevent `_PyInterpreterFrame`s from backing more than one `PyFrameObject` (pythonGH-97996) pythongh-97973: Return all necessary information from the tokenizer (pythonGH-97984) fixes pythongh-96078: os.sched_yield release the GIL while calling sched_yield(2). (pythongh-97965) pythongh-65961: Do not rely solely on `__cached__` (pythonGH-97990) pythongh-97850: Remove the open issues section from the import reference (python#97935) Docs: pin sphinx-lint (pythonGH-97992) pythongh-94590: add signatures to operator itemgetter, attrgetter, methodcaller (python#94591) Add Pynche's move to the What's new in 3.11 (python#97974) pythongh-97781: Apply changes from importlib_metadata 5. (pythonGH-97785) pythongh-86482: Document assignment expression need for ()s (python#23291) pythongh-97943: PyFunction_GetAnnotations should return a borrowed reference. (python#97949)
2 parents 1af2a98 + 21a2d9f commit 415ed49

35 files changed

+499
-518
lines changed

Doc/c-api/import.rst

+9
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,11 @@ Importing Modules
150150
See also :c:func:`PyImport_ExecCodeModuleEx` and
151151
:c:func:`PyImport_ExecCodeModuleWithPathnames`.
152152
153+
.. versionchanged:: 3.12
154+
The setting of :attr:`__cached__` and :attr:`__loader__` is
155+
deprecated. See :class:`~importlib.machinery.ModuleSpec` for
156+
alternatives.
157+
153158
154159
.. c:function:: PyObject* PyImport_ExecCodeModuleEx(const char *name, PyObject *co, const char *pathname)
155160
@@ -167,6 +172,10 @@ Importing Modules
167172
168173
.. versionadded:: 3.3
169174
175+
.. versionchanged:: 3.12
176+
Setting :attr:`__cached__` is deprecated. See
177+
:class:`~importlib.machinery.ModuleSpec` for alternatives.
178+
170179
171180
.. c:function:: PyObject* PyImport_ExecCodeModuleWithPathnames(const char *name, PyObject *co, const char *pathname, const char *cpathname)
172181

Doc/library/importlib.metadata.rst

+73-35
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,39 @@
1313

1414
**Source code:** :source:`Lib/importlib/metadata/__init__.py`
1515

16-
``importlib.metadata`` is a library that provides access to installed
17-
package metadata, such as its entry points or its
18-
top-level name. Built in part on Python's import system, this library
16+
``importlib_metadata`` is a library that provides access to
17+
the metadata of an installed `Distribution Package <https://packaging.python.org/en/latest/glossary/#term-Distribution-Package>`_,
18+
such as its entry points
19+
or its top-level names (`Import Package <https://packaging.python.org/en/latest/glossary/#term-Import-Package>`_\s, modules, if any).
20+
Built in part on Python's import system, this library
1921
intends to replace similar functionality in the `entry point
2022
API`_ and `metadata API`_ of ``pkg_resources``. Along with
2123
:mod:`importlib.resources`,
2224
this package can eliminate the need to use the older and less efficient
2325
``pkg_resources`` package.
2426

25-
By "installed package" we generally mean a third-party package installed into
26-
Python's ``site-packages`` directory via tools such as `pip
27-
<https://pypi.org/project/pip/>`_. Specifically,
28-
it means a package with either a discoverable ``dist-info`` or ``egg-info``
29-
directory, and metadata defined by :pep:`566` or its older specifications.
30-
By default, package metadata can live on the file system or in zip archives on
27+
``importlib_metadata`` operates on third-party *distribution packages*
28+
installed into Python's ``site-packages`` directory via tools such as
29+
`pip <https://pypi.org/project/pip/>`_.
30+
Specifically, it works with distributions with discoverable
31+
``dist-info`` or ``egg-info`` directories,
32+
and metadata defined by the `Core metadata specifications <https://packaging.python.org/en/latest/specifications/core-metadata/#core-metadata>`_.
33+
34+
.. important::
35+
36+
These are *not* necessarily equivalent to or correspond 1:1 with
37+
the top-level *import package* names
38+
that can be imported inside Python code.
39+
One *distribution package* can contain multiple *import packages*
40+
(and single modules),
41+
and one top-level *import package*
42+
may map to multiple *distribution packages*
43+
if it is a namespace package.
44+
You can use :ref:`package_distributions() <package-distributions>`
45+
to get a mapping between them.
46+
47+
By default, distribution metadata can live on the file system
48+
or in zip archives on
3149
:data:`sys.path`. Through an extension mechanism, the metadata can live almost
3250
anywhere.
3351

@@ -37,12 +55,19 @@ anywhere.
3755
https://importlib-metadata.readthedocs.io/
3856
The documentation for ``importlib_metadata``, which supplies a
3957
backport of ``importlib.metadata``.
58+
This includes an `API reference
59+
<https://importlib-metadata.readthedocs.io/en/latest/api.html>`__
60+
for this module's classes and functions,
61+
as well as a `migration guide
62+
<https://importlib-metadata.readthedocs.io/en/latest/migration.html>`__
63+
for existing users of ``pkg_resources``.
4064

4165

4266
Overview
4367
========
4468

45-
Let's say you wanted to get the version string for a package you've installed
69+
Let's say you wanted to get the version string for a
70+
`Distribution Package <https://packaging.python.org/en/latest/glossary/#term-Distribution-Package>`_ you've installed
4671
using ``pip``. We start by creating a virtual environment and installing
4772
something into it:
4873

@@ -151,19 +176,19 @@ for more information on entry points, their definition, and usage.
151176
The "selectable" entry points were introduced in ``importlib_metadata``
152177
3.6 and Python 3.10. Prior to those changes, ``entry_points`` accepted
153178
no parameters and always returned a dictionary of entry points, keyed
154-
by group. For compatibility, if no parameters are passed to entry_points,
155-
a ``SelectableGroups`` object is returned, implementing that dict
156-
interface. In the future, calling ``entry_points`` with no parameters
157-
will return an ``EntryPoints`` object. Users should rely on the selection
158-
interface to retrieve entry points by group.
179+
by group. With ``importlib_metadata`` 5.0 and Python 3.12,
180+
``entry_points`` always returns an ``EntryPoints`` object. See
181+
`backports.entry_points_selectable <https://pypi.org/project/backports.entry_points_selectable>`_
182+
for compatibility options.
159183

160184

161185
.. _metadata:
162186

163187
Distribution metadata
164188
---------------------
165189

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

169194
>>> wheel_metadata = metadata('wheel') # doctest: +SKIP
@@ -201,7 +226,8 @@ all the metadata in a JSON-compatible form per :PEP:`566`::
201226
Distribution versions
202227
---------------------
203228

204-
The ``version()`` function is the quickest way to get a distribution's version
229+
The ``version()`` function is the quickest way to get a
230+
`Distribution Package <https://packaging.python.org/en/latest/glossary/#term-Distribution-Package>`_'s version
205231
number, as a string::
206232

207233
>>> version('wheel') # doctest: +SKIP
@@ -214,7 +240,8 @@ Distribution files
214240
------------------
215241

216242
You can also get the full set of files contained within a distribution. The
217-
``files()`` function takes a distribution package name and returns all of the
243+
``files()`` function takes a `Distribution Package <https://packaging.python.org/en/latest/glossary/#term-Distribution-Package>`_ name
244+
and returns all of the
218245
files installed by this distribution. Each file object returned is a
219246
``PackagePath``, a :class:`pathlib.PurePath` derived object with additional ``dist``,
220247
``size``, and ``hash`` properties as indicated by the metadata. For example::
@@ -259,19 +286,24 @@ distribution is not known to have the metadata present.
259286
Distribution requirements
260287
-------------------------
261288

262-
To get the full set of requirements for a distribution, use the ``requires()``
289+
To get the full set of requirements for a `Distribution Package <https://packaging.python.org/en/latest/glossary/#term-Distribution-Package>`_,
290+
use the ``requires()``
263291
function::
264292

265293
>>> requires('wheel') # doctest: +SKIP
266294
["pytest (>=3.0.0) ; extra == 'test'", "pytest-cov ; extra == 'test'"]
267295

268296

269-
Package distributions
270-
---------------------
297+
.. _package-distributions:
298+
.. _import-distribution-package-mapping:
299+
300+
Mapping import to distribution packages
301+
---------------------------------------
271302

272-
A convenience method to resolve the distribution or
273-
distributions (in the case of a namespace package) for top-level
274-
Python packages or modules::
303+
A convenience method to resolve the `Distribution Package <https://packaging.python.org/en/latest/glossary/#term-Distribution-Package>`_
304+
name (or names, in the case of a namespace package)
305+
that provide each importable top-level
306+
Python module or `Import Package <https://packaging.python.org/en/latest/glossary/#term-Import-Package>`_::
275307

276308
>>> packages_distributions()
277309
{'importlib_metadata': ['importlib-metadata'], 'yaml': ['PyYAML'], 'jaraco': ['jaraco.classes', 'jaraco.functools'], ...}
@@ -285,7 +317,8 @@ Distributions
285317

286318
While the above API is the most common and convenient usage, you can get all
287319
of that information from the ``Distribution`` class. A ``Distribution`` is an
288-
abstract object that represents the metadata for a Python package. You can
320+
abstract object that represents the metadata for
321+
a Python `Distribution Package <https://packaging.python.org/en/latest/glossary/#term-Distribution-Package>`_. You can
289322
get the ``Distribution`` instance::
290323

291324
>>> from importlib.metadata import distribution # doctest: +SKIP
@@ -305,14 +338,16 @@ instance::
305338
>>> dist.metadata['License'] # doctest: +SKIP
306339
'MIT'
307340

308-
The full set of available metadata is not described here. See :pep:`566`
309-
for additional details.
341+
The full set of available metadata is not described here.
342+
See the `Core metadata specifications <https://packaging.python.org/en/latest/specifications/core-metadata/#core-metadata>`_ for additional details.
310343

311344

312345
Distribution Discovery
313346
======================
314347

315-
By default, this package provides built-in support for discovery of metadata for file system and zip file packages. This metadata finder search defaults to ``sys.path``, but varies slightly in how it interprets those values from how other import machinery does. In particular:
348+
By default, this package provides built-in support for discovery of metadata
349+
for file system and zip file `Distribution Package <https://packaging.python.org/en/latest/glossary/#term-Distribution-Package>`_\s.
350+
This metadata finder search defaults to ``sys.path``, but varies slightly in how it interprets those values from how other import machinery does. In particular:
316351

317352
- ``importlib.metadata`` does not honor :class:`bytes` objects on ``sys.path``.
318353
- ``importlib.metadata`` will incidentally honor :py:class:`pathlib.Path` objects on ``sys.path`` even though such values will be ignored for imports.
@@ -321,15 +356,18 @@ By default, this package provides built-in support for discovery of metadata for
321356
Extending the search algorithm
322357
==============================
323358

324-
Because package metadata is not available through :data:`sys.path` searches, or
325-
package loaders directly, the metadata for a package is found through import
326-
system :ref:`finders <finders-and-loaders>`. To find a distribution package's metadata,
359+
Because `Distribution Package <https://packaging.python.org/en/latest/glossary/#term-Distribution-Package>`_ metadata
360+
is not available through :data:`sys.path` searches, or
361+
package loaders directly,
362+
the metadata for a distribution is found through import
363+
system `finders`_. To find a distribution package's metadata,
327364
``importlib.metadata`` queries the list of :term:`meta path finders <meta path finder>` on
328365
:data:`sys.meta_path`.
329366

330-
The default ``PathFinder`` for Python includes a hook that calls into
331-
``importlib.metadata.MetadataPathFinder`` for finding distributions
332-
loaded from typical file-system-based paths.
367+
By default ``importlib_metadata`` installs a finder for distribution packages
368+
found on the file system.
369+
This finder doesn't actually find any *distributions*,
370+
but it can find their metadata.
333371

334372
The abstract class :py:class:`importlib.abc.MetaPathFinder` defines the
335373
interface expected of finders by Python's import system.
@@ -358,4 +396,4 @@ a custom finder, return instances of this derived ``Distribution`` in the
358396

359397
.. _`entry point API`: https://setuptools.readthedocs.io/en/latest/pkg_resources.html#entry-points
360398
.. _`metadata API`: https://setuptools.readthedocs.io/en/latest/pkg_resources.html#metadata-api
361-
.. _`importlib_resources`: https://importlib-resources.readthedocs.io/en/latest/index.html
399+
.. _`finders`: https://docs.python.org/3/reference/import.html#finders-and-loaders

Doc/library/runpy.rst

+9
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ The :mod:`runpy` module provides two functions:
9393
run this way, as well as ensuring the real module name is always
9494
accessible as ``__spec__.name``.
9595

96+
.. versionchanged:: 3.12
97+
The setting of ``__cached__``, ``__loader__``, and
98+
``__package__`` are deprecated. See
99+
:class:`~importlib.machinery.ModuleSpec` for alternatives.
100+
96101
.. function:: run_path(path_name, init_globals=None, run_name=None)
97102

98103
.. index::
@@ -163,6 +168,10 @@ The :mod:`runpy` module provides two functions:
163168
case where ``__main__`` is imported from a valid sys.path entry rather
164169
than being executed directly.
165170

171+
.. versionchanged:: 3.12
172+
The setting of ``__cached__``, ``__loader__``, and
173+
``__package__`` are deprecated.
174+
166175
.. seealso::
167176

168177
:pep:`338` -- Executing modules as scripts

Doc/reference/expressions.rst

+7
Original file line numberDiff line numberDiff line change
@@ -1766,6 +1766,13 @@ Or, when processing a file stream in chunks:
17661766
while chunk := file.read(9000):
17671767
process(chunk)
17681768
1769+
Assignment expressions must be surrounded by parentheses when used
1770+
as sub-expressions in slicing, conditional, lambda,
1771+
keyword-argument, and comprehension-if expressions
1772+
and in ``assert`` and ``with`` statements.
1773+
In all other places where they can be used, parentheses are not required,
1774+
including in ``if`` and ``while`` statements.
1775+
17691776
.. versionadded:: 3.8
17701777
See :pep:`572` for more details about assignment expressions.
17711778

Doc/reference/import.rst

-19
Original file line numberDiff line numberDiff line change
@@ -1025,25 +1025,6 @@ and ``__main__.__spec__`` is set accordingly, they're still considered
10251025
to populate the ``__main__`` namespace, and not during normal import.
10261026

10271027

1028-
Open issues
1029-
===========
1030-
1031-
XXX It would be really nice to have a diagram.
1032-
1033-
XXX * (import_machinery.rst) how about a section devoted just to the
1034-
attributes of modules and packages, perhaps expanding upon or supplanting the
1035-
related entries in the data model reference page?
1036-
1037-
XXX runpy, pkgutil, et al in the library manual should all get "See Also"
1038-
links at the top pointing to the new import system section.
1039-
1040-
XXX Add more explanation regarding the different ways in which
1041-
``__main__`` is initialized?
1042-
1043-
XXX Add more info on ``__main__`` quirks/pitfalls (i.e. copy from
1044-
:pep:`395`).
1045-
1046-
10471028
References
10481029
==========
10491030

Doc/requirements.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ blurb
1010
# sphinx-lint 0.6.2 yields many default role errors due to the new regular
1111
# expression used for default role detection, so we don't use the version
1212
# until the errors are fixed.
13-
sphinx-lint<1,!=0.6.2
13+
sphinx-lint==0.6.1
1414

1515
# The theme used by the documentation is stored separately, so we need
1616
# to install that as well.

Doc/whatsnew/3.11.rst

+4
Original file line numberDiff line numberDiff line change
@@ -1667,6 +1667,10 @@ Removed
16671667
Python's test suite."
16681668
(Contributed by Victor Stinner in :issue:`46852`.)
16691669

1670+
* Pynche --- The Pythonically Natural Color and Hue Editor --- has been moved out
1671+
of ``Tools/scripts`` and is `being developed independently
1672+
<https://gitlab.com/warsaw/pynche/-/tree/main>`_ from the Python source tree.
1673+
16701674
Porting to Python 3.11
16711675
======================
16721676

Doc/whatsnew/3.12.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,8 @@ Pending Removal in Python 3.14
280280
* Creating :c:data:`immutable types <Py_TPFLAGS_IMMUTABLETYPE>` with mutable
281281
bases using the C API.
282282

283-
* ``__package__`` will cease to be set or taken into consideration by
284-
the import system (:gh:`97879`).
283+
* ``__package__`` and ``__cached__`` will cease to be set or taken
284+
into consideration by the import system (:gh:`97879`).
285285

286286

287287
Pending Removal in Future Versions

Lib/cProfile.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
__all__ = ["run", "runctx", "Profile"]
88

99
import _lsprof
10+
import importlib.machinery
1011
import profile as _pyprofile
1112

1213
# ____________________________________________________________
@@ -169,9 +170,12 @@ def main():
169170
sys.path.insert(0, os.path.dirname(progname))
170171
with open(progname, 'rb') as fp:
171172
code = compile(fp.read(), progname, 'exec')
173+
spec = importlib.machinery.ModuleSpec(name='__main__', loader=None,
174+
origin=progname)
172175
globs = {
173-
'__file__': progname,
174-
'__name__': '__main__',
176+
'__spec__': spec,
177+
'__file__': spec.origin,
178+
'__name__': spec.name,
175179
'__package__': None,
176180
'__cached__': None,
177181
}

0 commit comments

Comments
 (0)