From 7c914ca4255ce3ef01700a760563cbe4add386c6 Mon Sep 17 00:00:00 2001 From: Dylan Baker Date: Wed, 12 Feb 2025 11:17:51 -0800 Subject: [PATCH] options: make the initializer less convenient. It's now intended that the `factory()` method will be used when not using `from_string`, so there's no reason to make it convenient to use the main initializer --- mesonbuild/options.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/mesonbuild/options.py b/mesonbuild/options.py index bc6890d7ed59..b78dfce05cd6 100644 --- a/mesonbuild/options.py +++ b/mesonbuild/options.py @@ -115,8 +115,8 @@ class OptionKey: _hash: int _as_tuple: T.Tuple[str, MachineChoice, str] - def __init__(self, name: str, subproject: str = '', - machine: MachineChoice = MachineChoice.HOST): + def __init__(self, name: str, subproject: str, + machine: MachineChoice, hash_: int): # the _type option to the constructor is kinda private. We want to be # able to save the state and avoid the lookup function when # pickling/unpickling, but we need to be able to calculate it when @@ -124,7 +124,7 @@ def __init__(self, name: str, subproject: str = '', object.__setattr__(self, 'name', name) object.__setattr__(self, 'subproject', subproject) object.__setattr__(self, 'machine', machine) - object.__setattr__(self, '_hash', hash((name, subproject, machine))) + object.__setattr__(self, '_hash', hash_) object.__setattr__(self, '_as_tuple', (self.subproject, self.machine, self.name)) def __setattr__(self, key: str, value: T.Any) -> None: @@ -135,6 +135,7 @@ def __getstate__(self) -> T.Dict[str, T.Any]: 'name': self.name, 'subproject': self.subproject, 'machine': self.machine, + '_hash': self._hash, } def __setstate__(self, state: T.Dict[str, T.Any]) -> None: @@ -202,7 +203,7 @@ def factory(cls, name: str, subproject: str = '', machine: MachineChoice = Machi :param machine: the machine the subproject belongs to, defaults to MachineChoice.HOST :return: An OptionKey """ - return cls(name, subproject, machine) + return cls(name, subproject, machine, hash((name, subproject, machine))) @classmethod @lru_cache(None) @@ -231,7 +232,7 @@ def from_string(cls, raw: str) -> 'OptionKey': assert ':' not in opt assert opt.count('.') < 2 - return cls(opt, subproject, for_machine) + return cls.factory(opt, subproject, for_machine) def evolve(self, name: T.Optional[str] = None, subproject: T.Optional[str] = None, machine: T.Optional[MachineChoice] = None) -> 'OptionKey':