From cdf7ca1dcc0271fec283b4fb1089bd8950e01c0a Mon Sep 17 00:00:00 2001 From: Fabian Dill Date: Mon, 10 Apr 2023 23:54:56 +0200 Subject: [PATCH] Core: allow ordered valid keys (#1690) Co-authored-by: el-u <109771707+el-u@users.noreply.github.com> --- Options.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/Options.py b/Options.py index 50c16a89..6fe70ad9 100644 --- a/Options.py +++ b/Options.py @@ -715,8 +715,16 @@ class SpecialRange(Range): f"random-range-high--, or random-range--.") -class VerifyKeys: - valid_keys = frozenset() +class FreezeValidKeys(AssembleOptions): + def __new__(mcs, name, bases, attrs): + if "valid_keys" in attrs: + attrs["_valid_keys"] = frozenset(attrs["valid_keys"]) + return super(FreezeValidKeys, mcs).__new__(mcs, name, bases, attrs) + + +class VerifyKeys(metaclass=FreezeValidKeys): + valid_keys: typing.Iterable = [] + _valid_keys: frozenset # gets created by AssembleOptions from valid_keys valid_keys_casefold: bool = False convert_name_groups: bool = False verify_item_name: bool = False @@ -728,10 +736,10 @@ class VerifyKeys: if cls.valid_keys: data = set(data) dataset = set(word.casefold() for word in data) if cls.valid_keys_casefold else set(data) - extra = dataset - cls.valid_keys + extra = dataset - cls._valid_keys if extra: raise Exception(f"Found unexpected key {', '.join(extra)} in {cls}. " - f"Allowed keys: {cls.valid_keys}.") + f"Allowed keys: {cls._valid_keys}.") def verify(self, world: typing.Type[World], player_name: str, plando_options: "PlandoOptions") -> None: if self.convert_name_groups and self.verify_item_name: @@ -792,6 +800,10 @@ class ItemDict(OptionDict): class OptionList(Option[typing.List[typing.Any]], VerifyKeys): + # Supports duplicate entries and ordering. + # If only unique entries are needed and input order of elements does not matter, OptionSet should be used instead. + # Not a docstring so it doesn't get grabbed by the options system. + default: typing.List[typing.Any] = [] supports_weighting = False