diff options
author | Eli Schwartz <eschwartz@archlinux.org> | 2021-12-04 21:11:51 -0500 |
---|---|---|
committer | Eli Schwartz <eschwartz@archlinux.org> | 2022-01-10 18:36:57 -0500 |
commit | 4b351aef26a19b4c73f6ef295f64da1c74bc713d (patch) | |
tree | 44f0b5c34c6bfb06360ffc22870ff423fc9305e2 /mesonbuild/mparser.py | |
parent | 98a41ec24e77d7670ea83fd986853d0fe7cb2f5b (diff) | |
download | meson-4b351aef26a19b4c73f6ef295f64da1c74bc713d.zip meson-4b351aef26a19b4c73f6ef295f64da1c74bc713d.tar.gz meson-4b351aef26a19b4c73f6ef295f64da1c74bc713d.tar.bz2 |
first pass at migrating to dataclasses
In some cases, init variables that accept None as a sentinel and
immediately overwrite with [], are migrated to dataclass field
factories. \o/
Note: dataclasses by default cannot provide eq methods, as they then
become unhashable. In the future we may wish to opt into declaring them
frozen, instead/additionally.
Diffstat (limited to 'mesonbuild/mparser.py')
-rw-r--r-- | mesonbuild/mparser.py | 35 |
1 files changed, 21 insertions, 14 deletions
diff --git a/mesonbuild/mparser.py b/mesonbuild/mparser.py index c470e29..735766e 100644 --- a/mesonbuild/mparser.py +++ b/mesonbuild/mparser.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +from dataclasses import dataclass import re import codecs import textwrap @@ -87,15 +88,15 @@ class BlockParseException(MesonException): TV_TokenTypes = T.TypeVar('TV_TokenTypes', int, str, bool) +@dataclass(eq=False) class Token(T.Generic[TV_TokenTypes]): - def __init__(self, tid: str, filename: str, line_start: int, lineno: int, colno: int, bytespan: T.Tuple[int, int], value: TV_TokenTypes): - self.tid = tid # type: str - self.filename = filename # type: str - self.line_start = line_start # type: int - self.lineno = lineno # type: int - self.colno = colno # type: int - self.bytespan = bytespan # type: T.Tuple[int, int] - self.value = value # type: TV_TokenTypes + tid: str + filename: str + line_start: int + lineno: int + colno: int + bytespan: T.Tuple[int, int] + value: TV_TokenTypes def __eq__(self, other: object) -> bool: if isinstance(other, str): @@ -237,13 +238,19 @@ class Lexer: if not matched: raise ParseException('lexer', self.getline(line_start), lineno, col) +@dataclass(eq=False) class BaseNode: - def __init__(self, lineno: int, colno: int, filename: str, end_lineno: T.Optional[int] = None, end_colno: T.Optional[int] = None): - self.lineno = lineno # type: int - self.colno = colno # type: int - self.filename = filename # type: str - self.end_lineno = end_lineno if end_lineno is not None else self.lineno - self.end_colno = end_colno if end_colno is not None else self.colno + lineno: int + colno: int + filename: str + end_lineno: T.Optional[int] = None + end_colno: T.Optional[int] = None + + def __post_init__(self) -> None: + if self.end_lineno is None: + self.end_lineno = self.lineno + if self.end_colno is None: + self.end_colno = self.colno # Attributes for the visitors self.level = 0 # type: int |