From 680a8207cb0f2b358f80824d76b88da2e9c965a4 Mon Sep 17 00:00:00 2001 From: Raghu Date: Fri, 22 May 2026 17:40:46 -0400 Subject: [PATCH 1/2] Clarify module.__file__ documentation --- Doc/reference/datamodel.rst | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst index aef5bbe151cfeb..0286fe47305725 100644 --- a/Doc/reference/datamodel.rst +++ b/Doc/reference/datamodel.rst @@ -1073,9 +1073,8 @@ this approach. .. attribute:: module.__file__ - :attr:`!__file__` is an optional attribute that - may or may not be set. Both attributes should be a :class:`str` when they - are available. + :attr:`!__file__` is an optional attribute that may or may not be set. + When it is set, it should be a :class:`str`. An optional attribute, :attr:`!__file__` indicates the pathname of the file from which the module was loaded (if loaded from a file), or the pathname of @@ -1084,6 +1083,9 @@ this approach. modules that are statically linked into the interpreter, and the :ref:`import system ` may opt to leave it unset if it has no semantic meaning (for example, a module loaded from a database). + For modules loaded from a file by the import system, it is usually an + absolute path. However, other loaders may set it to a different value, so + code that needs an absolute path should normalize it explicitly. .. deprecated-removed:: 3.13 3.15 Setting ``__cached__`` on a module while failing to set From 22d9aa0b53ac7f41000083d8265652dd18f9cd03 Mon Sep 17 00:00:00 2001 From: Raghu Date: Tue, 26 May 2026 18:51:36 -0400 Subject: [PATCH 2/2] gh-150274: address review feedback on module.__file__ docs --- Doc/reference/datamodel.rst | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst index 0286fe47305725..b9bca9896f8598 100644 --- a/Doc/reference/datamodel.rst +++ b/Doc/reference/datamodel.rst @@ -1073,8 +1073,8 @@ this approach. .. attribute:: module.__file__ - :attr:`!__file__` is an optional attribute that may or may not be set. - When it is set, it should be a :class:`str`. + :attr:`!__file__` is an optional attribute that + may or may not be set. It should be a :class:`str` when it is available. An optional attribute, :attr:`!__file__` indicates the pathname of the file from which the module was loaded (if loaded from a file), or the pathname of @@ -1083,9 +1083,6 @@ this approach. modules that are statically linked into the interpreter, and the :ref:`import system ` may opt to leave it unset if it has no semantic meaning (for example, a module loaded from a database). - For modules loaded from a file by the import system, it is usually an - absolute path. However, other loaders may set it to a different value, so - code that needs an absolute path should normalize it explicitly. .. deprecated-removed:: 3.13 3.15 Setting ``__cached__`` on a module while failing to set