Skip to content

bpo-37800: Clean up importlib documentation for some module attributes #10016

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 48 commits into from
Nov 16, 2021
Merged
Changes from 26 commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
611e6b5
Correct the description of ModuleSpec attributes
geryogam Oct 20, 2018
7113766
Update the description of the `submodule_search_locations` and `cache…
geryogam Oct 21, 2018
11ed136
Correct a typo
geryogam Oct 21, 2018
9a8d8e6
Fix and improve wording
geryogam Nov 26, 2018
859ff69
Document the read-only property for spec.parent
geryogam Nov 26, 2018
1f63f5b
Add information on the loader_state and has_location attributes
geryogam Dec 2, 2018
98fdbf7
Update importlib.rst
geryogam May 2, 2019
ac69aa1
Apply suggestions from code review
geryogam Jun 1, 2019
8bbdb64
Update importlib.rst
geryogam Jun 4, 2019
e126a5e
Update importlib.rst
geryogam Jun 4, 2019
7b7eb9d
Update importlib.rst
geryogam Jun 30, 2019
5290628
Update importlib.rst
geryogam Jun 30, 2019
89efbff
Update importlib.rst
geryogam Jun 30, 2019
8ba8ede
Apply suggestions from code review
geryogam Jul 13, 2019
2b7b6aa
Update import.rst
geryogam Jul 13, 2019
c71487c
Update import.rst
geryogam Jul 13, 2019
d7e66f6
Update import.rst
geryogam Jul 13, 2019
2123bb0
Update import.rst
geryogam Jul 13, 2019
3abaa63
Update importlib.rst
geryogam Jul 13, 2019
084915d
Update importlib.rst
geryogam Jul 13, 2019
88d2674
Update import.rst
geryogam Jul 13, 2019
37a5b86
Merge branch 'master' into patch-1
geryogam Aug 6, 2019
ee0625a
Apply suggestions from code review
geryogam Aug 9, 2019
cd0edda
Update importlib.rst
geryogam Aug 9, 2019
8957275
Remove changes to the language reference (to do in another PR)
geryogam Aug 9, 2019
9147e5c
Update importlib.rst
geryogam Aug 9, 2019
779a2f1
Update importlib.rst
geryogam Aug 10, 2019
ff0d5ae
Apply suggestions from code review
geryogam Aug 10, 2019
e651eb7
Update importlib.rst
geryogam Aug 10, 2019
9e8c67f
Apply suggestions from code review
geryogam Aug 10, 2019
d79b79a
Update Doc/library/importlib.rst
geryogam Sep 11, 2019
50cd998
Update importlib.rst
geryogam Sep 11, 2019
55e038d
Update importlib.rst
geryogam Sep 11, 2019
e8aa313
Update importlib.rst
geryogam Oct 23, 2020
51812a6
Update importlib.rst
geryogam Oct 24, 2020
3899dfa
Update importlib.rst
geryogam Oct 24, 2020
0a2790d
Merge branch 'master' into patch-1
geryogam Oct 24, 2020
ce8d9ac
Update Doc/library/importlib.rst
geryogam Jul 23, 2021
0cc0b11
Update Doc/library/importlib.rst
geryogam Jul 23, 2021
69d187d
Update importlib.rst
geryogam Jul 23, 2021
3461332
Update importlib.rst
geryogam Jul 29, 2021
19b0542
Apply suggestions from code review
geryogam Jul 29, 2021
5094cc5
Update importlib.rst
geryogam Jul 29, 2021
7bd4866
Update importlib.rst
geryogam Jul 29, 2021
fc40bb0
Update importlib.rst
geryogam Jul 29, 2021
60dc201
Update importlib.rst
geryogam Jul 29, 2021
fcea8dd
Update Doc/library/importlib.rst
brettcannon Nov 16, 2021
2bb2c14
Update Doc/library/importlib.rst
brettcannon Nov 16, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 57 additions & 45 deletions Doc/library/importlib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -420,30 +420,29 @@ ABC hierarchy::
reloaded):

- :attr:`__name__`
The name of the module.
The module's fully-qualified name.

- :attr:`__file__`
The path to where the module data is stored (not set for built-in
modules).
The location from which the module was loaded (usually the path).
It is not set on all modules (e.g. built-in modules).

- :attr:`__cached__`
The path to where a compiled version of the module is/should be
stored (not set when the attribute would be inappropriate).
The path to a compiled version of the code.
It is not set when the attribute would be inappropriate.

- :attr:`__path__`
A list of strings specifying the search path within a
package. This attribute is not set on modules.
For packages, the list of strings specifying where to look for submodules.
This is used in the same way as :attr:`sys.path`, but just for the package.
Effectively, it is the indicator that the module is a package. For
non-package modules, it is not set.

- :attr:`__package__`
The parent package for the module/package. If the module is
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's keep in simple:

            The fully-qualified name of the package the module is in
            (or the empty string for top-level modules, like ``sys``).
            For all packages it is the same as ``__name__``.

Note that I dropped the part about module_for_loader().

Copy link
Contributor Author

@geryogam geryogam Jul 13, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could not have said it better, your version is very concise but precise. Alright.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same, does not look resoled.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

top-level then it has a value of the empty string. The
:func:`importlib.util.module_for_loader` decorator can handle the
details for :attr:`__package__`.
The fully-qualified name of the package under which the module was
loaded as a submodule (or the empty string for top-level modules).
For packages, it is the same as :attr:`__name__`.

- :attr:`__loader__`
The loader used to load the module. The
:func:`importlib.util.module_for_loader` decorator can handle the
details for :attr:`__package__`.
The loader that was used when loading the module.

When :meth:`exec_module` is available then backwards-compatible
functionality is provided.
Expand Down Expand Up @@ -1282,68 +1281,80 @@ find and load modules.
.. class:: ModuleSpec(name, loader, *, origin=None, loader_state=None, is_package=None)

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

.. versionadded:: 3.4

.. attribute:: name

(``__name__``)
(:attr:`__name__`)

A string for the fully-qualified name of the module.
The module's fully-qualified name. Finders should always set this to a
non-empty (identifier) string. The import system may set this to ``None``
in a few special cases.

.. attribute:: loader

(``__loader__``)
(:attr:`__loader__`)

The loader to use for loading. For namespace packages this should be
set to ``None``.
The :class:`Loader <importlib.abc.Loader>` that should be used when
loading the module. Finders should always set this.

.. attribute:: origin

(``__file__``)
(:attr:`__file__`)

Name of the place from which the module is loaded, e.g. "builtin" for
built-in modules and the filename for modules loaded from source.
Normally "origin" should be set, but it may be ``None`` (the default)
which indicates it is unspecified (e.g. for namespace packages).
The name of where the module should be loaded from (e.g. ``'builtin'``
for built-in modules and the path for modules loaded from source).
Finders should normally set this attribute, but it may be ``None`` (the
default) which indicates it is unspecified (like for namespace packages).

.. attribute:: submodule_search_locations

(``__path__``)
(:attr:`__path__`)

List of strings for where to find submodules, if a package (``None``
otherwise).
For packages, the Finder must set this to the iterable specifying the
locations where submodules may be found for the package, even if
it's just an empty list.
For non-package modules, this should be set to ``None``.
For namespace packages (where :attr:`origin` is ``None``), this
is set automatically.

.. attribute:: loader_state

Container of extra module-specific data for use during loading (or
``None``).
The Finder may set this to an object containing additional,
module-specific data to use when loading the module. Otherwise it
should be set to ``None``.

.. attribute:: cached

(``__cached__``)
(:attr:`__cached__`)

String for where the compiled module should be stored (or ``None``).
Where a compiled version of the code should be stored (or ``None``).
This is typically a filename as provided by
::func::`importlib.util.cache_from_source`.

.. attribute:: parent

(``__package__``)
(:attr:`__package__`)

(Read-only) Fully-qualified name of the package to which the module
belongs as a submodule (or ``None``).
(Read-only) The fully-qualified name of the package under which the
module should be loaded as a submodule (or the empty string for top-level
modules). For packages, it is the same as :attr:`__name__`.

.. attribute:: has_location

Boolean indicating whether or not the module's "origin"
attribute refers to a loadable location.
True if the spec's :attr:`origin` refers to a loadable location. False
otherwise. This value impacts how :attr:`origin` is interpreted and how
the module's :attr:`__file__`` is populated.

:mod:`importlib.util` -- Utility code for importers
---------------------------------------------------
Expand Down Expand Up @@ -1435,8 +1446,9 @@ an :term:`importer`.

:exc:`ImportError` is raised if **name** is a relative module name but
**package** is a false value (e.g. ``None`` or the empty string).
:exc:`ImportError` is also raised a relative name would escape its containing
package (e.g. requesting ``..bacon`` from within the ``spam`` package).
:exc:`ImportError` is also raised if a relative name would escape its
containing package (e.g. requesting ``..bacon`` from within the ``spam``
package).

.. versionadded:: 3.3

Expand Down