Skip to content

Commit

Permalink
allow common collection in option custructors
Browse files Browse the repository at this point in the history
  • Loading branch information
Jouramie committed Feb 28, 2024
1 parent 36cee91 commit 04eb846
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions Options.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
from __future__ import annotations

import abc
import logging
from copy import deepcopy
from dataclasses import dataclass
import functools
import logging
import math
import numbers
import random
import typing
from copy import deepcopy
from dataclasses import dataclass

from schema import And, Optional, Or, Schema

Expand Down Expand Up @@ -59,6 +58,7 @@ def __new__(mcs, name, bases, attrs):
def verify(self, *args, **kwargs) -> None:
for f in verifiers:
f(self, *args, **kwargs)

attrs["verify"] = verify
else:
assert verifiers, "class Option is supposed to implement def verify"
Expand Down Expand Up @@ -183,6 +183,7 @@ def get_option_name(cls, value: str) -> str:

class NumericOption(Option[int], numbers.Integral, abc.ABC):
default = 0

# note: some of the `typing.Any`` here is a result of unresolved issue in python standards
# `int` is not a `numbers.Integral` according to the official typestubs
# (even though isinstance(5, numbers.Integral) == True)
Expand Down Expand Up @@ -598,7 +599,7 @@ def verify(self, world: typing.Type[World], player_name: str, plando_options: "P
if isinstance(self.value, int):
return
from BaseClasses import PlandoOptions
if not(PlandoOptions.bosses & plando_options):
if not (PlandoOptions.bosses & plando_options):
# plando is disabled but plando options were given so pull the option and change it to an int
option = self.value.split(";")[-1]
self.value = self.options[option]
Expand Down Expand Up @@ -843,11 +844,11 @@ class OptionList(Option[typing.List[typing.Any]], VerifyKeys):
# 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] = []
default: typing.Union[typing.List[typing.Any], typing.Tuple[typing.Any, ...]] = ()
supports_weighting = False

def __init__(self, value: typing.List[typing.Any]):
self.value = deepcopy(value)
def __init__(self, value: typing.Iterable[str]):
self.value = list(deepcopy(value))
super(OptionList, self).__init__()

@classmethod
Expand All @@ -856,7 +857,7 @@ def from_text(cls, text: str):

@classmethod
def from_any(cls, data: typing.Any):
if type(data) == list:
if isinstance(data, (list, set, frozenset, tuple)):
cls.verify_keys(data)
return cls(data)
return cls.from_text(str(data))
Expand All @@ -882,7 +883,7 @@ def from_text(cls, text: str):

@classmethod
def from_any(cls, data: typing.Any):
if isinstance(data, (list, set, frozenset)):
if isinstance(data, (list, set, frozenset, tuple)):
cls.verify_keys(data)
return cls(data)
return cls.from_text(str(data))
Expand Down Expand Up @@ -932,7 +933,7 @@ def __new__(mcs,
bases: typing.Tuple[type, ...],
attrs: typing.Dict[str, typing.Any]) -> "OptionsMetaProperty":
for attr_type in attrs.values():
assert not isinstance(attr_type, AssembleOptions),\
assert not isinstance(attr_type, AssembleOptions), \
f"Options for {name} should be type hinted on the class, not assigned"
return super().__new__(mcs, name, bases, attrs)

Expand Down

0 comments on commit 04eb846

Please sign in to comment.