]> Git Repo - J-linux.git/commitdiff
kunit: tool: fix typecheck errors about loading qemu configs
authorDaniel Latypov <[email protected]>
Fri, 22 Oct 2021 00:49:36 +0000 (17:49 -0700)
committerShuah Khan <[email protected]>
Fri, 29 Oct 2021 19:05:47 +0000 (13:05 -0600)
Currently, we have these errors:
$ mypy ./tools/testing/kunit/*.py
tools/testing/kunit/kunit_kernel.py:213: error: Item "_Loader" of "Optional[_Loader]" has no attribute "exec_module"
tools/testing/kunit/kunit_kernel.py:213: error: Item "None" of "Optional[_Loader]" has no attribute "exec_module"
tools/testing/kunit/kunit_kernel.py:214: error: Module has no attribute "QEMU_ARCH"
tools/testing/kunit/kunit_kernel.py:215: error: Module has no attribute "QEMU_ARCH"

exec_module
===========

pytype currently reports no errors, but that's because there's a comment
disabling it on 213.

This is due to https://github.com/python/typeshed/pull/2626.
The fix is to assert the loaded module implements the ABC
(abstract base class) we want which has exec_module support.

QEMU_ARCH
=========

pytype is fine with this, but mypy is not:
https://github.com/python/mypy/issues/5059

Add a check that the loaded module does indeed have QEMU_ARCH.
Note: this is not enough to appease mypy, so we also add a comment to
squash the warning.

Signed-off-by: Daniel Latypov <[email protected]>
Reviewed-by: David Gow <[email protected]>
Reviewed-by: Brendan Higgins <[email protected]>
Signed-off-by: Shuah Khan <[email protected]>
tools/testing/kunit/kunit_kernel.py

index f08c6c36a947863bb666bd2033c9efc66e3ef400..66095568bf327eb65045019a676c4ab3257d518f 100644 (file)
@@ -207,12 +207,15 @@ def get_source_tree_ops_from_qemu_config(config_path: str,
        module_path = '.' + os.path.join(os.path.basename(QEMU_CONFIGS_DIR), os.path.basename(config_path))
        spec = importlib.util.spec_from_file_location(module_path, config_path)
        config = importlib.util.module_from_spec(spec)
-       # TODO([email protected]): I looked this up and apparently other
-       # Python projects have noted that pytype complains that "No attribute
-       # 'exec_module' on _importlib_modulespec._Loader". Disabling for now.
-       spec.loader.exec_module(config) # pytype: disable=attribute-error
-       return config.QEMU_ARCH.linux_arch, LinuxSourceTreeOperationsQemu(
-                       config.QEMU_ARCH, cross_compile=cross_compile)
+       # See https://github.com/python/typeshed/pull/2626 for context.
+       assert isinstance(spec.loader, importlib.abc.Loader)
+       spec.loader.exec_module(config)
+
+       if not hasattr(config, 'QEMU_ARCH'):
+               raise ValueError('qemu_config module missing "QEMU_ARCH": ' + config_path)
+       params: qemu_config.QemuArchParams = config.QEMU_ARCH  # type: ignore
+       return params.linux_arch, LinuxSourceTreeOperationsQemu(
+                       params, cross_compile=cross_compile)
 
 class LinuxSourceTree(object):
        """Represents a Linux kernel source tree with KUnit tests."""
This page took 0.055514 seconds and 4 git commands to generate.