From e681235e5fe3ee0a40dd6a3f5922c2c4b0cf98b4 Mon Sep 17 00:00:00 2001 From: Daniel Mensinger Date: Tue, 1 Sep 2020 19:58:10 +0200 Subject: [PATCH] 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(-) 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