Skip to content

Commit d7e2100

Browse files
authored
bpo-37800: Clean up importlib documentation for some module attributes (GH-10016)
Automerge-Triggered-By: GH:brettcannon
1 parent df4ae55 commit d7e2100

File tree

1 file changed

+77
-63
lines changed

1 file changed

+77
-63
lines changed

Doc/library/importlib.rst

+77-63
Original file line numberDiff line numberDiff line change
@@ -383,11 +383,11 @@ ABC hierarchy::
383383
See :pep:`302` for the exact definition for a loader.
384384

385385
Loaders that wish to support resource reading should implement a
386-
``get_resource_reader(fullname)`` method as specified by
386+
:meth:`get_resource_reader` method as specified by
387387
:class:`importlib.abc.ResourceReader`.
388388

389389
.. versionchanged:: 3.7
390-
Introduced the optional ``get_resource_reader()`` method.
390+
Introduced the optional :meth:`get_resource_reader` method.
391391

392392
.. method:: create_module(spec)
393393

@@ -405,80 +405,82 @@ ABC hierarchy::
405405

406406
An abstract method that executes the module in its own namespace
407407
when a module is imported or reloaded. The module should already
408-
be initialized when ``exec_module()`` is called. When this method exists,
409-
:meth:`~importlib.abc.Loader.create_module` must be defined.
408+
be initialized when :meth:`exec_module` is called. When this method exists,
409+
:meth:`create_module` must be defined.
410410

411411
.. versionadded:: 3.4
412412

413413
.. versionchanged:: 3.6
414-
:meth:`~importlib.abc.Loader.create_module` must also be defined.
414+
:meth:`create_module` must also be defined.
415415

416416
.. method:: load_module(fullname)
417417

418-
A legacy method for loading a module. If the module cannot be
418+
A legacy method for loading a module. If the module cannot be
419419
loaded, :exc:`ImportError` is raised, otherwise the loaded module is
420420
returned.
421421

422422
If the requested module already exists in :data:`sys.modules`, that
423423
module should be used and reloaded.
424424
Otherwise the loader should create a new module and insert it into
425425
:data:`sys.modules` before any loading begins, to prevent recursion
426-
from the import. If the loader inserted a module and the load fails, it
426+
from the import. If the loader inserted a module and the load fails, it
427427
must be removed by the loader from :data:`sys.modules`; modules already
428428
in :data:`sys.modules` before the loader began execution should be left
429429
alone (see :func:`importlib.util.module_for_loader`).
430430

431-
The loader should set several attributes on the module.
432-
(Note that some of these attributes can change when a module is
431+
The loader should set several attributes on the module
432+
(note that some of these attributes can change when a module is
433433
reloaded):
434434

435435
- :attr:`__name__`
436-
The name of the module.
436+
The module's fully-qualified name.
437+
It is ``'__main__'`` for an executed module.
437438

438439
- :attr:`__file__`
439-
The path to where the module data is stored (not set for built-in
440-
modules).
440+
The location the :term:`loader` used to load the module.
441+
For example, for modules loaded from a .py file this is the filename.
442+
It is not set on all modules (e.g. built-in modules).
441443

442444
- :attr:`__cached__`
443-
The path to where a compiled version of the module is/should be
444-
stored (not set when the attribute would be inappropriate).
445+
The filename of a compiled version of the module's code.
446+
It is not set on all modules (e.g. built-in modules).
445447

446448
- :attr:`__path__`
447-
A list of strings specifying the search path within a
448-
package. This attribute is not set on modules.
449+
The list of locations where the package's submodules will be found.
450+
Most of the time this is a single directory.
451+
The import system passes this attribute to ``__import__()`` and to finders
452+
in the same way as :attr:`sys.path` but just for the package.
453+
It is not set on non-package modules so it can be used
454+
as an indicator that the module is a package.
449455

450456
- :attr:`__package__`
451-
The fully-qualified name of the package under which the module was
452-
loaded as a submodule (or the empty string for top-level modules).
453-
For packages, it is the same as :attr:`__name__`. The
454-
:func:`importlib.util.module_for_loader` decorator can handle the
455-
details for :attr:`__package__`.
457+
The fully-qualified name of the package the module is in (or the
458+
empty string for a top-level module).
459+
If the module is a package then this is the same as :attr:`__name__`.
456460

457461
- :attr:`__loader__`
458-
The loader used to load the module. The
459-
:func:`importlib.util.module_for_loader` decorator can handle the
460-
details for :attr:`__package__`.
462+
The :term:`loader` used to load the module.
461463

462464
When :meth:`exec_module` is available then backwards-compatible
463465
functionality is provided.
464466

465467
.. versionchanged:: 3.4
466468
Raise :exc:`ImportError` when called instead of
467-
:exc:`NotImplementedError`. Functionality provided when
469+
:exc:`NotImplementedError`. Functionality provided when
468470
:meth:`exec_module` is available.
469471

470472
.. deprecated:: 3.4
471473
The recommended API for loading a module is :meth:`exec_module`
472-
(and :meth:`create_module`). Loaders should implement
473-
it instead of load_module(). The import machinery takes care of
474-
all the other responsibilities of load_module() when exec_module()
475-
is implemented.
474+
(and :meth:`create_module`). Loaders should implement it instead of
475+
:meth:`load_module`. The import machinery takes care of all the
476+
other responsibilities of :meth:`load_module` when
477+
:meth:`exec_module` is implemented.
476478

477479
.. method:: module_repr(module)
478480

479-
A legacy method which when implemented calculates and returns the
480-
given module's repr, as a string. The module type's default repr() will
481-
use the result of this method as appropriate.
481+
A legacy method which when implemented calculates and returns the given
482+
module's representation, as a string. The module type's default
483+
:meth:`__repr__` will use the result of this method as appropriate.
482484

483485
.. versionadded:: 3.3
484486

@@ -1420,69 +1422,80 @@ find and load modules.
14201422
.. class:: ModuleSpec(name, loader, *, origin=None, loader_state=None, is_package=None)
14211423

14221424
A specification for a module's import-system-related state. This is
1423-
typically exposed as the module's ``__spec__`` attribute. In the
1425+
typically exposed as the module's :attr:`__spec__` attribute. In the
14241426
descriptions below, the names in parentheses give the corresponding
1425-
attribute available directly on the module object.
1426-
E.g. ``module.__spec__.origin == module.__file__``. Note however that
1427+
attribute available directly on the module object,
1428+
e.g. ``module.__spec__.origin == module.__file__``. Note, however, that
14271429
while the *values* are usually equivalent, they can differ since there is
1428-
no synchronization between the two objects. Thus it is possible to update
1429-
the module's ``__path__`` at runtime, and this will not be automatically
1430-
reflected in ``__spec__.submodule_search_locations``.
1430+
no synchronization between the two objects. For example, it is possible to update
1431+
the module's :attr:`__file__` at runtime and this will not be automatically
1432+
reflected in the module's :attr:`__spec__.origin`, and vice versa.
14311433

14321434
.. versionadded:: 3.4
14331435

14341436
.. attribute:: name
14351437

1436-
(``__name__``)
1438+
(:attr:`__name__`)
14371439

1438-
A string for the fully-qualified name of the module.
1440+
The module's fully-qualified name.
1441+
The :term:`finder` should always set this attribute to a non-empty string.
14391442

14401443
.. attribute:: loader
14411444

1442-
(``__loader__``)
1445+
(:attr:`__loader__`)
14431446

1444-
The :term:`Loader <loader>` that should be used when loading
1445-
the module. :term:`Finders <finder>` should always set this.
1447+
The :term:`loader` used to load the module.
1448+
The :term:`finder` should always set this attribute.
14461449

14471450
.. attribute:: origin
14481451

1449-
(``__file__``)
1452+
(:attr:`__file__`)
14501453

1451-
Name of the place from which the module is loaded, e.g. "builtin" for
1452-
built-in modules and the filename for modules loaded from source.
1453-
Normally "origin" should be set, but it may be ``None`` (the default)
1454-
which indicates it is unspecified (e.g. for namespace packages).
1454+
The location the :term:`loader` should use to load the module.
1455+
For example, for modules loaded from a .py file this is the filename.
1456+
The :term:`finder` should always set this attribute to a meaningful value
1457+
for the :term:`loader` to use. In the uncommon case that there is not one
1458+
(like for namespace packages), it should be set to ``None``.
14551459

14561460
.. attribute:: submodule_search_locations
14571461

1458-
(``__path__``)
1462+
(:attr:`__path__`)
14591463

1460-
List of strings for where to find submodules, if a package (``None``
1461-
otherwise).
1464+
The list of locations where the package's submodules will be found.
1465+
Most of the time this is a single directory.
1466+
The :term:`finder` should set this attribute to a list, even an empty one, to indicate
1467+
to the import system that the module is a package. It should be set to ``None`` for
1468+
non-package modules. It is set automatically later to a special object for
1469+
namespace packages.
14621470

14631471
.. attribute:: loader_state
14641472

1465-
Container of extra module-specific data for use during loading (or
1466-
``None``).
1473+
The :term:`finder` may set this attribute to an object containing additional,
1474+
module-specific data to use when loading the module. Otherwise it should be
1475+
set to ``None``.
14671476

14681477
.. attribute:: cached
14691478

1470-
(``__cached__``)
1479+
(:attr:`__cached__`)
14711480

1472-
String for where the compiled module should be stored (or ``None``).
1481+
The filename of a compiled version of the module's code.
1482+
The :term:`finder` should always set this attribute but it may be ``None``
1483+
for modules that do not need compiled code stored.
14731484

14741485
.. attribute:: parent
14751486

1476-
(``__package__``)
1487+
(:attr:`__package__`)
14771488

1478-
(Read-only) The fully-qualified name of the package under which the module
1479-
should be loaded as a submodule (or the empty string for top-level modules).
1480-
For packages, it is the same as :attr:`__name__`.
1489+
(Read-only) The fully-qualified name of the package the module is in (or the
1490+
empty string for a top-level module).
1491+
If the module is a package then this is the same as :attr:`name`.
14811492

14821493
.. attribute:: has_location
14831494

1484-
Boolean indicating whether or not the module's "origin"
1485-
attribute refers to a loadable location.
1495+
``True`` if the spec's :attr:`origin` refers to a loadable location,
1496+
``False`` otherwise. This value impacts how :attr:`origin` is interpreted
1497+
and how the module's :attr:`__file__` is populated.
1498+
14861499

14871500
:mod:`importlib.util` -- Utility code for importers
14881501
---------------------------------------------------
@@ -1574,8 +1587,9 @@ an :term:`importer`.
15741587

15751588
:exc:`ImportError` is raised if **name** is a relative module name but
15761589
**package** is a false value (e.g. ``None`` or the empty string).
1577-
:exc:`ImportError` is also raised a relative name would escape its containing
1578-
package (e.g. requesting ``..bacon`` from within the ``spam`` package).
1590+
:exc:`ImportError` is also raised if a relative name would escape its
1591+
containing package (e.g. requesting ``..bacon`` from within the ``spam``
1592+
package).
15791593

15801594
.. versionadded:: 3.3
15811595

0 commit comments

Comments
 (0)