qemu-patch-raspberry4/tests/qemu-iotests/linters.py
John Snow 558dbe9935 iotests/linters: Add workaround for mypy bug #9852
This one is insidious: if you write an import as "from {namespace}
import {subpackage}" as mirror-top-perms (now) does, mypy will fail on
every-other invocation *if* the package being imported is a typed,
installed, namespace-scoped package.

Upsettingly, that's exactly what 'qemu.[aqmp|qmp|machine]' et al are in
the context of Python CI tests.

Now, I could just edit mirror-top-perms to avoid this invocation, but
since I tripped on a landmine, I might as well head it off at the pass
and make sure nobody else trips on that same landmine.

It seems to have something to do with the order in which files are
checked as well, meaning the random order in which set(os.listdir())
produces the list of files to test will cause problems intermittently
and not just strictly "every other run".

This will be fixed in mypy >= 0.920, which is not released yet. The
workaround for now is to disable incremental checking, which avoids the
issue.

Note: This workaround is not applied when running iotest 297 directly,
because the bug does not surface there! Given the nature of CI jobs not
starting with any stale cache to begin with, this really only has a
half-second impact on manual runs of the Python test suite when executed
directly by a developer on their local machine. The workaround may be
removed when the Python package requirements can stipulate mypy 0.920 or
higher, which can happen as soon as it is released. (Barring any
unforseen compatibility issues that 0.920 may bring with it.)

See also:
 https://github.com/python/mypy/issues/11010
 https://github.com/python/mypy/issues/9852

Signed-off-by: John Snow <jsnow@redhat.com>
Reviewed-by: Hanna Reitz <hreitz@redhat.com>
Message-id: 20211019144918.3159078-14-jsnow@redhat.com
Signed-off-by: John Snow <jsnow@redhat.com>
2021-11-01 11:54:59 -04:00

106 lines
3.3 KiB
Python

# Copyright (C) 2020 Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
import re
import subprocess
import sys
from typing import List, Mapping, Optional
# TODO: Empty this list!
SKIP_FILES = (
'030', '040', '041', '044', '045', '055', '056', '057', '065', '093',
'096', '118', '124', '132', '136', '139', '147', '148', '149',
'151', '152', '155', '163', '165', '194', '196', '202',
'203', '205', '206', '207', '208', '210', '211', '212', '213', '216',
'218', '219', '224', '228', '234', '235', '236', '237', '238',
'240', '242', '245', '246', '248', '255', '256', '257', '258', '260',
'262', '264', '266', '274', '277', '280', '281', '295', '296', '298',
'299', '302', '303', '304', '307',
'nbd-fault-injector.py', 'qcow2.py', 'qcow2_format.py', 'qed.py'
)
def is_python_file(filename):
if not os.path.isfile(filename):
return False
if filename.endswith('.py'):
return True
with open(filename, encoding='utf-8') as f:
try:
first_line = f.readline()
return re.match('^#!.*python', first_line) is not None
except UnicodeDecodeError: # Ignore binary files
return False
def get_test_files() -> List[str]:
named_tests = [f'tests/{entry}' for entry in os.listdir('tests')]
check_tests = set(os.listdir('.') + named_tests) - set(SKIP_FILES)
return list(filter(is_python_file, check_tests))
def run_linter(
tool: str,
args: List[str],
env: Optional[Mapping[str, str]] = None,
suppress_output: bool = False,
) -> None:
"""
Run a python-based linting tool.
:param suppress_output: If True, suppress all stdout/stderr output.
:raise CalledProcessError: If the linter process exits with failure.
"""
subprocess.run(
('python3', '-m', tool, *args),
env=env,
check=True,
stdout=subprocess.PIPE if suppress_output else None,
stderr=subprocess.STDOUT if suppress_output else None,
universal_newlines=True,
)
def main() -> None:
"""
Used by the Python CI system as an entry point to run these linters.
"""
def show_usage() -> None:
print(f"Usage: {sys.argv[0]} < --mypy | --pylint >", file=sys.stderr)
sys.exit(1)
if len(sys.argv) != 2:
show_usage()
files = get_test_files()
if sys.argv[1] == '--pylint':
run_linter('pylint', files)
elif sys.argv[1] == '--mypy':
# mypy bug #9852; disable incremental checking as a workaround.
args = ['--no-incremental'] + files
run_linter('mypy', args)
else:
print(f"Unrecognized argument: '{sys.argv[1]}'", file=sys.stderr)
show_usage()
if __name__ == '__main__':
main()