Skip to contentSkip to conversation

python: add QAPI and qapidoc et al to python tests

PATCH In review John Snow 4 participants 13 messages Jun 4, 2025 – Jun 5, 2025

lore
Share & tools
lore

John Snow

author
#0 lore raw

v5 cover python: add QAPI and qapidoc et al to python tests

Hiya, this series turns on automated linting for scripts/qapi, docs/sphinx/qapidoc.py and docs/sphinx/qapi_domain.py.

This includes flake8/isort/pylint/mypy for scripts/qapi, but omits mypy from the Sphinx plugins owing to my inability to strictly type the extensions given the wide versions of Sphinx we actually support.

[Maybe this will change soon, but it's not true just yet.]

Though I have been using black in my own development, I have not yet enabled it anywhere automatically. Maybe soon.

V5:
- Fixed test failure on GitLab CI. As far as I can tell, the problem stems from Sphinx requiring a version of packaging that is old enough that some newer versions of setuptools break when it is installed. Fix this by pinning setuptools to an older version, for now. Note that this only applies to the "minreqs" test and not general installation.

(This change applies only to patch 4/6.)

(Note that I am still carrying a patch to bump the Sphinx version, but I wanted to get this series out of the way first... I'll revisit this pinning again shortly.)

V4:
- Rebase and resend. Pull Request imminent.

V3:
- Folded in Markus's commit to clean up setup.cfg.

V2:
- Mostly commit message updates based on Markus's's's review.

John Snow (5):
qapi: Add some pylint ignores
docs/qapidoc: linting fixes
python: update missing dependencies from minreqs
python: add qapi static analysis tests
qapi: delete un-needed python static analysis configs

Markus Armbruster (1):
python: Drop redundant warn_unused_configs = True

 docs/sphinx/qapi_domain.py  | 25 ++++++++++++++-----------
 docs/sphinx/qapidoc.py      |  5 +++--
 python/setup.cfg            |  2 +-
 python/tests/minreqs.txt    | 31 +++++++++++++++++++++++++++++++
 python/tests/qapi-flake8.sh |  6 ++++++
 python/tests/qapi-isort.sh  |  8 ++++++++
 python/tests/qapi-mypy.sh   |  4 ++++
 python/tests/qapi-pylint.sh |  8 ++++++++
 scripts/qapi/.flake8        |  3 ---
 scripts/qapi/.isort.cfg     |  7 -------
 scripts/qapi/backend.py     |  2 ++
 scripts/qapi/mypy.ini       |  4 ----
 scripts/qapi/pylintrc       |  1 +
 13 files changed, 78 insertions(+), 28 deletions(-)
 create mode 100755 python/tests/qapi-flake8.sh
 create mode 100755 python/tests/qapi-isort.sh
 create mode 100755 python/tests/qapi-mypy.sh
 create mode 100755 python/tests/qapi-pylint.sh
 delete mode 100644 scripts/qapi/.flake8
 delete mode 100644 scripts/qapi/.isort.cfg
 delete mode 100644 scripts/qapi/mypy.ini
Signature · 1 line
2.48.1
#1 lore raw

PATCH v5 1/6 qapi: Add some pylint ignores

This restores the linting baseline in QAPI.

---
 scripts/qapi/backend.py | 2 ++
 1 file changed, 2 insertions(+)
scripts/qapi/backend.py +2 -0
index 14e60aa67af..49ae6ecdd33 100644
@@ -13,6 +13,7 @@
 
 
 class QAPIBackend(ABC):
+    # pylint: disable=too-few-public-methods
 
     @abstractmethod
     def generate(self,
@@ -36,6 +37,7 @@ def generate(self,
 
 
 class QAPICBackend(QAPIBackend):
+    # pylint: disable=too-few-public-methods
 
     def generate(self,
                  schema: QAPISchema,
Signature · 1 line
2.48.1
author
#2 lore raw

PATCH v5 2/6 docs/qapidoc: linting fixes

In reply to John Snow

This restores the linting baseline in qapidoc. The order of some imports change slightly here due to configuring isort a little better:
previously, isort was having difficulty understanding that "compat" and "qapidoc_legacy" were local modules because docs/sphinx "isn't a python package". Configuring this manually, isort chooses a different import ordering, which _is_ intentional here.

Also: extra ignores are added for pylint. The most recent versions of pylint don't require these ignores, but the oldest versions we support do, so in the extra ignores go.

---
 docs/sphinx/qapi_domain.py | 25 ++++++++++++++-----------
 docs/sphinx/qapidoc.py     |  5 +++--
 2 files changed, 17 insertions(+), 13 deletions(-)
docs/sphinx/qapi_domain.py +14 -11
index c94af5719ca..ebc46a72c61 100644
@@ -20,16 +20,6 @@
 
 from docutils import nodes
 from docutils.parsers.rst import directives
-
-from compat import (
-    CompatField,
-    CompatGroupedField,
-    CompatTypedField,
-    KeywordNode,
-    ParserFix,
-    Signature,
-    SpaceNode,
-)
 from sphinx import addnodes
 from sphinx.directives import ObjectDescription
 from sphinx.domains import (
@@ -44,6 +34,16 @@
 from sphinx.util.docutils import SphinxDirective
 from sphinx.util.nodes import make_id, make_refnode
 
+from compat import (
+    CompatField,
+    CompatGroupedField,
+    CompatTypedField,
+    KeywordNode,
+    ParserFix,
+    Signature,
+    SpaceNode,
+)
+
 
 if TYPE_CHECKING:
     from typing import (
@@ -56,7 +56,6 @@
     )
 
     from docutils.nodes import Element, Node
-
     from sphinx.addnodes import desc_signature, pending_xref
     from sphinx.application import Sphinx
     from sphinx.builders import Builder
@@ -168,6 +167,8 @@ class QAPIDescription(ParserFix):
     """
 
     def handle_signature(self, sig: str, signode: desc_signature) -> Signature:
+        # pylint: disable=unused-argument
+
         # Do nothing. The return value here is the "name" of the entity
         # being documented; for QAPI, this is the same as the
         # "signature", which is just a name.
@@ -210,6 +211,8 @@ def _get_fqn(self, name: Signature) -> str:
     def add_target_and_index(
         self, name: Signature, sig: str, signode: desc_signature
     ) -> None:
+        # pylint: disable=unused-argument
+
         # name is the return value of handle_signature.
         # sig is the original, raw text argument to handle_signature.
         # For QAPI, these are identical, currently.
docs/sphinx/qapidoc.py +3 -2
index 661b2c4ed0e..8011ac9efaf 100644
@@ -27,6 +27,7 @@
 
 from __future__ import annotations
 
+
 __version__ = "2.0"
 
 from contextlib import contextmanager
@@ -56,8 +57,6 @@
     QAPISchemaVisitor,
 )
 from qapi.source import QAPISourceInfo
-
-from qapidoc_legacy import QAPISchemaGenRSTVisitor  # type: ignore
 from sphinx import addnodes
 from sphinx.directives.code import CodeBlock
 from sphinx.errors import ExtensionError
@@ -65,6 +64,8 @@
 from sphinx.util.docutils import SphinxDirective, switch_source_input
 from sphinx.util.nodes import nested_parse_with_titles
 
+from qapidoc_legacy import QAPISchemaGenRSTVisitor  # type: ignore
+
 
 if TYPE_CHECKING:
     from typing import (
Signature · 1 line
2.48.1
author
#3 lore raw

PATCH v5 3/6 python: update missing dependencies from minreqs

In reply to John Snow

We pin all dependencies for the "check-minreqs" test because pip lacks a dependency resolver that installs "the oldest possible package that meets dependency criteria". So, in order to test our stated minimum requirements, we pin all of our dependencies (and their dependencies, transitively) at the oldest possible versions that still work and pass tests; proving that our minimum requirements are correct.

(It also ensures no new features accidentally sneak in from developers on newer platforms.)

A few transitive dependencies were omitted from the pinned dependency file by accident; as a result, pip's dependency solver can pull in newer dependencies, which we don't want. This patch corrects the previous oversight and pins the missing dependencies.

---
 python/tests/minreqs.txt | 4 ++++
 1 file changed, 4 insertions(+)
python/tests/minreqs.txt +4 -0
index 6445407ba87..d3d53e0da81 100644
@@ -38,10 +38,14 @@ pyflakes==2.5.0
 
 # Transitive mypy dependencies
 mypy-extensions==1.0.0
+tomli==1.1.0
 typing-extensions==4.7.1
 
 # Transitive pylint dependencies
 astroid==2.15.4
+dill==0.2
 lazy-object-proxy==1.4.0
+platformdirs==2.2.0
 toml==0.10.0
+tomlkit==0.10.1
 wrapt==1.14.0
Signature · 1 line
2.48.1
author
#4 lore raw

PATCH v5 4/6 python: add qapi static analysis tests

In reply to John Snow

Update the python tests to also check QAPI and the QAPI Sphinx extensions. The docs/sphinx/qapidoc_legacy.py file is not included in these checks, as it is destined for removal soon. mypy is also not called on the QAPI Sphinx extensions, owing to difficulties supporting Sphinx 3.x - 8.x while maintaining static type checking support. mypy *is* called on all of the QAPI tools themselves, though.

flake8, isort and mypy use the tool configuration from the existing python directory (in setup.cfg). pylint continues to use the special configuration located in scripts/qapi/ - that configuration is more permissive. If we wish to unify the two configurations, that's a separate series and a discussion for a later date.

The list of pylint ignores is also updated, owing again to the wide window of pylint version support: newer versions require pragmas to occasionally silence the "too many positional arguments" warning, but older versions do not have such a warning category and will instead yelp about an unrecognized option. Silence that warning, too.

As a result of this patch, one would be able to run any of the following tests locally from the qemu.git/python directory and have it cover the QAPI tooling as well. All of the following options run the python tests, static analysis tests, and linter checks; but with different combinations of dependencies and interpreters.

- "make check-minreqs" Run tests specifically under our oldest supported Python and our oldest supported dependencies. This is the test that runs on GitLab as "check-python-minreqs". This helps ensure we do not regress support on older platforms accidentally.

- "make check-tox" Runs the tests under the newest supported dependencies, but under each supported version of Python in turn. At time of writing, this is Python 3.8 to 3.13 inclusive. This test helps catch bleeding-edge problems before they become problems for developer workstations. This is the GitLab test "check-python-tox" and is an optionally run, may-fail test due to the unpredictable nature of new dependencies being released into the ecosystem that may cause regressions.

- "make check-dev" Runs the tests under the newest supported dependencies using whatever version of Python the user happens to have installed. This is a quick convenience check that does not map to any particular GitLab test.

(Note! check-dev may be busted on Fedora 41 and bleeding edge versions of setuptools. That's unrelated to this patch and I'll address it separately and soon. Thank you for your patience, --mgmt)

Finally, finally, finally: this means that QAPI tooling will be linted and type-checked from the GitLab pipelines.

add licenses or whatever

---
 python/setup.cfg            |  1 +
 python/tests/minreqs.txt    | 27 +++++++++++++++++++++++++++
 python/tests/qapi-flake8.sh |  6 ++++++
 python/tests/qapi-isort.sh  |  8 ++++++++
 python/tests/qapi-mypy.sh   |  4 ++++
 python/tests/qapi-pylint.sh |  8 ++++++++
 scripts/qapi/pylintrc       |  1 +
 7 files changed, 55 insertions(+)
 create mode 100755 python/tests/qapi-flake8.sh
 create mode 100755 python/tests/qapi-isort.sh
 create mode 100755 python/tests/qapi-mypy.sh
 create mode 100755 python/tests/qapi-pylint.sh
python/setup.cfg +1 -0
index c48dff280a3..d21304cadd1 100644
@@ -46,6 +46,7 @@ devel =
     urwid >= 2.1.2
     urwid-readline >= 0.13
     Pygments >= 2.9.0
+    sphinx >= 3.4.3
 
 # Provides qom-fuse functionality
 fuse =
python/tests/minreqs.txt +27 -0
index d3d53e0da81..cd2e2a81c3d 100644
@@ -11,6 +11,15 @@
 # When adding new dependencies, pin the very oldest non-yanked version
 # on PyPI that allows the test suite to pass.
 
+# For some reason, the presence of packaging==14.0 below requires us to
+# also pin setuptools to version 70 or below. Otherwise, the
+# installation of the QEMU package itself fails, failing to find
+# setuptools.
+setuptools<=70
+
+# Dependencies for qapidoc/qapi_domain et al
+sphinx==3.4.3
+
 # Dependencies for the TUI addon (Required for successful linting)
 urwid==2.1.2
 urwid-readline==0.13
@@ -49,3 +58,21 @@ platformdirs==2.2.0
 toml==0.10.0
 tomlkit==0.10.1
 wrapt==1.14.0
+
+# Transitive sphinx dependencies
+Jinja2==2.7
+MarkupSafe==1.1.0
+alabaster==0.7.1
+babel==1.3
+docutils==0.12
+imagesize==0.5.0
+packaging==14.0
+pytz==2011b0
+requests==2.5.0
+snowballstemmer==1.1
+sphinxcontrib-applehelp==1.0.0
+sphinxcontrib-devhelp==1.0.0
+sphinxcontrib-htmlhelp==1.0.0
+sphinxcontrib-jsmath==1.0.0
+sphinxcontrib-qthelp==1.0.0
+sphinxcontrib-serializinghtml==1.0.0
python/tests/qapi-flake8.sh new file +6 -0
new file mode 100755
index 00000000000..2211b6c7f41
@@ -0,0 +1,6 @@
+#!/bin/sh -e
+# SPDX-License-Identifier: LGPL-2.1-or-later
+
+python3 -m flake8 ../scripts/qapi/ \
+        ../docs/sphinx/qapidoc.py \
+        ../docs/sphinx/qapi_domain.py
python/tests/qapi-isort.sh new file +8 -0
new file mode 100755
index 00000000000..e546f5a7b5c
@@ -0,0 +1,8 @@
+#!/bin/sh -e
+# SPDX-License-Identifier: LGPL-2.1-or-later
+
+python3 -m isort --sp . -c ../scripts/qapi/
+# Force isort to recognize "compat" as a local module and not third-party
+python3 -m isort --sp . -c -p compat -p qapidoc_legacy \
+        ../docs/sphinx/qapi_domain.py \
+        ../docs/sphinx/qapidoc.py
python/tests/qapi-mypy.sh new file +4 -0
new file mode 100755
index 00000000000..50fb8ed1e3e
@@ -0,0 +1,4 @@
+#!/bin/sh -e
+# SPDX-License-Identifier: LGPL-2.1-or-later
+
+python3 -m mypy ../scripts/qapi
python/tests/qapi-pylint.sh new file +8 -0
new file mode 100755
index 00000000000..5f1b79c1883
@@ -0,0 +1,8 @@
+#!/bin/sh -e
+# SPDX-License-Identifier: LGPL-2.1-or-later
+
+SETUPTOOLS_USE_DISTUTILS=stdlib python3 -m pylint \
+                                --rcfile=../scripts/qapi/pylintrc \
+                                ../scripts/qapi/ \
+                                ../docs/sphinx/qapidoc.py \
+                                ../docs/sphinx/qapi_domain.py
scripts/qapi/pylintrc +1 -0
index d24eece7411..e16283ada3d 100644
@@ -19,6 +19,7 @@ disable=consider-using-f-string,
         too-many-instance-attributes,
         too-many-positional-arguments,
         too-many-statements,
+        unknown-option-value,
         useless-option-value,
 
 [REPORTS]
Signature · 1 line
2.48.1
#6 lore raw

Re: [PATCH v5 4/6] python: add qapi static analysis tests

In reply to John Snow

John Snow <[email protected]> writes:

[...]

python/tests/qapi-flake8.sh line 2

Quoted 8 lines
python/tests/qapi-flake8.sh new file +2 -0
new file mode 100755
index 00000000000..2211b6c7f41
@@ -0,0 +1,6 @@
+#!/bin/sh -e
+# SPDX-License-Identifier: LGPL-2.1-or-later

Why not GPL-2.0-or-later?

python/tests/qapi-isort.sh line 2

Quoted 12 lines
python/tests/qapi-flake8.sh new file +4 -0
+
+python3 -m flake8 ../scripts/qapi/ \
+        ../docs/sphinx/qapidoc.py \
+        ../docs/sphinx/qapi_domain.py
python/tests/qapi-isort.sh new file +2 -0
new file mode 100755
index 00000000000..e546f5a7b5c
@@ -0,0 +1,8 @@
+#!/bin/sh -e
+# SPDX-License-Identifier: LGPL-2.1-or-later

Likewise.

Quoted 12 lines
python/tests/qapi-isort.sh new file +6 -0
+
+python3 -m isort --sp . -c ../scripts/qapi/
+# Force isort to recognize "compat" as a local module and not third-party
+python3 -m isort --sp . -c -p compat -p qapidoc_legacy \
+        ../docs/sphinx/qapi_domain.py \
+        ../docs/sphinx/qapidoc.py
python/tests/qapi-mypy.sh new file +0 -0
new file mode 100755
index 00000000000..50fb8ed1e3e

python/tests/qapi-mypy.sh line 2

Quoted 2 lines
python/tests/qapi-mypy.sh new file +2 -0
@@ -0,0 +1,4 @@
+#!/bin/sh -e
+# SPDX-License-Identifier: LGPL-2.1-or-later

Likewise.

python/tests/qapi-pylint.sh line 2

Quoted 10 lines
python/tests/qapi-mypy.sh new file +2 -0
+
+python3 -m mypy ../scripts/qapi
python/tests/qapi-pylint.sh new file +2 -0
new file mode 100755
index 00000000000..5f1b79c1883
@@ -0,0 +1,8 @@
+#!/bin/sh -e
+# SPDX-License-Identifier: LGPL-2.1-or-later

Likewise.

Quoted 18 lines
python/tests/qapi-pylint.sh new file +6 -0
+
+SETUPTOOLS_USE_DISTUTILS=stdlib python3 -m pylint \
+                                --rcfile=../scripts/qapi/pylintrc \
+                                ../scripts/qapi/ \
+                                ../docs/sphinx/qapidoc.py \
+                                ../docs/sphinx/qapi_domain.py
scripts/qapi/pylintrc +1 -0
index d24eece7411..e16283ada3d 100644
@@ -19,6 +19,7 @@ disable=consider-using-f-string,
         too-many-instance-attributes,
         too-many-positional-arguments,
         too-many-statements,
+        unknown-option-value,
         useless-option-value,
 
 [REPORTS]
#7 lore raw

Re: [PATCH v5 4/6] python: add qapi static analysis tests

Quoted 4 lines · Markus Armbruster

John Snow <[email protected]> writes:

[...]

Quoted 10 lines

python/tests/qapi-flake8.sh line 2

Quoted 8 lines
python/tests/qapi-flake8.sh new file +2 -0
new file mode 100755
index 00000000000..2211b6c7f41
@@ -0,0 +1,6 @@
+#!/bin/sh -e
+# SPDX-License-Identifier: LGPL-2.1-or-later

Why not GPL-2.0-or-later?

Indeed, checkpatch.pl should be warning about this choice, and requesting that it be explained in the commit message.

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
author
#8 lore raw

Re: [PATCH v5 4/6] python: add qapi static analysis tests

Quoted 16 lines · Daniel P. Berrangé
Quoted 4 lines · Markus Armbruster

John Snow <[email protected]> writes:

[...]

Quoted 10 lines

python/tests/qapi-flake8.sh line 2

Quoted 8 lines
python/tests/qapi-flake8.sh new file +2 -0
new file mode 100755
index 00000000000..2211b6c7f41
@@ -0,0 +1,6 @@
+#!/bin/sh -e
+# SPDX-License-Identifier: LGPL-2.1-or-later

Why not GPL-2.0-or-later?

Quoted 3 lines

Indeed, checkpatch.pl should be warning about this choice, and requesting that it be explained in the commit message.

Ah. Well, it's what I have defaulted to for the standalone QMP library, but I realize now this code isn't anything I will be bothering to sync over to that library, so GPL is fine. I will adjust it for my pull request.

(And I'll fix the squashed commit message I forgot about! Oops!)

author
#9 lore raw

PATCH v5 5/6 python: Drop redundant warn_unused_configs = True

In reply to John Snow

From: Markus Armbruster <[email protected]>

strict = True implies warn_unused_configs = True.

---
 python/setup.cfg | 1 -
 1 file changed, 1 deletion(-)
python/setup.cfg +0 -1
index d21304cadd1..d7f5dc7bafe 100644
@@ -79,7 +79,6 @@ exclude = __pycache__,
 [mypy]
 strict = True
 python_version = 3.9
-warn_unused_configs = True
 namespace_packages = True
 warn_unused_ignores = False
 
Signature · 1 line
2.48.1
author
#10 lore raw

PATCH v5 6/6 qapi: delete un-needed python static analysis configs

In reply to John Snow

Since the previous commit, python/setup.cfg applies to scripts/qapi/ as well. Configuration files in scripts/qapi/ override python/setup.cfg.

scripts/qapi/.flake8 and scripts/qapi/.isort.cfg actually match python/setup.cfg exactly, and can go.

The differences between scripts/qapi/mypy.ini and python/setup.cfg are
harmless: namespace_packages being set to True is a requirement for the PEP420 nested package structure of QEMU but not for scripts/qapi, but has no effect on type checking the QAPI code. warn_unused_ignores is used in python/ to be able to target a wide variety of mypy versions; some of which that have added new ignore categories that are not present in older versions.

Ultimately, scripts/qapi/mypy.ini can be removed without any real change in behavior to how mypy enforces type safety there.

The pylint config is being left in place because the settings differ enough from the python/ directory settings that we need a chit-chat on how to merge them O:-)

---
 scripts/qapi/.flake8    | 3 ---
 scripts/qapi/.isort.cfg | 7 -------
 scripts/qapi/mypy.ini   | 4 ----
 3 files changed, 14 deletions(-)
 delete mode 100644 scripts/qapi/.flake8
 delete mode 100644 scripts/qapi/.isort.cfg
 delete mode 100644 scripts/qapi/mypy.ini
scripts/qapi/.flake8 deleted +0 -3
deleted file mode 100644
index a873ff67309..00000000000
@@ -1,3 +0,0 @@
-[flake8]
-# Prefer pylint's bare-except checks to flake8's
-extend-ignore = E722
scripts/qapi/.isort.cfg deleted +0 -7
deleted file mode 100644
index 643caa1fbd6..00000000000
@@ -1,7 +0,0 @@
-[settings]
-force_grid_wrap=4
-force_sort_within_sections=True
-include_trailing_comma=True
-line_length=72
-lines_after_imports=2
-multi_line_output=3
scripts/qapi/mypy.ini deleted +0 -4
deleted file mode 100644
index c9dbcec2db0..00000000000
@@ -1,4 +0,0 @@
-[mypy]
-strict = True
-disallow_untyped_calls = False
-python_version = 3.9
Signature · 1 line
2.48.1
Reviewed-by on the v5 series
#11 lore raw
In reply to John Snow
Quoted 11 lines · John Snow

Hiya, this series turns on automated linting for scripts/qapi, docs/sphinx/qapidoc.py and docs/sphinx/qapi_domain.py.

This includes flake8/isort/pylint/mypy for scripts/qapi, but omits mypy from the Sphinx plugins owing to my inability to strictly type the extensions given the wide versions of Sphinx we actually support.

[Maybe this will change soon, but it's not true just yet.]

Though I have been using black in my own development, I have not yet enabled it anywhere automatically. Maybe soon.

You lost my series

:)

author
#12 lore raw
Quoted 11 lines · Markus Armbruster
Quoted 9 lines · John Snow

Hiya, this series turns on automated linting for scripts/qapi, docs/sphinx/qapidoc.py and docs/sphinx/qapi_domain.py.

This includes flake8/isort/pylint/mypy for scripts/qapi, but omits mypy from the Sphinx plugins owing to my inability to strictly type the extensions given the wide versions of Sphinx we actually support.

[Maybe this will change soon, but it's not true just yet.]

Quoted 7 lines
Quoted 2 lines

Though I have been using black in my own development, I have not yet enabled it anywhere automatically. Maybe soon.

You lost my series

:)

Didn't expect to need to manually copy it forward to a v5 ...!

Thanks,
--js