AnyType, need to fix nested function type hinting

This commit is contained in:
Jesse Bannon 2023-11-14 23:49:14 -08:00
parent 9b7ba5cb3e
commit c4f9bf4fa0
9 changed files with 48 additions and 38 deletions

View file

@ -1,30 +1,29 @@
from ytdl_sub.script.types.resolvable import Boolean from ytdl_sub.script.types.resolvable import Boolean, AnyType
from ytdl_sub.script.types.resolvable import Resolvable
class BooleanFunctions: class BooleanFunctions:
@staticmethod @staticmethod
def bool(value: Resolvable) -> Boolean: def bool(value: AnyType) -> Boolean:
return Boolean(bool(value.value)) return Boolean(bool(value.value))
@staticmethod @staticmethod
def equals(left: Resolvable, right: Resolvable) -> Boolean: def equals(left: AnyType, right: AnyType) -> Boolean:
return Boolean(left.value == right.value) return Boolean(left.value == right.value)
@staticmethod @staticmethod
def lt(left: Resolvable, right: Resolvable) -> Boolean: def lt(left: AnyType, right: AnyType) -> Boolean:
return Boolean(left.value < right.value) return Boolean(left.value < right.value)
@staticmethod @staticmethod
def lte(left: Resolvable, right: Resolvable) -> Boolean: def lte(left: AnyType, right: AnyType) -> Boolean:
return Boolean(left.value <= right.value) return Boolean(left.value <= right.value)
@staticmethod @staticmethod
def gt(left: Resolvable, right: Resolvable) -> Boolean: def gt(left: AnyType, right: AnyType) -> Boolean:
return Boolean(left.value > right.value) return Boolean(left.value > right.value)
@staticmethod @staticmethod
def gte(left: Resolvable, right: Resolvable) -> Boolean: def gte(left: AnyType, right: AnyType) -> Boolean:
return Boolean(left.value >= right.value) return Boolean(left.value >= right.value)
@staticmethod @staticmethod

View file

@ -6,6 +6,7 @@ from ytdl_sub.script.types.array import Array
from ytdl_sub.script.types.map import Map from ytdl_sub.script.types.map import Map
from ytdl_sub.script.types.resolvable import Hashable from ytdl_sub.script.types.resolvable import Hashable
from ytdl_sub.script.types.resolvable import Resolvable from ytdl_sub.script.types.resolvable import Resolvable
from ytdl_sub.script.types.resolvable import AnyType
from ytdl_sub.script.types.resolvable import String from ytdl_sub.script.types.resolvable import String
from ytdl_sub.utils.exceptions import StringFormattingException from ytdl_sub.utils.exceptions import StringFormattingException
@ -26,7 +27,7 @@ class MapFunctions:
return Map(output) return Map(output)
@staticmethod @staticmethod
def get(mapping: Map, key: Hashable, default: Optional[Resolvable] = None) -> Resolvable: def get(mapping: Map, key: Hashable, default: Optional[AnyType] = None) -> AnyType:
if key not in mapping.value: if key not in mapping.value:
if default is not None: if default is not None:
return default return default

View file

@ -1,7 +1,7 @@
from ytdl_sub.script.types.resolvable import Float from ytdl_sub.script.types.resolvable import Float
from ytdl_sub.script.types.resolvable import Integer from ytdl_sub.script.types.resolvable import Integer
from ytdl_sub.script.types.resolvable import Numeric from ytdl_sub.script.types.resolvable import Numeric
from ytdl_sub.script.types.resolvable import Resolvable from ytdl_sub.script.types.resolvable import AnyType
def _to_numeric(value: int | float) -> Numeric: def _to_numeric(value: int | float) -> Numeric:
@ -12,11 +12,11 @@ def _to_numeric(value: int | float) -> Numeric:
class NumericFunctions: class NumericFunctions:
@staticmethod @staticmethod
def float(value: Resolvable) -> Float: def float(value: AnyType) -> Float:
return Float(value=float(value.value)) return Float(value=float(value.value))
@staticmethod @staticmethod
def int(value: Resolvable) -> Integer: def int(value: AnyType) -> Integer:
return Integer(value=int(value.value)) return Integer(value=int(value.value))
@staticmethod @staticmethod

View file

@ -1,16 +1,15 @@
from typing import Union from typing import Union
from ytdl_sub.script.types.resolvable import Boolean from ytdl_sub.script.types.resolvable import Boolean
from ytdl_sub.script.types.resolvable import Resolvable from ytdl_sub.script.types.resolvable import AnyType_1
from ytdl_sub.script.types.resolvable import Resolvable_1 from ytdl_sub.script.types.resolvable import AnyType_2
from ytdl_sub.script.types.resolvable import Resolvable_2
class SpecialFunctions: class SpecialFunctions:
@staticmethod @staticmethod
def if_( def if_(
condition: Boolean, true: Resolvable, false: Resolvable condition: Boolean, true: AnyType_1, false: AnyType_2
) -> Union[Resolvable_1, Resolvable_2]: ) -> Union[AnyType_1, AnyType_2]:
if condition.value: if condition.value:
return true return true
return false return false

View file

@ -1,13 +1,13 @@
from typing import Optional from typing import Optional
from ytdl_sub.script.types.resolvable import Integer from ytdl_sub.script.types.resolvable import Integer
from ytdl_sub.script.types.resolvable import Resolvable from ytdl_sub.script.types.resolvable import AnyType
from ytdl_sub.script.types.resolvable import String from ytdl_sub.script.types.resolvable import String
class StringFunctions: class StringFunctions:
@staticmethod @staticmethod
def string(value: Resolvable) -> String: def string(value: AnyType) -> String:
return String(value=str(value.value)) return String(value=str(value.value))
@staticmethod @staticmethod

View file

@ -4,7 +4,7 @@ from typing import Dict
from typing import List from typing import List
from typing import Set from typing import Set
from ytdl_sub.script.types.resolvable import ArgumentType from ytdl_sub.script.types.resolvable import ArgumentType, FutureResolvable
from ytdl_sub.script.types.resolvable import NonHashable from ytdl_sub.script.types.resolvable import NonHashable
from ytdl_sub.script.types.resolvable import Resolvable from ytdl_sub.script.types.resolvable import Resolvable
from ytdl_sub.script.types.resolvable import ResolvableToJson from ytdl_sub.script.types.resolvable import ResolvableToJson
@ -19,7 +19,7 @@ class Array(NonHashable):
@dataclass(frozen=True) @dataclass(frozen=True)
class UnresolvedArray(Array, VariableDependency, ArgumentType): class UnresolvedArray(Array, VariableDependency, FutureResolvable):
value: List[ArgumentType] value: List[ArgumentType]
@property @property

View file

@ -16,9 +16,9 @@ from typing import get_origin
from ytdl_sub.script.functions import Functions from ytdl_sub.script.functions import Functions
from ytdl_sub.script.types.resolvable import ArgumentType from ytdl_sub.script.types.resolvable import ArgumentType
from ytdl_sub.script.types.resolvable import Resolvable from ytdl_sub.script.types.resolvable import Resolvable
from ytdl_sub.script.types.resolvable import Resolvable_0 from ytdl_sub.script.types.resolvable import AnyType_0
from ytdl_sub.script.types.resolvable import Resolvable_1 from ytdl_sub.script.types.resolvable import AnyType_1
from ytdl_sub.script.types.resolvable import Resolvable_2 from ytdl_sub.script.types.resolvable import AnyType_2
from ytdl_sub.script.types.variable import FunctionArgument from ytdl_sub.script.types.variable import FunctionArgument
from ytdl_sub.script.types.variable import Variable from ytdl_sub.script.types.variable import Variable
from ytdl_sub.script.types.variable_dependency import VariableDependency from ytdl_sub.script.types.variable_dependency import VariableDependency
@ -245,11 +245,11 @@ class BuiltInFunction(Function):
if is_union(output_type): if is_union(output_type):
union_types_list = [] union_types_list = []
for union_type in output_type.__args__: for union_type in output_type.__args__:
if union_type == Resolvable_0: if union_type == AnyType_0:
union_types_list.append(type(self.args[0])) union_types_list.append(type(self.args[0]))
elif union_type == Resolvable_1: elif union_type == AnyType_1:
union_types_list.append(type(self.args[1])) union_types_list.append(type(self.args[1]))
elif union_type == Resolvable_2: elif union_type == AnyType_2:
union_types_list.append(type(self.args[2])) union_types_list.append(type(self.args[2]))
else: else:
union_types_list.append(union_type) union_types_list.append(union_type)

View file

@ -4,7 +4,7 @@ from typing import Dict
from typing import List from typing import List
from typing import Set from typing import Set
from ytdl_sub.script.types.resolvable import ArgumentType from ytdl_sub.script.types.resolvable import ArgumentType, FutureResolvable
from ytdl_sub.script.types.resolvable import Hashable from ytdl_sub.script.types.resolvable import Hashable
from ytdl_sub.script.types.resolvable import NonHashable from ytdl_sub.script.types.resolvable import NonHashable
from ytdl_sub.script.types.resolvable import Resolvable from ytdl_sub.script.types.resolvable import Resolvable
@ -21,7 +21,7 @@ class Map(NonHashable):
@dataclass(frozen=True) @dataclass(frozen=True)
class UnresolvedMap(Map, VariableDependency, ArgumentType): class UnresolvedMap(Map, VariableDependency, FutureResolvable):
value: Dict[ArgumentType, ArgumentType] value: Dict[ArgumentType, ArgumentType]
@property @property

View file

@ -11,25 +11,36 @@ NumericT = TypeVar("NumericT", bound=int | float)
class ArgumentType(ABC): class ArgumentType(ABC):
"""
Any possible argument type that has not been resolved yet
"""
pass
class AnyType(ArgumentType, ABC):
"""
Human-readable name for FutureResolvable
"""
value: Any
class FutureResolvable(AnyType, ABC):
"""
Type that will be resolved in the future
"""
class AnyType_0(FutureResolvable, ABC):
pass pass
class Resolvable_0(ABC): class AnyType_1(FutureResolvable, ABC):
pass pass
class Resolvable_1(ABC): class AnyType_2(FutureResolvable, ABC):
pass
class Resolvable_2(ABC):
pass pass
@dataclass(frozen=True) @dataclass(frozen=True)
class Resolvable(Resolvable_0, Resolvable_1, Resolvable_2, ABC): class Resolvable(AnyType_0, AnyType_1, AnyType_2, ABC):
value: Any
def __str__(self) -> str: def __str__(self) -> str:
return str(self.value) return str(self.value)