Skip to content

Commit

Permalink
options: Fix nullability of OptionKey.subproject
Browse files Browse the repository at this point in the history
As a cleanup, this causes the cache to use the same tuple layout that
the `_to_tuple` method does, which makes it easier to ensure everything
is right.
  • Loading branch information
dcbaker committed Mar 3, 2025
1 parent d16d539 commit aa1885f
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions mesonbuild/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
'UserStringOption', 'UserUmaskOption']
ElementaryOptionValues: TypeAlias = T.Union[str, int, bool, T.List[str]]

_OptionKeyTuple: TypeAlias = T.Tuple[T.Optional[str], MachineChoice, str]

class ArgparseKWs(TypedDict, total=False):

action: str
Expand Down Expand Up @@ -102,7 +104,7 @@ class ArgparseKWs(TypedDict, total=False):
}

_BAD_VALUE = 'Qwert Zuiopü'
_optionkey_cache: T.Dict[T.Tuple[str, str, MachineChoice], OptionKey] = {}
_optionkey_cache: T.Dict[_OptionKeyTuple, OptionKey] = {}


class OptionKey:
Expand Down Expand Up @@ -131,7 +133,7 @@ def __new__(cls,
if not name:
return super().__new__(cls) # for unpickling, do not cache now

tuple_ = (name, subproject, machine)
tuple_: _OptionKeyTuple = (subproject, machine, name)
try:
return _optionkey_cache[tuple_]
except KeyError:
Expand Down Expand Up @@ -168,12 +170,12 @@ def __getstate__(self) -> T.Dict[str, T.Any]:
def __setstate__(self, state: T.Dict[str, T.Any]) -> None:
# Here, the object is created using __new__()
self._init(**state)
_optionkey_cache[(self.name, self.subproject, self.machine)] = self
_optionkey_cache[self._to_tuple()] = self

def __hash__(self) -> int:
return self._hash

def _to_tuple(self) -> T.Tuple[str, MachineChoice, str]:
def _to_tuple(self) -> _OptionKeyTuple:
return (self.subproject, self.machine, self.name)

def __eq__(self, other: object) -> bool:
Expand Down

0 comments on commit aa1885f

Please sign in to comment.