gh-76007: pydoc: Catch DeprecationWarning for stdlib module __version__ attributes (#139997)
Some checks failed
Tests / Change detection (push) Waiting to run
Tests / Docs (push) Blocked by required conditions
Tests / Check if Autoconf files are up to date (push) Blocked by required conditions
Tests / Check if generated files are up to date (push) Blocked by required conditions
Tests / (push) Blocked by required conditions
Tests / Windows MSI (push) Blocked by required conditions
Tests / Ubuntu SSL tests with OpenSSL (push) Blocked by required conditions
Tests / Ubuntu SSL tests with AWS-LC (push) Blocked by required conditions
Tests / Android (aarch64) (push) Blocked by required conditions
Tests / Android (x86_64) (push) Blocked by required conditions
Tests / iOS (push) Blocked by required conditions
Tests / WASI (push) Blocked by required conditions
Tests / Sanitizers (push) Blocked by required conditions
Lint / lint (push) Waiting to run
Tests / Hypothesis tests on Ubuntu (push) Blocked by required conditions
Tests / Address sanitizer (push) Blocked by required conditions
Tests / Cross build Linux (push) Blocked by required conditions
Tests / CIFuzz (push) Blocked by required conditions
Tests / All required checks pass (push) Blocked by required conditions
mypy / Run mypy on Lib/_pyrepl (push) Waiting to run
mypy / Run mypy on Lib/test/libregrtest (push) Waiting to run
mypy / Run mypy on Lib/tomllib (push) Waiting to run
mypy / Run mypy on Tools/build (push) Waiting to run
mypy / Run mypy on Tools/cases_generator (push) Waiting to run
mypy / Run mypy on Tools/clinic (push) Waiting to run
mypy / Run mypy on Tools/jit (push) Waiting to run
mypy / Run mypy on Tools/peg_generator (push) Waiting to run
JIT / Interpreter (Debug) (push) Has been cancelled
Tail calling interpreter / aarch64-apple-darwin/clang (push) Has been cancelled
Tail calling interpreter / aarch64-unknown-linux-gnu/gcc (push) Has been cancelled
Tail calling interpreter / x86_64-pc-windows-msvc/msvc (push) Has been cancelled
Tail calling interpreter / x86_64-apple-darwin/clang (push) Has been cancelled
Tail calling interpreter / free-threading (push) Has been cancelled
Tail calling interpreter / x86_64-unknown-linux-gnu/gcc (push) Has been cancelled
JIT / aarch64-pc-windows-msvc/msvc (Release) (push) Has been cancelled
JIT / aarch64-pc-windows-msvc/msvc (Debug) (push) Has been cancelled
JIT / i686-pc-windows-msvc/msvc (Release) (push) Has been cancelled
JIT / i686-pc-windows-msvc/msvc (Debug) (push) Has been cancelled
JIT / aarch64-apple-darwin/clang (Release) (push) Has been cancelled
JIT / aarch64-unknown-linux-gnu/gcc (Release) (push) Has been cancelled
JIT / aarch64-apple-darwin/clang (Debug) (push) Has been cancelled
JIT / aarch64-unknown-linux-gnu/gcc (Debug) (push) Has been cancelled
JIT / x86_64-pc-windows-msvc/msvc (Release) (push) Has been cancelled
JIT / x86_64-pc-windows-msvc/msvc (Debug) (push) Has been cancelled
JIT / x86_64-apple-darwin/clang (Release) (push) Has been cancelled
JIT / x86_64-unknown-linux-gnu/gcc (Release) (push) Has been cancelled
JIT / x86_64-apple-darwin/clang (Debug) (push) Has been cancelled
JIT / x86_64-unknown-linux-gnu/gcc (Debug) (push) Has been cancelled
JIT / Free-Threaded (Debug) (push) Has been cancelled
JIT / JIT without optimizations (Debug) (push) Has been cancelled
JIT / JIT with tail calling interpreter (push) Has been cancelled

This commit is contained in:
Stan Ulbrych 2025-12-14 12:59:05 +00:00 committed by GitHub
parent 04da416e6b
commit 78a50ee10e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 65 additions and 18 deletions

View file

@ -450,6 +450,7 @@ class Doc:
PYTHONDOCS = os.environ.get("PYTHONDOCS",
"https://docs.python.org/%d.%d/library"
% sys.version_info[:2])
STDLIB_DIR = sysconfig.get_path('stdlib')
def document(self, object, name=None, *args):
"""Generate documentation for an object."""
@ -475,23 +476,12 @@ class Doc:
docmodule = docclass = docroutine = docother = docproperty = docdata = fail
def getdocloc(self, object, basedir=sysconfig.get_path('stdlib')):
def getdocloc(self, object, basedir=None):
"""Return the location of module docs or None"""
try:
file = inspect.getabsfile(object)
except TypeError:
file = '(built-in)'
basedir = self.STDLIB_DIR if basedir is None else basedir
docloc = os.environ.get("PYTHONDOCS", self.PYTHONDOCS)
basedir = os.path.normcase(basedir)
if (isinstance(object, type(os)) and
(object.__name__ in ('errno', 'exceptions', 'gc',
'marshal', 'posix', 'signal', 'sys',
'_thread', 'zipimport') or
(file.startswith(basedir) and
not file.startswith(os.path.join(basedir, 'site-packages')))) and
if (self._is_stdlib_module(object, basedir) and
object.__name__ not in ('xml.etree', 'test.test_pydoc.pydoc_mod')):
if docloc.startswith(("http://", "https://")):
docloc = "{}/{}.html".format(docloc.rstrip("/"), object.__name__.lower())
@ -501,6 +491,36 @@ class Doc:
docloc = None
return docloc
def _get_version(self, object):
if self._is_stdlib_module(object):
with warnings.catch_warnings():
warnings.simplefilter("ignore", DeprecationWarning)
version = getattr(object, '__version__', None)
else:
version = getattr(object, '__version__', None)
return '' if version is None else str(version)
def _is_stdlib_module(self, object, basedir=None):
basedir = self.STDLIB_DIR if basedir is None else basedir
try:
file = inspect.getabsfile(object)
except TypeError:
file = '(built-in)'
if sysconfig.is_python_build():
srcdir = sysconfig.get_config_var('srcdir')
if srcdir:
basedir = os.path.join(srcdir, 'Lib')
basedir = os.path.normcase(basedir)
return (isinstance(object, type(os)) and
(object.__name__ in ('errno', 'exceptions', 'gc',
'marshal', 'posix', 'signal', 'sys',
'_thread', 'zipimport')
or (file.startswith(basedir) and
not file.startswith(os.path.join(basedir, 'site-packages')))))
# -------------------------------------------- HTML documentation generator
class HTMLRepr(Repr):
@ -760,8 +780,8 @@ class HTMLDoc(Doc):
except TypeError:
filelink = '(built-in)'
info = []
if hasattr(object, '__version__'):
version = str(object.__version__)
if version := self._get_version(object):
if version[:11] == '$' + 'Revision: ' and version[-1:] == '$':
version = version[11:-1].strip()
info.append('version %s' % self.escape(version))
@ -1296,8 +1316,7 @@ location listed above.
contents.append(self.docother(value, key, name, maxlen=70))
result = result + self.section('DATA', '\n'.join(contents))
if hasattr(object, '__version__'):
version = str(object.__version__)
if version := self._get_version(object):
if version[:11] == '$' + 'Revision: ' and version[-1:] == '$':
version = version[11:-1].strip()
result = result + self.section('VERSION', version)

View file

@ -2305,6 +2305,32 @@ class TestInternalUtilities(unittest.TestCase):
trailing_argv0dir = trailing_curdir + [self.argv0dir]
self.assertIsNone(self._get_revised_path(trailing_argv0dir))
def test__get_version(self):
import json
import warnings
class MyModule:
__name__ = 'my_module'
@property
def __version__(self):
warnings._deprecated("__version__", remove=(3, 20))
return "1.2.3"
module = MyModule()
doc = pydoc.Doc()
with warnings.catch_warnings(record=True) as w: # TODO: remove in 3.20
warnings.simplefilter("always")
version = doc._get_version(json)
self.assertEqual(version, "2.0.9")
self.assertEqual(len(w), 0)
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
version = doc._get_version(module)
self.assertEqual(version, "1.2.3")
self.assertEqual(len(w), 1)
def setUpModule():
thread_info = threading_helper.threading_setup()

View file

@ -0,0 +1,2 @@
:mod:`pydoc`: Fix :exc:`DeprecationWarning` being raised when generating doc for
:term:`stdlib` modules.