diff options
author | Daniel Mensinger <daniel@mensinger-ka.de> | 2020-08-28 19:48:00 +0200 |
---|---|---|
committer | Daniel Mensinger <daniel@mensinger-ka.de> | 2020-09-08 20:15:56 +0200 |
commit | fb9738b8c7dd1a900898a850b22810c5370fd9e5 (patch) | |
tree | 899fc44ffaa64ebe521a133445df1bb2296c953e | |
parent | bb09ca9ad551e00780dbd6a12ae1619859d4bc6a (diff) | |
download | meson-fb9738b8c7dd1a900898a850b22810c5370fd9e5.zip meson-fb9738b8c7dd1a900898a850b22810c5370fd9e5.tar.gz meson-fb9738b8c7dd1a900898a850b22810c5370fd9e5.tar.bz2 |
typing: fully annotate mparser.py
-rw-r--r-- | mesonbuild/interpreterbase.py | 8 | ||||
-rw-r--r-- | mesonbuild/mparser.py | 39 | ||||
-rwxr-xr-x | run_mypy.py | 2 |
3 files changed, 31 insertions, 18 deletions
diff --git a/mesonbuild/interpreterbase.py b/mesonbuild/interpreterbase.py index 43de2f2..f6c9559 100644 --- a/mesonbuild/interpreterbase.py +++ b/mesonbuild/interpreterbase.py @@ -454,7 +454,7 @@ def is_disabled(args: T.Sequence[T.Any], kwargs: T.Dict[str, T.Any]) -> bool: def default_resolve_key(key: mparser.BaseNode) -> str: if not isinstance(key, mparser.IdNode): raise InterpreterException('Invalid kwargs format.') - return T.cast(str, key.value) + return key.value class InterpreterBase: elementary_types = (int, float, str, bool, list) @@ -552,9 +552,9 @@ class InterpreterBase: elif isinstance(cur, mparser.MethodNode): return self.method_call(cur) elif isinstance(cur, mparser.StringNode): - return T.cast(str, cur.value) + return cur.value elif isinstance(cur, mparser.BooleanNode): - return T.cast(bool, cur.value) + return cur.value elif isinstance(cur, mparser.IfClauseNode): return self.evaluate_if(cur) elif isinstance(cur, mparser.IdNode): @@ -566,7 +566,7 @@ class InterpreterBase: elif isinstance(cur, mparser.DictNode): return self.evaluate_dictstatement(cur) elif isinstance(cur, mparser.NumberNode): - return T.cast(int, cur.value) + return cur.value elif isinstance(cur, mparser.AndNode): return self.evaluate_andstatement(cur) elif isinstance(cur, mparser.OrNode): diff --git a/mesonbuild/mparser.py b/mesonbuild/mparser.py index b9e381e..e386858 100644 --- a/mesonbuild/mparser.py +++ b/mesonbuild/mparser.py @@ -35,25 +35,34 @@ ESCAPE_SEQUENCE_SINGLE_RE = re.compile(r''' )''', re.UNICODE | re.VERBOSE) class MesonUnicodeDecodeError(MesonException): - def __init__(self, match): - super().__init__("%s" % match) + def __init__(self, match: str) -> None: + super().__init__(match) self.match = match -def decode_match(match): +def decode_match(match: T.Match[str]) -> str: try: - return codecs.decode(match.group(0), 'unicode_escape') + return codecs.decode(match.group(0).encode(), 'unicode_escape') except UnicodeDecodeError: raise MesonUnicodeDecodeError(match.group(0)) class ParseException(MesonException): - def __init__(self, text, line, lineno, colno): + def __init__(self, text: str, line: str, lineno: int, colno: int) -> None: # Format as error message, followed by the line with the error, followed by a caret to show the error column. - super().__init__("%s\n%s\n%s" % (text, line, '%s^' % (' ' * colno))) + super().__init__("{}\n{}\n{}".format(text, line, '{}^'.format(' ' * colno))) self.lineno = lineno self.colno = colno class BlockParseException(MesonException): - def __init__(self, text, line, lineno, colno, start_line, start_lineno, start_colno): + def __init__( + self, + text: str, + line: str, + lineno: int, + colno: int, + start_line: str, + start_lineno: int, + start_colno: int, + ) -> None: # This can be formatted in two ways - one if the block start and end are on the same line, and a different way if they are on different lines. if lineno == start_lineno: @@ -88,10 +97,12 @@ class Token(T.Generic[TV_TokenTypes]): self.bytespan = bytespan # type: T.Tuple[int, int] self.value = value # type: TV_TokenTypes - def __eq__(self, other) -> bool: + def __eq__(self, other: T.Any) -> bool: if isinstance(other, str): return self.tid == other - return self.tid == other.tid + elif isinstance(other, Token): + return self.tid == other.tid + return NotImplemented class Lexer: def __init__(self, code: str): @@ -261,7 +272,7 @@ class IdNode(ElementaryNode[str]): super().__init__(token) assert isinstance(self.value, str) - def __str__(self): + def __str__(self) -> str: return "Id node: '%s' (%d, %d)." % (self.value, self.lineno, self.colno) class NumberNode(ElementaryNode[int]): @@ -274,7 +285,7 @@ class StringNode(ElementaryNode[str]): super().__init__(token) assert isinstance(self.value, str) - def __str__(self): + def __str__(self) -> str: return "String node: '%s' (%d, %d)." % (self.value, self.lineno, self.colno) class ContinueNode(ElementaryNode): @@ -703,7 +714,7 @@ class Parser: s = self.statement() return a - def method_call(self, source_object) -> MethodNode: + def method_call(self, source_object: BaseNode) -> MethodNode: methodname = self.e9() if not(isinstance(methodname, IdNode)): raise ParseException('Method name must be plain id', @@ -717,7 +728,7 @@ class Parser: return self.method_call(method) return method - def index_call(self, source_object) -> IndexNode: + def index_call(self, source_object: BaseNode) -> IndexNode: index_statement = self.statement() self.expect('rbracket') return IndexNode(source_object, index_statement) @@ -750,7 +761,7 @@ class Parser: clause.elseblock = self.elseblock() return clause - def elseifblock(self, clause) -> None: + def elseifblock(self, clause: IfClauseNode) -> None: while self.accept('elif'): s = self.statement() self.expect('eol') diff --git a/run_mypy.py b/run_mypy.py index e9b8229..4e56168 100755 --- a/run_mypy.py +++ b/run_mypy.py @@ -30,9 +30,11 @@ normal_modules = [ strict_modules = [ 'mesonbuild/interpreterbase.py', + 'mesonbuild/mparser.py', 'mesonbuild/mesonlib.py', 'mesonbuild/mlog.py', 'mesonbuild/ast', + # 'mesonbuild/wrap', 'run_mypy.py', ] |