Skip to content

Commit 14a5db1

Browse files
Clarify attribute docs on types.ModuleType (GH-24974)
(cherry picked from commit 76b5d71) Co-authored-by: Brett Cannon <brett@python.org>
1 parent 93b0da7 commit 14a5db1

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

Doc/library/types.rst

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ Standard names are defined for the following types:
209209

210210
.. class:: ModuleType(name, doc=None)
211211

212-
The type of :term:`modules <module>`. Constructor takes the name of the
212+
The type of :term:`modules <module>`. The constructor takes the name of the
213213
module to be created and optionally its :term:`docstring`.
214214

215215
.. note::
@@ -224,12 +224,23 @@ Standard names are defined for the following types:
224224

225225
The :term:`loader` which loaded the module. Defaults to ``None``.
226226

227+
This attribute is to match :attr:`importlib.machinery.ModuleSpec.loader`
228+
as stored in the attr:`__spec__` object.
229+
230+
.. note::
231+
A future version of Python may stop setting this attribute by default.
232+
To guard against this potential change, preferrably read from the
233+
:attr:`__spec__` attribute instead or use
234+
``getattr(module, "__loader__", None)`` if you explicitly need to use
235+
this attribute.
236+
227237
.. versionchanged:: 3.4
228238
Defaults to ``None``. Previously the attribute was optional.
229239

230240
.. attribute:: __name__
231241

232-
The name of the module.
242+
The name of the module. Expected to match
243+
:attr:`importlib.machinery.ModuleSpec.name`.
233244

234245
.. attribute:: __package__
235246

@@ -238,9 +249,26 @@ Standard names are defined for the following types:
238249
to ``''``, else it should be set to the name of the package (which can be
239250
:attr:`__name__` if the module is a package itself). Defaults to ``None``.
240251

252+
This attribute is to match :attr:`importlib.machinery.ModuleSpec.parent`
253+
as stored in the attr:`__spec__` object.
254+
255+
.. note::
256+
A future version of Python may stop setting this attribute by default.
257+
To guard against this potential change, preferrably read from the
258+
:attr:`__spec__` attribute instead or use
259+
``getattr(module, "__package__", None)`` if you explicitly need to use
260+
this attribute.
261+
241262
.. versionchanged:: 3.4
242263
Defaults to ``None``. Previously the attribute was optional.
243264

265+
.. attribute:: __spec__
266+
267+
A record of the the module's import-system-related state. Expected to be
268+
an instance of :class:`importlib.machinery.ModuleSpec`.
269+
270+
.. versionadded:: 3.4
271+
244272

245273
.. class:: TracebackType(tb_next, tb_frame, tb_lasti, tb_lineno)
246274

0 commit comments

Comments
 (0)