Skip to content

Commit 5e42567

Browse files
committed
make annotation optional for convert_req_function_args
1 parent 7dad583 commit 5e42567

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

src/Rules.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import re
1010
import math
1111
import inspect
12+
import logging
1213

1314
if TYPE_CHECKING:
1415
from . import ManualWorld
@@ -94,8 +95,8 @@ def checkRequireStringForArea(state: CollectionState, area: dict):
9495

9596
if not callable(func):
9697
raise ValueError(f"Invalid function `{func_name}` in {area}.")
97-
if func_args:
98-
convert_req_function_args(func, func_args, area["name"])
98+
99+
convert_req_function_args(func, func_args, area["name"])
99100
result = func(world, multiworld, state, player, *func_args)
100101
if isinstance(result, bool):
101102
requires_list = requires_list.replace("{" + func_name + "(" + item[1] + ")}", "1" if result else "0")
@@ -273,7 +274,7 @@ def allRegionsAccessible(state):
273274
# Victory requirement
274275
multiworld.completion_condition[player] = lambda state: state.has("__Victory__", player)
275276

276-
def convert_req_function_args(func, args: list[str], areaName: str):
277+
def convert_req_function_args(func, args: list[str], areaName: str, warn: bool = False):
277278
parameters = inspect.signature(func).parameters
278279
knownArguments = ["world", "multiworld", "state", "player"]
279280
index = 0
@@ -282,8 +283,12 @@ def convert_req_function_args(func, args: list[str], areaName: str):
282283
continue
283284

284285
argType = info.annotation
286+
287+
if issubclass(argType, inspect._empty): #if not set then it wont get converted but still be checked for valid data at index
288+
argType = str
289+
285290
try:
286-
value = args[index].strip().lower()
291+
value = args[index].strip()
287292

288293
except IndexError:
289294
if info is not inspect.Parameter.empty:
@@ -292,19 +297,22 @@ def convert_req_function_args(func, args: list[str], areaName: str):
292297
else:
293298
raise Exception(f"A call of the {func.__name__} function in '{areaName}'s requirement, asks for a value of type {argType}\nfor its argument '{info.name}' but its missing")
294299

300+
295301
if not isinstance(value, argType):
296302
if issubclass(argType, bool):
297303
#Special conversion to bool
298-
if value in ['true', '1']:
304+
if value.lower() in ['true', '1']:
299305
value = True
300306

301-
elif value in ['false', '0']:
307+
elif value.lower() in ['false', '0']:
302308
value = False
303309

304310
else:
305-
# warning here spam the console, might be worth to make it a data validation instead
306-
# logging.warn(f"A function in '{areaName}'s requirement, asks for a value of type {argType}\nfor its argument '{info.name}' but an unknown string was passed")
307311
value = bool(value)
312+
if warn:
313+
# warning here spam the console if called from rules.py, might be worth to make it a data validation instead
314+
logging.warn(f"A call of the {func.__name__} function in '{areaName}'s requirement, asks for a value of type {argType}\nfor its argument '{info.name}' but an unknown string was passed and thus converted to {value}")
315+
308316

309317
else:
310318
try:

0 commit comments

Comments
 (0)