Skip to content

Commit

Permalink
Merge pull request #1 from beauxq/Jouramie-core/allow-more-iterables-…
Browse files Browse the repository at this point in the history
…in-option-parsing

allow any iterable of strings
  • Loading branch information
Jouramie authored Mar 1, 2024
2 parents fc7c91b + 6ae0fda commit 2954a7d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 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
from Utils import get_fuzzy_results, is_iterable_of_str

if typing.TYPE_CHECKING:
from BaseClasses import PlandoOptions
Expand Down Expand Up @@ -766,7 +766,7 @@ class VerifyKeys(metaclass=FreezeValidKeys):
value: typing.Any

@classmethod
def verify_keys(cls, data: typing.List[str]):
def verify_keys(cls, data: typing.Iterable[str]):
if cls.valid_keys:
data = set(data)
dataset = set(word.casefold() for word in data) if cls.valid_keys_casefold else set(data)
Expand Down Expand Up @@ -857,7 +857,7 @@ def from_text(cls, text: str):

@classmethod
def from_any(cls, data: typing.Any):
if isinstance(data, (list, set, frozenset, tuple)):
if is_iterable_of_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 isinstance(data, (list, set, frozenset, tuple)):
if is_iterable_of_str(data):
cls.verify_keys(data)
return cls(data)
return cls.from_text(str(data))
Expand Down
11 changes: 11 additions & 0 deletions Utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from argparse import Namespace
from settings import Settings, get_settings
from typing import BinaryIO, Coroutine, Optional, Set, Dict, Any, Union
from typing_extensions import TypeGuard
from yaml import load, load_all, dump

try:
Expand Down Expand Up @@ -966,3 +967,13 @@ def __bool__(self):

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]`) """
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)
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ certifi>=2023.11.17
cython>=3.0.8
cymem>=2.0.8
orjson>=3.9.10
typing-extensions>=4.7.0

0 comments on commit 2954a7d

Please sign in to comment.