From cb4ac15993db67084ab3b7f0ed8a3edb7a866bba Mon Sep 17 00:00:00 2001 From: Dylan Baker Date: Tue, 17 Dec 2024 11:58:46 -0800 Subject: [PATCH] utils: Use dataclasses for PerMachine classes This allows us to simplify the initializers, as well as remove our custom repr methods. --- mesonbuild/utils/universal.py | 37 +++++++++++++---------------------- 1 file changed, 14 insertions(+), 23 deletions(-) diff --git a/mesonbuild/utils/universal.py b/mesonbuild/utils/universal.py index c5ed23bd2032..538b0bd9f01d 100644 --- a/mesonbuild/utils/universal.py +++ b/mesonbuild/utils/universal.py @@ -502,10 +502,10 @@ def get_prefix(self) -> str: return MACHINE_PREFIXES[self.value] +@dataclasses.dataclass(eq=False, order=False) class PerMachine(T.Generic[_T]): - def __init__(self, build: _T, host: _T) -> None: - self.build = build - self.host = host + build: _T + host: _T def __getitem__(self, machine: MachineChoice) -> _T: return [self.build, self.host][machine.value] @@ -513,7 +513,7 @@ def __getitem__(self, machine: MachineChoice) -> _T: def __setitem__(self, machine: MachineChoice, val: _T) -> None: setattr(self, machine.get_lower_case_name(), val) - def miss_defaulting(self) -> "PerMachineDefaultable[T.Optional[_T]]": + def miss_defaulting(self) -> PerMachineDefaultable[T.Optional[_T]]: """Unset definition duplicated from their previous to None This is the inverse of ''default_missing''. By removing defaulted @@ -531,10 +531,8 @@ def assign(self, build: _T, host: _T) -> None: self.build = build self.host = host - def __repr__(self) -> str: - return f'PerMachine({self.build!r}, {self.host!r})' - +@dataclasses.dataclass(eq=False, order=False) class PerThreeMachine(PerMachine[_T]): """Like `PerMachine` but includes `target` too. @@ -542,9 +540,8 @@ class PerThreeMachine(PerMachine[_T]): need to computer the `target` field so we don't bother overriding the `__getitem__`/`__setitem__` methods. """ - def __init__(self, build: _T, host: _T, target: _T) -> None: - super().__init__(build, host) - self.target = target + + target: _T def miss_defaulting(self) -> "PerThreeMachineDefaultable[T.Optional[_T]]": """Unset definition duplicated from their previous to None @@ -566,15 +563,14 @@ def miss_defaulting(self) -> "PerThreeMachineDefaultable[T.Optional[_T]]": def matches_build_machine(self, machine: MachineChoice) -> bool: return self.build == self[machine] - def __repr__(self) -> str: - return f'PerThreeMachine({self.build!r}, {self.host!r}, {self.target!r})' - +@dataclasses.dataclass(eq=False, order=False) class PerMachineDefaultable(PerMachine[T.Optional[_T]]): """Extends `PerMachine` with the ability to default from `None`s. """ - def __init__(self, build: T.Optional[_T] = None, host: T.Optional[_T] = None) -> None: - super().__init__(build, host) + + build: T.Optional[_T] = None + host: T.Optional[_T] = None def default_missing(self) -> PerMachine[_T]: """Default host to build @@ -585,9 +581,6 @@ def default_missing(self) -> PerMachine[_T]: assert self.build is not None, 'Cannot fill in missing when all fields are empty' return PerMachine(self.build, self.host if self.host is not None else self.build) - def __repr__(self) -> str: - return f'PerMachineDefaultable({self.build!r}, {self.host!r})' - @classmethod def default(cls, is_cross: bool, build: _T, host: _T) -> PerMachine[_T]: """Easy way to get a defaulted value @@ -603,11 +596,12 @@ def default(cls, is_cross: bool, build: _T, host: _T) -> PerMachine[_T]: return m.default_missing() +@dataclasses.dataclass(eq=False, order=False) class PerThreeMachineDefaultable(PerMachineDefaultable[T.Optional[_T]], PerThreeMachine[T.Optional[_T]]): """Extends `PerThreeMachine` with the ability to default from `None`s. """ - def __init__(self) -> None: - PerThreeMachine.__init__(self, None, None, None) + + target: T.Optional[_T] = None def default_missing(self) -> PerThreeMachine[_T]: """Default host to build and target to host. @@ -621,9 +615,6 @@ def default_missing(self) -> PerThreeMachine[_T]: target = self.target if self.target is not None else host return PerThreeMachine(self.build, host, target) - def __repr__(self) -> str: - return f'PerThreeMachineDefaultable({self.build!r}, {self.host!r}, {self.target!r})' - def is_sunos() -> bool: return platform.system().lower() == 'sunos'