From 0d57e307b2fea541a9ee368873431fe224e5c982 Mon Sep 17 00:00:00 2001 From: Daniel Mensinger Date: Fri, 28 Aug 2020 21:40:51 +0200 Subject: typing: fully annotate tools --- tools/build_website.py | 4 ++-- tools/cmake2meson.py | 19 ++++++++++--------- tools/dircondenser.py | 4 ++-- tools/regenerate_docs.py | 16 ++++++++-------- 4 files changed, 22 insertions(+), 21 deletions(-) (limited to 'tools') diff --git a/tools/build_website.py b/tools/build_website.py index 5486b6937..baf81db99 100755 --- a/tools/build_website.py +++ b/tools/build_website.py @@ -6,14 +6,14 @@ assert(os.getcwd() == '/home/jpakkane') from glob import glob -def purge(fname): +def purge(fname: str) -> None: if not os.path.exists(fname): return if os.path.isdir(fname): shutil.rmtree(fname) os.unlink(fname) -def update(): +def update() -> None: webdir = 'mesonweb' repodir = 'mesonwebbuild' docdir = os.path.join(repodir, 'docs') diff --git a/tools/cmake2meson.py b/tools/cmake2meson.py index 05acd8f68..e19ef682d 100755 --- a/tools/cmake2meson.py +++ b/tools/cmake2meson.py @@ -34,7 +34,7 @@ class Statement: self.args = args class Lexer: - def __init__(self): + def __init__(self) -> None: self.token_specification = [ # Need to be sorted longest to shortest. ('ignore', re.compile(r'[ \t]')), @@ -87,11 +87,11 @@ class Lexer: raise ValueError('Lexer got confused line %d column %d' % (lineno, col)) class Parser: - def __init__(self, code: str): + def __init__(self, code: str) -> None: self.stream = Lexer().lex(code) self.getsym() - def getsym(self): + def getsym(self) -> None: try: self.current = next(self.stream) except StopIteration: @@ -118,8 +118,8 @@ class Parser: self.expect('rparen') return Statement(cur.value, args) - def arguments(self) -> list: - args = [] + def arguments(self) -> T.List[T.Union[Token, T.Any]]: + args = [] # type: T.List[T.Union[Token, T.Any]] if self.accept('lparen'): args.append(self.arguments()) self.expect('rparen') @@ -139,7 +139,7 @@ class Parser: while not self.accept('eof'): yield(self.statement()) -def token_or_group(arg): +def token_or_group(arg: T.Union[Token, T.List[Token]]) -> str: if isinstance(arg, Token): return ' ' + arg.value elif isinstance(arg, list): @@ -148,6 +148,7 @@ def token_or_group(arg): line += ' ' + token_or_group(a) line += ' )' return line + raise RuntimeError('Conversion error in token_or_group') class Converter: ignored_funcs = {'cmake_minimum_required': True, @@ -183,7 +184,7 @@ class Converter: return res[0] return '' - def write_entry(self, outfile: T.TextIO, t: Statement): + def write_entry(self, outfile: T.TextIO, t: Statement) -> None: if t.name in Converter.ignored_funcs: return preincrement = 0 @@ -274,7 +275,7 @@ class Converter: outfile.write('\n') self.indent_level += postincrement - def convert(self, subdir: Path = None): + def convert(self, subdir: Path = None) -> None: if not subdir: subdir = self.cmake_root cfile = Path(subdir).expanduser() / 'CMakeLists.txt' @@ -297,7 +298,7 @@ class Converter: if subdir == self.cmake_root and len(self.options) > 0: self.write_options() - def write_options(self): + def write_options(self) -> None: filename = self.cmake_root / 'meson_options.txt' with filename.open('w') as optfile: for o in self.options: diff --git a/tools/dircondenser.py b/tools/dircondenser.py index 0e28becca..8da0ce292 100755 --- a/tools/dircondenser.py +++ b/tools/dircondenser.py @@ -53,7 +53,7 @@ def get_entries() -> T.List[T.Tuple[int, str]]: entries.sort() return entries -def replace_source(sourcefile: str, replacements: T.List[T.Tuple[str, str]]): +def replace_source(sourcefile: str, replacements: T.List[T.Tuple[str, str]]) -> None: with open(sourcefile, 'r') as f: contents = f.read() for old_name, new_name in replacements: @@ -61,7 +61,7 @@ def replace_source(sourcefile: str, replacements: T.List[T.Tuple[str, str]]): with open(sourcefile, 'w') as f: f.write(contents) -def condense(dirname: str): +def condense(dirname: str) -> None: curdir = os.getcwd() os.chdir(dirname) entries = get_entries() diff --git a/tools/regenerate_docs.py b/tools/regenerate_docs.py index d44357036..74a8b0c0c 100755 --- a/tools/regenerate_docs.py +++ b/tools/regenerate_docs.py @@ -31,21 +31,21 @@ from pathlib import Path PathLike = T.Union[Path,str] -def _get_meson_output(root_dir: Path, args: T.List): +def _get_meson_output(root_dir: Path, args: T.List) -> str: env = os.environ.copy() env['COLUMNS'] = '80' return subprocess.run([str(sys.executable), str(root_dir/'meson.py')] + args, check=True, capture_output=True, text=True, env=env).stdout.strip() -def get_commands_data(root_dir: Path): +def get_commands_data(root_dir: Path) -> T.Dict[str, T.Any]: usage_start_pattern = re.compile(r'^usage: ', re.MULTILINE) positional_start_pattern = re.compile(r'^positional arguments:[\t ]*[\r\n]+', re.MULTILINE) options_start_pattern = re.compile(r'^optional arguments:[\t ]*[\r\n]+', re.MULTILINE) commands_start_pattern = re.compile(r'^[A-Za-z ]*[Cc]ommands:[\t ]*[\r\n]+', re.MULTILINE) - def get_next_start(iterators, end): + def get_next_start(iterators: T.Sequence[T.Any], end: T.Optional[int]) -> int: return next((i.start() for i in iterators if i), end) - def normalize_text(text): + def normalize_text(text: str) -> str: # clean up formatting out = text out = re.sub(r'\r\n', r'\r', out, flags=re.MULTILINE) # replace newlines with a linux EOL @@ -53,7 +53,7 @@ def get_commands_data(root_dir: Path): out = re.sub(r'(?:^\n+|\n+$)', '', out) # remove trailing empty lines return out - def parse_cmd(cmd): + def parse_cmd(cmd: str) -> T.Dict[str, str]: cmd_len = len(cmd) usage = usage_start_pattern.search(cmd) positionals = positional_start_pattern.search(cmd) @@ -72,7 +72,7 @@ def get_commands_data(root_dir: Path): 'arguments': normalize_text(cmd[arguments_start:cmd_len]), } - def clean_dir_arguments(text): + def clean_dir_arguments(text: str) -> str: # Remove platform specific defaults args = [ 'prefix', @@ -127,7 +127,7 @@ def regenerate_docs(output_dir: PathLike, dummy_output_file: T.Optional[PathLike]) -> None: if not output_dir: raise ValueError(f'Output directory value is not set') - + output_dir = Path(output_dir).resolve() output_dir.mkdir(parents=True, exist_ok=True) @@ -143,7 +143,7 @@ if __name__ == '__main__': parser = argparse.ArgumentParser(description='Generate meson docs') parser.add_argument('--output-dir', required=True) parser.add_argument('--dummy-output-file', type=str) - + args = parser.parse_args() regenerate_docs(output_dir=args.output_dir, -- cgit v1.2.3 From e681235e5fe3ee0a40dd6a3f5922c2c4b0cf98b4 Mon Sep 17 00:00:00 2001 From: Daniel Mensinger Date: Tue, 1 Sep 2020 19:58:10 +0200 Subject: typing: fix code review --- mesonbuild/arglist.py | 8 ++++---- mesonbuild/ast/introspection.py | 2 +- mesonbuild/build.py | 8 ++++---- mesonbuild/dependencies/boost.py | 8 ++++---- mesonbuild/envconfig.py | 10 +++++----- mesonbuild/mesonlib.py | 32 ++++++++++++++++---------------- mesonbuild/mintro.py | 3 ++- mesonbuild/mparser.py | 2 +- mesonbuild/scripts/depfixer.py | 2 +- mesonbuild/wrap/wrap.py | 2 +- run_project_tests.py | 2 +- tools/boost_names.py | 6 +++--- 12 files changed, 43 insertions(+), 42 deletions(-) (limited to 'tools') diff --git a/mesonbuild/arglist.py b/mesonbuild/arglist.py index d88438957..d1d489bfd 100644 --- a/mesonbuild/arglist.py +++ b/mesonbuild/arglist.py @@ -164,7 +164,7 @@ class CompilerArgs(collections.abc.MutableSequence): def __getitem__(self, index: slice) -> T.MutableSequence[str]: # noqa: F811 pass - def __getitem__(self, index): # type: ignore # noqa: F811 + def __getitem__(self, index: T.Union[int, slice]) -> T.Union[str, T.MutableSequence[str]]: # noqa: F811 self.flush_pre_post() return self._container[index] @@ -176,9 +176,9 @@ class CompilerArgs(collections.abc.MutableSequence): def __setitem__(self, index: slice, value: T.Iterable[str]) -> None: # noqa: F811 pass - def __setitem__(self, index, value) -> None: # type: ignore # noqa: F811 + def __setitem__(self, index: T.Union[int, slice], value: T.Union[str, T.Iterable[str]]) -> None: # noqa: F811 self.flush_pre_post() - self._container[index] = value + self._container[index] = value # type: ignore # TODO: fix 'Invalid index type' and 'Incompatible types in assignment' erros def __delitem__(self, index: T.Union[int, slice]) -> None: self.flush_pre_post() @@ -314,7 +314,7 @@ class CompilerArgs(collections.abc.MutableSequence): new += self return new - def __eq__(self, other: T.Any) -> T.Union[bool]: + def __eq__(self, other: object) -> T.Union[bool]: self.flush_pre_post() # Only allow equality checks against other CompilerArgs and lists instances if isinstance(other, CompilerArgs): diff --git a/mesonbuild/ast/introspection.py b/mesonbuild/ast/introspection.py index 19fedbf6f..d7d982e44 100644 --- a/mesonbuild/ast/introspection.py +++ b/mesonbuild/ast/introspection.py @@ -296,7 +296,7 @@ class IntrospectionInterpreter(AstInterpreter): return None def is_subproject(self) -> bool: - return str(self.subproject) != '' + return self.subproject != '' def analyze(self) -> None: self.load_root_meson_file() diff --git a/mesonbuild/build.py b/mesonbuild/build.py index bf325b090..369ac7bf6 100644 --- a/mesonbuild/build.py +++ b/mesonbuild/build.py @@ -372,22 +372,22 @@ a hard error in the future.'''.format(name)) if not hasattr(self, 'typename'): raise RuntimeError('Target type is not set for target class "{}". This is a bug'.format(type(self).__name__)) - def __lt__(self, other: T.Any) -> T.Union[bool, type(NotImplemented)]: + def __lt__(self, other: object) -> T.Union[bool, type(NotImplemented)]: if not hasattr(other, 'get_id') and not callable(other.get_id): return NotImplemented return self.get_id() < other.get_id() - def __le__(self, other: T.Any) -> T.Union[bool, type(NotImplemented)]: + def __le__(self, other: object) -> T.Union[bool, type(NotImplemented)]: if not hasattr(other, 'get_id') and not callable(other.get_id): return NotImplemented return self.get_id() <= other.get_id() - def __gt__(self, other: T.Any) -> T.Union[bool, type(NotImplemented)]: + def __gt__(self, other: object) -> T.Union[bool, type(NotImplemented)]: if not hasattr(other, 'get_id') and not callable(other.get_id): return NotImplemented return self.get_id() > other.get_id() - def __ge__(self, other: T.Any) -> T.Union[bool, type(NotImplemented)]: + def __ge__(self, other: object) -> T.Union[bool, type(NotImplemented)]: if not hasattr(other, 'get_id') and not callable(other.get_id): return NotImplemented return self.get_id() >= other.get_id() diff --git a/mesonbuild/dependencies/boost.py b/mesonbuild/dependencies/boost.py index 6e9586029..370fa72d1 100644 --- a/mesonbuild/dependencies/boost.py +++ b/mesonbuild/dependencies/boost.py @@ -95,7 +95,7 @@ class BoostIncludeDir(): def __repr__(self) -> str: return ''.format(self.version, self.path) - def __lt__(self, other: T.Any) -> bool: + def __lt__(self, other: object) -> bool: if isinstance(other, BoostIncludeDir): return (self.version_int, self.path) < (other.version_int, other.path) return NotImplemented @@ -187,7 +187,7 @@ class BoostLibraryFile(): def __repr__(self) -> str: return ''.format(self.abitag, self.mod_name, self.path) - def __lt__(self, other: T.Any) -> bool: + def __lt__(self, other: object) -> bool: if isinstance(other, BoostLibraryFile): return ( self.mod_name, self.static, self.version_lib, self.arch, @@ -204,7 +204,7 @@ class BoostLibraryFile(): ) return NotImplemented - def __eq__(self, other: T.Any) -> bool: + def __eq__(self, other: object) -> bool: if isinstance(other, BoostLibraryFile): return self.name == other.name return NotImplemented @@ -346,7 +346,7 @@ class BoostDependency(ExternalDependency): self.debug = buildtype.startswith('debug') self.multithreading = kwargs.get('threading', 'multi') == 'multi' - self.boost_root = None # type: Path + self.boost_root = None # type: T.Optional[Path] self.explicit_static = 'static' in kwargs # Extract and validate modules diff --git a/mesonbuild/envconfig.py b/mesonbuild/envconfig.py index 127066652..836ec069b 100644 --- a/mesonbuild/envconfig.py +++ b/mesonbuild/envconfig.py @@ -147,7 +147,7 @@ class Properties: return p return mesonlib.listify(p) - def __eq__(self, other: T.Any) -> 'T.Union[bool, NotImplemented]': + def __eq__(self, other: object) -> 'T.Union[bool, NotImplemented]': if isinstance(other, type(self)): return self.properties == other.properties return NotImplemented @@ -172,8 +172,8 @@ class MachineInfo: self.endian = endian self.is_64_bit = cpu_family in CPU_FAMILES_64_BIT # type: bool - def __eq__(self, other: T.Any) -> 'T.Union[bool, NotImplemented]': - if self.__class__ is not other.__class__: + def __eq__(self, other: object) -> 'T.Union[bool, NotImplemented]': + if self.__class__ is not other.__class__ or not isinstance(other, MachineInfo): return NotImplemented return \ self.system == other.system and \ @@ -181,8 +181,8 @@ class MachineInfo: self.cpu == other.cpu and \ self.endian == other.endian - def __ne__(self, other: T.Any) -> 'T.Union[bool, NotImplemented]': - if self.__class__ is not other.__class__: + def __ne__(self, other: object) -> 'T.Union[bool, NotImplemented]': + if self.__class__ is not other.__class__ or not isinstance(other, MachineInfo): return NotImplemented return not self.__eq__(other) diff --git a/mesonbuild/mesonlib.py b/mesonbuild/mesonlib.py index 769a904cd..6cc3c081a 100644 --- a/mesonbuild/mesonlib.py +++ b/mesonbuild/mesonlib.py @@ -289,7 +289,7 @@ class File: def split(self, s: str) -> T.List[str]: return self.fname.split(s) - def __eq__(self, other: T.Any) -> bool: + def __eq__(self, other: object) -> bool: if not isinstance(other, File): return NotImplemented if self.hash != other.hash: @@ -327,23 +327,23 @@ class OrderedEnum(Enum): """ An Enum which additionally offers homogeneous ordered comparison. """ - def __ge__(self, other: T.Any) -> bool: - if self.__class__ is other.__class__ and isinstance(self.value, int) and isinstance(other.value, int): + def __ge__(self, other: object) -> bool: + if self.__class__ is other.__class__ and isinstance(other, OrderedEnum)and isinstance(self.value, int) and isinstance(other.value, int): return self.value >= other.value return NotImplemented - def __gt__(self, other: T.Any) -> bool: - if self.__class__ is other.__class__ and isinstance(self.value, int) and isinstance(other.value, int): + def __gt__(self, other: object) -> bool: + if self.__class__ is other.__class__ and isinstance(other, OrderedEnum)and isinstance(self.value, int) and isinstance(other.value, int): return self.value > other.value return NotImplemented - def __le__(self, other: T.Any) -> bool: - if self.__class__ is other.__class__ and isinstance(self.value, int) and isinstance(other.value, int): + def __le__(self, other: object) -> bool: + if self.__class__ is other.__class__ and isinstance(other, OrderedEnum)and isinstance(self.value, int) and isinstance(other.value, int): return self.value <= other.value return NotImplemented - def __lt__(self, other: T.Any) -> bool: - if self.__class__ is other.__class__ and isinstance(self.value, int) and isinstance(other.value, int): + def __lt__(self, other: object) -> bool: + if self.__class__ is other.__class__ and isinstance(other, OrderedEnum) and isinstance(self.value, int) and isinstance(other.value, int): return self.value < other.value return NotImplemented @@ -609,32 +609,32 @@ class Version: def __repr__(self) -> str: return ''.format(self._s) - def __lt__(self, other: T.Any) -> bool: + def __lt__(self, other: object) -> bool: if isinstance(other, Version): return self.__cmp(other, operator.lt) return NotImplemented - def __gt__(self, other: T.Any) -> bool: + def __gt__(self, other: object) -> bool: if isinstance(other, Version): return self.__cmp(other, operator.gt) return NotImplemented - def __le__(self, other: T.Any) -> bool: + def __le__(self, other: object) -> bool: if isinstance(other, Version): return self.__cmp(other, operator.le) return NotImplemented - def __ge__(self, other: T.Any) -> bool: + def __ge__(self, other: object) -> bool: if isinstance(other, Version): return self.__cmp(other, operator.ge) return NotImplemented - def __eq__(self, other: T.Any) -> bool: + def __eq__(self, other: object) -> bool: if isinstance(other, Version): return self._v == other._v return NotImplemented - def __ne__(self, other: T.Any) -> bool: + def __ne__(self, other: object) -> bool: if isinstance(other, Version): return self._v != other._v return NotImplemented @@ -1115,7 +1115,7 @@ def unholder(item: T.List[_T]) -> T.List[_T]: ... @T.overload def unholder(item: T.List[T.Union[_T, 'ObjectHolder[_T]']]) -> T.List[_T]: ... -def unholder(item): # type: ignore # TODO for some reason mypy throws the "Function is missing a type annotation" error +def unholder(item): # type: ignore # TODO fix overload (somehow) """Get the held item of an object holder or list of object holders.""" if isinstance(item, list): return [i.held_object if hasattr(i, 'held_object') else i for i in item] diff --git a/mesonbuild/mintro.py b/mesonbuild/mintro.py index 1e400b0d8..66bbe2ab8 100644 --- a/mesonbuild/mintro.py +++ b/mesonbuild/mintro.py @@ -30,7 +30,8 @@ from .mparser import BaseNode, FunctionNode, ArrayNode, ArgumentNode, StringNode from .interpreter import Interpreter from pathlib import PurePath import typing as T -import os, argparse +import os +import argparse def get_meson_info_file(info_dir: str) -> str: return os.path.join(info_dir, 'meson-info.json') diff --git a/mesonbuild/mparser.py b/mesonbuild/mparser.py index e3868583f..a60109d01 100644 --- a/mesonbuild/mparser.py +++ b/mesonbuild/mparser.py @@ -97,7 +97,7 @@ class Token(T.Generic[TV_TokenTypes]): self.bytespan = bytespan # type: T.Tuple[int, int] self.value = value # type: TV_TokenTypes - def __eq__(self, other: T.Any) -> bool: + def __eq__(self, other: object) -> bool: if isinstance(other, str): return self.tid == other elif isinstance(other, Token): diff --git a/mesonbuild/scripts/depfixer.py b/mesonbuild/scripts/depfixer.py index 76cf2b50a..18d70cca9 100644 --- a/mesonbuild/scripts/depfixer.py +++ b/mesonbuild/scripts/depfixer.py @@ -328,7 +328,7 @@ class Elf(DataSizes): new_rpath = b':'.join(new_rpaths) if len(old_rpath) < len(new_rpath): - msg = "New rpath must not be longer than the old one.\n Old: {!r}\n New: {!r}".format(old_rpath, new_rpath) + msg = "New rpath must not be longer than the old one.\n Old: {}\n New: {}".format(old_rpath.decode('utf-8'), new_rpath.decode('utf-8')) sys.exit(msg) # The linker does read-only string deduplication. If there is a # string that shares a suffix with the rpath, they might get diff --git a/mesonbuild/wrap/wrap.py b/mesonbuild/wrap/wrap.py index eacc1ddd5..68f83c129 100644 --- a/mesonbuild/wrap/wrap.py +++ b/mesonbuild/wrap/wrap.py @@ -107,7 +107,7 @@ class WrapNotFoundException(WrapException): class PackageDefinition: def __init__(self, fname: str): self.filename = fname - self.type = None # type: str + self.type = None # type: T.Optional[str] self.values = {} # type: T.Dict[str, str] self.provided_deps = {} # type: T.Dict[str, T.Optional[str]] self.provided_programs = [] # type: T.List[str] diff --git a/run_project_tests.py b/run_project_tests.py index c84388892..4566de12f 100755 --- a/run_project_tests.py +++ b/run_project_tests.py @@ -216,7 +216,7 @@ class TestDef: return '{} ({})'.format(self.path.as_posix(), self.name) return self.path.as_posix() - def __lt__(self, other: T.Any) -> bool: + def __lt__(self, other: object) -> bool: if isinstance(other, TestDef): # None is not sortable, so replace it with an empty string s_id = int(self.path.name.split(' ')[0]) diff --git a/tools/boost_names.py b/tools/boost_names.py index b66c6cc1e..897b4fd6d 100755 --- a/tools/boost_names.py +++ b/tools/boost_names.py @@ -48,12 +48,12 @@ class BoostLibrary(): self.single = sorted(set(single)) self.multi = sorted(set(multi)) - def __lt__(self, other: T.Any) -> T.Union[bool, 'NotImplemented']: + def __lt__(self, other: object) -> T.Union[bool, 'NotImplemented']: if isinstance(other, BoostLibrary): return self.name < other.name return NotImplemented - def __eq__(self, other: T.Any) -> T.Union[bool, 'NotImplemented']: + def __eq__(self, other: object) -> T.Union[bool, 'NotImplemented']: if isinstance(other, BoostLibrary): return self.name == other.name elif isinstance(other, str): @@ -71,7 +71,7 @@ class BoostModule(): self.desc = desc self.libs = libs - def __lt__(self, other: T.Any) -> T.Union[bool, 'NotImplemented']: + def __lt__(self, other: object) -> T.Union[bool, 'NotImplemented']: if isinstance(other, BoostModule): return self.key < other.key return NotImplemented -- cgit v1.2.3