Skip to content

Commit cba3776

Browse files
AlexWaygoodhugovkwarsawJelleZijlstra
authored
[3.12] gh-101100: Consolidate documentation on ModuleType attributes (#124709) (#125211)
Co-authored-by: Hugo van Kemenade <[email protected]> Co-authored-by: Barry Warsaw <[email protected]> Co-authored-by: Jelle Zijlstra <[email protected]>
1 parent 046687c commit cba3776

21 files changed

+348
-324
lines changed

Doc/c-api/import.rst

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,14 @@ Importing Modules
120120
such modules have no way to know that the module object is an unknown (and
121121
probably damaged with respect to the module author's intents) state.
122122
123-
The module's :attr:`__spec__` and :attr:`__loader__` will be set, if
124-
not set already, with the appropriate values. The spec's loader will
125-
be set to the module's ``__loader__`` (if set) and to an instance of
126-
:class:`~importlib.machinery.SourceFileLoader` otherwise.
123+
The module's :attr:`~module.__spec__` and :attr:`~module.__loader__` will be
124+
set, if not set already, with the appropriate values. The spec's loader
125+
will be set to the module's :attr:`!__loader__` (if set) and to an instance
126+
of :class:`~importlib.machinery.SourceFileLoader` otherwise.
127127
128-
The module's :attr:`__file__` attribute will be set to the code object's
129-
:attr:`~codeobject.co_filename`. If applicable, :attr:`__cached__` will also
130-
be set.
128+
The module's :attr:`~module.__file__` attribute will be set to the code
129+
object's :attr:`~codeobject.co_filename`. If applicable,
130+
:attr:`~module.__cached__` will also be set.
131131
132132
This function will reload the module if it was already imported. See
133133
:c:func:`PyImport_ReloadModule` for the intended way to reload a module.
@@ -139,29 +139,29 @@ Importing Modules
139139
:c:func:`PyImport_ExecCodeModuleWithPathnames`.
140140
141141
.. versionchanged:: 3.12
142-
The setting of :attr:`__cached__` and :attr:`__loader__` is
143-
deprecated. See :class:`~importlib.machinery.ModuleSpec` for
142+
The setting of :attr:`~module.__cached__` and :attr:`~module.__loader__`
143+
is deprecated. See :class:`~importlib.machinery.ModuleSpec` for
144144
alternatives.
145145
146146
147147
.. c:function:: PyObject* PyImport_ExecCodeModuleEx(const char *name, PyObject *co, const char *pathname)
148148
149-
Like :c:func:`PyImport_ExecCodeModule`, but the :attr:`__file__` attribute of
150-
the module object is set to *pathname* if it is non-``NULL``.
149+
Like :c:func:`PyImport_ExecCodeModule`, but the :attr:`~module.__file__`
150+
attribute of the module object is set to *pathname* if it is non-``NULL``.
151151
152152
See also :c:func:`PyImport_ExecCodeModuleWithPathnames`.
153153
154154
155155
.. c:function:: PyObject* PyImport_ExecCodeModuleObject(PyObject *name, PyObject *co, PyObject *pathname, PyObject *cpathname)
156156
157-
Like :c:func:`PyImport_ExecCodeModuleEx`, but the :attr:`__cached__`
157+
Like :c:func:`PyImport_ExecCodeModuleEx`, but the :attr:`~module.__cached__`
158158
attribute of the module object is set to *cpathname* if it is
159159
non-``NULL``. Of the three functions, this is the preferred one to use.
160160
161161
.. versionadded:: 3.3
162162
163163
.. versionchanged:: 3.12
164-
Setting :attr:`__cached__` is deprecated. See
164+
Setting :attr:`~module.__cached__` is deprecated. See
165165
:class:`~importlib.machinery.ModuleSpec` for alternatives.
166166
167167

Doc/c-api/module.rst

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,19 @@ Module Objects
3737
single: __package__ (module attribute)
3838
single: __loader__ (module attribute)
3939
40-
Return a new module object with the :attr:`__name__` attribute set to *name*.
41-
The module's :attr:`__name__`, :attr:`__doc__`, :attr:`__package__`, and
42-
:attr:`__loader__` attributes are filled in (all but :attr:`__name__` are set
43-
to ``None``); the caller is responsible for providing a :attr:`__file__`
44-
attribute.
40+
Return a new module object with :attr:`module.__name__` set to *name*.
41+
The module's :attr:`!__name__`, :attr:`~module.__doc__`,
42+
:attr:`~module.__package__` and :attr:`~module.__loader__` attributes are
43+
filled in (all but :attr:`!__name__` are set to ``None``). The caller is
44+
responsible for setting a :attr:`~module.__file__` attribute.
4545
4646
Return ``NULL`` with an exception set on error.
4747
4848
.. versionadded:: 3.3
4949
5050
.. versionchanged:: 3.4
51-
:attr:`__package__` and :attr:`__loader__` are set to ``None``.
51+
:attr:`~module.__package__` and :attr:`~module.__loader__` are now set to
52+
``None``.
5253
5354
5455
.. c:function:: PyObject* PyModule_New(const char *name)
@@ -77,8 +78,9 @@ Module Objects
7778
single: __name__ (module attribute)
7879
single: SystemError (built-in exception)
7980
80-
Return *module*'s :attr:`__name__` value. If the module does not provide one,
81-
or if it is not a string, :exc:`SystemError` is raised and ``NULL`` is returned.
81+
Return *module*'s :attr:`~module.__name__` value. If the module does not
82+
provide one, or if it is not a string, :exc:`SystemError` is raised and
83+
``NULL`` is returned.
8284
8385
.. versionadded:: 3.3
8486
@@ -108,8 +110,8 @@ Module Objects
108110
single: SystemError (built-in exception)
109111
110112
Return the name of the file from which *module* was loaded using *module*'s
111-
:attr:`__file__` attribute. If this is not defined, or if it is not a
112-
unicode string, raise :exc:`SystemError` and return ``NULL``; otherwise return
113+
:attr:`~module.__file__` attribute. If this is not defined, or if it is not a
114+
string, raise :exc:`SystemError` and return ``NULL``; otherwise return
113115
a reference to a Unicode object.
114116
115117
.. versionadded:: 3.2

Doc/deprecations/pending-removal-in-3.14.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
Pending Removal in Python 3.14
22
------------------------------
33

4+
* The import system:
5+
6+
* Setting :attr:`~module.__loader__` on a module while
7+
failing to set :attr:`__spec__.loader <importlib.machinery.ModuleSpec.loader>`
8+
is deprecated. In Python 3.14, :attr:`!__loader__` will cease to be set or
9+
taken into consideration by the import system or the standard library.
10+
411
* :mod:`argparse`: The *type*, *choices*, and *metavar* parameters
512
of :class:`!argparse.BooleanOptionalAction` are deprecated
613
and will be removed in 3.14.

Doc/glossary.rst

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ Glossary
437437
<meta path finder>` for use with :data:`sys.meta_path`, and :term:`path
438438
entry finders <path entry finder>` for use with :data:`sys.path_hooks`.
439439

440-
See :ref:`importsystem` and :mod:`importlib` for much more detail.
440+
See :ref:`finders-and-loaders` and :mod:`importlib` for much more detail.
441441

442442
floor division
443443
Mathematical division that rounds down to nearest integer. The floor
@@ -750,8 +750,11 @@ Glossary
750750
loader
751751
An object that loads a module. It must define a method named
752752
:meth:`load_module`. A loader is typically returned by a
753-
:term:`finder`. See :pep:`302` for details and
754-
:class:`importlib.abc.Loader` for an :term:`abstract base class`.
753+
:term:`finder`. See also:
754+
755+
* :ref:`finders-and-loaders`
756+
* :class:`importlib.abc.Loader`
757+
* :pep:`302`
755758

756759
locale encoding
757760
On Unix, it is the encoding of the LC_CTYPE locale. It can be set with
@@ -821,6 +824,8 @@ Glossary
821824
A namespace containing the import-related information used to load a
822825
module. An instance of :class:`importlib.machinery.ModuleSpec`.
823826

827+
See also :ref:`module-specs`.
828+
824829
MRO
825830
See :term:`method resolution order`.
826831

Doc/library/ast.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -889,7 +889,7 @@ Statements
889889
(indicating a "simple" target). A "simple" target consists solely of a
890890
:class:`Name` node that does not appear between parentheses; all other
891891
targets are considered complex. Only simple targets appear in
892-
the :attr:`__annotations__` dictionary of modules and classes.
892+
the :attr:`~object.__annotations__` dictionary of modules and classes.
893893

894894
.. doctest::
895895

Doc/library/importlib.rst

Lines changed: 34 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ ABC hierarchy::
249249
An abstract method for finding a :term:`spec <module spec>` for
250250
the specified module. If this is a top-level import, *path* will
251251
be ``None``. Otherwise, this is a search for a subpackage or
252-
module and *path* will be the value of :attr:`__path__` from the
252+
module and *path* will be the value of :attr:`~module.__path__` from the
253253
parent package. If a spec cannot be found, ``None`` is returned.
254254
When passed in, ``target`` is a module object that the finder may
255255
use to make a more educated guess about what spec to return.
@@ -355,34 +355,12 @@ ABC hierarchy::
355355
(note that some of these attributes can change when a module is
356356
reloaded):
357357

358-
- :attr:`__name__`
359-
The module's fully qualified name.
360-
It is ``'__main__'`` for an executed module.
361-
362-
- :attr:`__file__`
363-
The location the :term:`loader` used to load the module.
364-
For example, for modules loaded from a .py file this is the filename.
365-
It is not set on all modules (e.g. built-in modules).
366-
367-
- :attr:`__cached__`
368-
The filename of a compiled version of the module's code.
369-
It is not set on all modules (e.g. built-in modules).
370-
371-
- :attr:`__path__`
372-
The list of locations where the package's submodules will be found.
373-
Most of the time this is a single directory.
374-
The import system passes this attribute to ``__import__()`` and to finders
375-
in the same way as :data:`sys.path` but just for the package.
376-
It is not set on non-package modules so it can be used
377-
as an indicator that the module is a package.
378-
379-
- :attr:`__package__`
380-
The fully qualified name of the package the module is in (or the
381-
empty string for a top-level module).
382-
If the module is a package then this is the same as :attr:`__name__`.
383-
384-
- :attr:`__loader__`
385-
The :term:`loader` used to load the module.
358+
- :attr:`module.__name__`
359+
- :attr:`module.__file__`
360+
- :attr:`module.__cached__`
361+
- :attr:`module.__path__`
362+
- :attr:`module.__package__`
363+
- :attr:`module.__loader__` *(deprecated)*
386364

387365
When :meth:`exec_module` is available then backwards-compatible
388366
functionality is provided.
@@ -418,7 +396,8 @@ ABC hierarchy::
418396
can implement this abstract method to give direct access
419397
to the data stored. :exc:`OSError` is to be raised if the *path* cannot
420398
be found. The *path* is expected to be constructed using a module's
421-
:attr:`__file__` attribute or an item from a package's :attr:`__path__`.
399+
:attr:`~module.__file__` attribute or an item from a package's
400+
:attr:`~module.__path__`.
422401

423402
.. versionchanged:: 3.4
424403
Raises :exc:`OSError` instead of :exc:`NotImplementedError`.
@@ -505,9 +484,9 @@ ABC hierarchy::
505484

506485
.. abstractmethod:: get_filename(fullname)
507486

508-
An abstract method that is to return the value of :attr:`__file__` for
509-
the specified module. If no path is available, :exc:`ImportError` is
510-
raised.
487+
An abstract method that is to return the value of
488+
:attr:`~module.__file__` for the specified module. If no path is
489+
available, :exc:`ImportError` is raised.
511490

512491
If source code is available, then the method should return the path to
513492
the source file, regardless of whether a bytecode was used to load the
@@ -1166,43 +1145,45 @@ find and load modules.
11661145
.. class:: ModuleSpec(name, loader, *, origin=None, loader_state=None, is_package=None)
11671146

11681147
A specification for a module's import-system-related state. This is
1169-
typically exposed as the module's :attr:`__spec__` attribute. Many
1148+
typically exposed as the module's :attr:`~module.__spec__` attribute. Many
11701149
of these attributes are also available directly on a module: for example,
11711150
``module.__spec__.origin == module.__file__``. Note, however, that
11721151
while the *values* are usually equivalent, they can differ since there is
1173-
no synchronization between the two objects. For example, it is possible to update
1174-
the module's :attr:`__file__` at runtime and this will not be automatically
1175-
reflected in the module's :attr:`__spec__.origin`, and vice versa.
1152+
no synchronization between the two objects. For example, it is possible to
1153+
update the module's :attr:`~module.__file__` at runtime and this will not be
1154+
automatically reflected in the module's
1155+
:attr:`__spec__.origin <ModuleSpec.origin>`, and vice versa.
11761156

11771157
.. versionadded:: 3.4
11781158

11791159
.. attribute:: name
11801160

1181-
The module's fully qualified name
1182-
(see :attr:`__name__` attributes on modules).
1161+
The module's fully qualified name (see :attr:`module.__name__`).
11831162
The :term:`finder` should always set this attribute to a non-empty string.
11841163

11851164
.. attribute:: loader
11861165

1187-
The :term:`loader` used to load the module
1188-
(see :attr:`__loader__` attributes on modules).
1166+
The :term:`loader` used to load the module (see :attr:`module.__loader__`).
11891167
The :term:`finder` should always set this attribute.
11901168

11911169
.. attribute:: origin
11921170

11931171
The location the :term:`loader` should use to load the module
1194-
(see :attr:`__file__` attributes on modules).
1195-
For example, for modules loaded from a .py file this is the filename.
1172+
(see :attr:`module.__file__`).
1173+
For example, for modules loaded from a ``.py`` file this is the filename.
11961174
The :term:`finder` should always set this attribute to a meaningful value
11971175
for the :term:`loader` to use. In the uncommon case that there is not one
11981176
(like for namespace packages), it should be set to ``None``.
11991177

12001178
.. attribute:: submodule_search_locations
12011179

1202-
The list of locations where the package's submodules will be found
1203-
(see :attr:`__path__` attributes on modules).
1204-
Most of the time this is a single directory.
1205-
The :term:`finder` should set this attribute to a list, even an empty one, to indicate
1180+
A (possibly empty) :term:`sequence` of strings enumerating the locations
1181+
in which a package's submodules will be found
1182+
(see :attr:`module.__path__`). Most of the time there will only be a
1183+
single directory in this list.
1184+
1185+
The :term:`finder` should set this attribute to a sequence, even an empty
1186+
one, to indicate
12061187
to the import system that the module is a package. It should be set to ``None`` for
12071188
non-package modules. It is set automatically later to a special object for
12081189
namespace packages.
@@ -1216,22 +1197,22 @@ find and load modules.
12161197
.. attribute:: cached
12171198

12181199
The filename of a compiled version of the module's code
1219-
(see :attr:`__cached__` attributes on modules).
1200+
(see :attr:`module.__cached__`).
12201201
The :term:`finder` should always set this attribute but it may be ``None``
12211202
for modules that do not need compiled code stored.
12221203

12231204
.. attribute:: parent
12241205

12251206
(Read-only) The fully qualified name of the package the module is in (or the
12261207
empty string for a top-level module).
1227-
See :attr:`__package__` attributes on modules.
1208+
See :attr:`module.__package__`.
12281209
If the module is a package then this is the same as :attr:`name`.
12291210

12301211
.. attribute:: has_location
12311212

12321213
``True`` if the spec's :attr:`origin` refers to a loadable location,
1233-
``False`` otherwise. This value impacts how :attr:`origin` is interpreted
1234-
and how the module's :attr:`__file__` is populated.
1214+
``False`` otherwise. This value impacts how :attr:`!origin` is interpreted
1215+
and how the module's :attr:`~module.__file__` is populated.
12351216

12361217

12371218
:mod:`importlib.util` -- Utility code for importers
@@ -1353,8 +1334,8 @@ an :term:`importer`.
13531334

13541335
.. versionchanged:: 3.7
13551336
Raises :exc:`ModuleNotFoundError` instead of :exc:`AttributeError` if
1356-
**package** is in fact not a package (i.e. lacks a :attr:`__path__`
1357-
attribute).
1337+
**package** is in fact not a package (i.e. lacks a
1338+
:attr:`~module.__path__` attribute).
13581339

13591340
.. function:: module_from_spec(spec)
13601341

Doc/library/pkgutil.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ support.
2626
__path__ = extend_path(__path__, __name__)
2727

2828
For each directory on :data:`sys.path` that has a subdirectory that matches the
29-
package name, add the subdirectory to the package's :attr:`__path__`. This is useful
29+
package name, add the subdirectory to the package's
30+
:attr:`~module.__path__`. This is useful
3031
if one wants to distribute different parts of a single logical package as multiple
3132
directories.
3233

Doc/library/sys.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1239,7 +1239,8 @@ always available.
12391239
that implement Python's default import semantics. The
12401240
:meth:`~importlib.abc.MetaPathFinder.find_spec` method is called with at
12411241
least the absolute name of the module being imported. If the module to be
1242-
imported is contained in a package, then the parent package's :attr:`__path__`
1242+
imported is contained in a package, then the parent package's
1243+
:attr:`~module.__path__`
12431244
attribute is passed in as a second argument. The method returns a
12441245
:term:`module spec`, or ``None`` if the module cannot be found.
12451246

0 commit comments

Comments
 (0)