summaryrefslogtreecommitdiff
path: root/mesonbuild/utils
diff options
context:
space:
mode:
authorDylan Baker <dylan@pnwbakers.com>2024-12-17 11:58:46 -0800
committerDylan Baker <dylan@pnwbakers.com>2025-01-31 07:49:26 -0800
commitcb4ac15993db67084ab3b7f0ed8a3edb7a866bba (patch)
treebb6bfbe32be3880c9aea89309564684b1a28f8af /mesonbuild/utils
parent42c4f746321b19c8295d153ca281b9b5249e5fb4 (diff)
downloadmeson-cb4ac15993db67084ab3b7f0ed8a3edb7a866bba.tar.gz
utils: Use dataclasses for PerMachine classes
This allows us to simplify the initializers, as well as remove our custom repr methods.
Diffstat (limited to 'mesonbuild/utils')
-rw-r--r--mesonbuild/utils/universal.py37
1 files changed, 14 insertions, 23 deletions
diff --git a/mesonbuild/utils/universal.py b/mesonbuild/utils/universal.py
index c5ed23bd2..538b0bd9f 100644
--- a/mesonbuild/utils/universal.py
+++ b/mesonbuild/utils/universal.py
@@ -502,10 +502,10 @@ class MachineChoice(enum.IntEnum):
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 @@ class PerMachine(T.Generic[_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 @@ class PerMachine(T.Generic[_T]):
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 @@ class PerThreeMachine(PerMachine[_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 @@ class PerMachineDefaultable(PerMachine[T.Optional[_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 @@ class PerMachineDefaultable(PerMachine[T.Optional[_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 @@ class PerThreeMachineDefaultable(PerMachineDefaultable[T.Optional[_T]], PerThree
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'