[BUGFIX] Fix output sanitization type checking
This commit is contained in:
parent
108c7cfa14
commit
576fdb18d6
2 changed files with 35 additions and 21 deletions
|
|
@ -7,8 +7,11 @@ from typing import Callable, List, Optional, Type, TypeVar, Union, get_origin
|
||||||
|
|
||||||
from ytdl_sub.script.types.resolvable import (
|
from ytdl_sub.script.types.resolvable import (
|
||||||
Argument,
|
Argument,
|
||||||
|
Boolean,
|
||||||
BuiltInFunctionType,
|
BuiltInFunctionType,
|
||||||
|
Float,
|
||||||
FutureResolvable,
|
FutureResolvable,
|
||||||
|
Integer,
|
||||||
Lambda,
|
Lambda,
|
||||||
LambdaReduce,
|
LambdaReduce,
|
||||||
LambdaThree,
|
LambdaThree,
|
||||||
|
|
@ -248,6 +251,17 @@ class FunctionSpec:
|
||||||
return l_type
|
return l_type
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
def has_sanitized_output(self) -> bool:
|
||||||
|
"""
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
If this function were to be sanitized, whether it's redundant or not based on its
|
||||||
|
output
|
||||||
|
"""
|
||||||
|
return inspect.isclass(self.return_type) and issubclass(
|
||||||
|
self.return_type, (Integer, Float, Boolean)
|
||||||
|
)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _to_human_readable_name(cls, python_type: Type[NamedType] | Type[Union[NamedType]]) -> str:
|
def _to_human_readable_name(cls, python_type: Type[NamedType] | Type[Union[NamedType]]) -> str:
|
||||||
if is_optional(python_type):
|
if is_optional(python_type):
|
||||||
|
|
|
||||||
|
|
@ -134,28 +134,28 @@ class ScriptUtils:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
output = ""
|
output = ""
|
||||||
for sub_arg in arg.args:
|
try:
|
||||||
if isinstance(sub_arg, Variable):
|
for sub_arg in arg.args:
|
||||||
# No need to sanitize built-in integer variables
|
if isinstance(sub_arg, Variable):
|
||||||
if isinstance(VARIABLES.get(sub_arg.name), (IntegerVariable, BooleanVariable)):
|
# No need to sanitize built-in integer variables
|
||||||
output += f"{{ {sub_arg.name} }}"
|
if isinstance(VARIABLES.get(sub_arg.name), (IntegerVariable, BooleanVariable)):
|
||||||
|
output += f"{{ {sub_arg.name} }}"
|
||||||
|
else:
|
||||||
|
output += f"{{ {sub_arg.name}_sanitized }}"
|
||||||
|
elif isinstance(sub_arg, (Integer, Float, Boolean)):
|
||||||
|
output += str(sub_arg.native)
|
||||||
|
elif isinstance(sub_arg, String):
|
||||||
|
output += CustomFunctions.sanitize(sub_arg).native
|
||||||
|
elif isinstance(sub_arg, BuiltInFunction) and (
|
||||||
|
sub_arg.function_spec.has_sanitized_output() or sub_arg.name == "pad_zero"
|
||||||
|
):
|
||||||
|
# If we know the function's output is sanitized, let's not wrap it
|
||||||
|
output += cls._to_script_code(sub_arg, top_level=True)
|
||||||
else:
|
else:
|
||||||
output += f"{{ {sub_arg.name}_sanitized }}"
|
# Purposefully do not set top_level to True so we do not recurse
|
||||||
elif isinstance(sub_arg, (Integer, Float, Boolean)):
|
output += f"{{ {cls._to_script_code(BuiltInFunction(name='sanitize', args=[sub_arg]))} }}"
|
||||||
output += str(sub_arg.native)
|
except TypeError:
|
||||||
elif isinstance(sub_arg, String):
|
print("huh")
|
||||||
output += CustomFunctions.sanitize(sub_arg).native
|
|
||||||
elif isinstance(sub_arg, BuiltInFunction) and (
|
|
||||||
issubclass(sub_arg.function_spec.return_type, (Integer, Float, Boolean))
|
|
||||||
or sub_arg.name == "pad_zero"
|
|
||||||
):
|
|
||||||
# If we know the function's output is sanitized, let's not wrap it
|
|
||||||
output += cls._to_script_code(sub_arg, top_level=True)
|
|
||||||
else:
|
|
||||||
# Purposefully do not set top_level to True so we do not recurse
|
|
||||||
output += (
|
|
||||||
f"{{ {cls._to_script_code(BuiltInFunction(name='sanitize', args=[sub_arg]))} }}"
|
|
||||||
)
|
|
||||||
|
|
||||||
return output
|
return output
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue