Skip to content

Commit

Permalink
Core: Fix OptionList and OptionSet to allow Iterable of Iterable (Arc…
Browse files Browse the repository at this point in the history
…hipelagoMW#2911)

* fix, maybe

* typegard for iterable of any

* wow I'm so tired I just changed the method name without changing what it actually does...

* also exclude bytes in is_iterable_but_str

* apply pr comments

* Update Utils.py

Co-authored-by: Doug Hoskisson <[email protected]>

* Revert "also exclude bytes in is_iterable_but_str"

This reverts commit cf087d2.

---------

Co-authored-by: Doug Hoskisson <[email protected]>
  • Loading branch information
2 people authored and EmilyV99 committed Apr 15, 2024
1 parent eb1f10b commit e43be30
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 11 deletions.
8 changes: 4 additions & 4 deletions Options.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

from schema import And, Optional, Or, Schema

from Utils import get_fuzzy_results, is_iterable_of_str
from Utils import get_fuzzy_results, is_iterable_except_str

if typing.TYPE_CHECKING:
from BaseClasses import PlandoOptions
Expand Down Expand Up @@ -847,7 +847,7 @@ class OptionList(Option[typing.List[typing.Any]], VerifyKeys):
default: typing.Union[typing.List[typing.Any], typing.Tuple[typing.Any, ...]] = ()
supports_weighting = False

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

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

@classmethod
def from_any(cls, data: typing.Any):
if is_iterable_of_str(data):
if is_iterable_except_str(data):
cls.verify_keys(data)
return cls(data)
return cls.from_text(str(data))
Expand All @@ -883,7 +883,7 @@ def from_text(cls, text: str):

@classmethod
def from_any(cls, data: typing.Any):
if is_iterable_of_str(data):
if is_iterable_except_str(data):
cls.verify_keys(data)
return cls(data)
return cls.from_text(str(data))
Expand Down
11 changes: 4 additions & 7 deletions Utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,7 @@ def is_kivy_running():
import ctypes
style = 0x10 if error else 0x0
return ctypes.windll.user32.MessageBoxW(0, text, title, style)

# fall back to tk
try:
import tkinter
Expand Down Expand Up @@ -969,11 +969,8 @@ def __len__(self):
return sum(len(iterable) for iterable in self.iterable)


def is_iterable_of_str(obj: object) -> TypeGuard[typing.Iterable[str]]:
""" but not a `str` (because technically, `str` is `Iterable[str]`) """
def is_iterable_except_str(obj: object) -> TypeGuard[typing.Iterable[typing.Any]]:
""" `str` is `Iterable`, but that's not what we want """
if isinstance(obj, str):
return False
if not isinstance(obj, typing.Iterable):
return False
obj_it: typing.Iterable[object] = obj
return all(isinstance(v, str) for v in obj_it)
return isinstance(obj, typing.Iterable)

0 comments on commit e43be30

Please sign in to comment.