diff --git a/src/ytdl_sub/script/parser.py b/src/ytdl_sub/script/parser.py index 6ed6a1df..32f4ed19 100644 --- a/src/ytdl_sub/script/parser.py +++ b/src/ytdl_sub/script/parser.py @@ -1,4 +1,3 @@ -from contextlib import contextmanager from enum import Enum from typing import Dict from typing import List @@ -16,9 +15,9 @@ from ytdl_sub.script.types.resolvable import String from ytdl_sub.script.types.syntax_tree import SyntaxTree from ytdl_sub.script.types.variable import FunctionArgument from ytdl_sub.script.types.variable import Variable +from ytdl_sub.script.utils.exceptions import UNREACHABLE from ytdl_sub.script.utils.exceptions import IncompatibleFunctionArguments from ytdl_sub.script.utils.exceptions import InvalidSyntaxException -from ytdl_sub.script.utils.exceptions import UnreachableSyntaxException from ytdl_sub.script.utils.exceptions import UserException from ytdl_sub.script.utils.parser_exception_formatter import ParserExceptionFormatter from ytdl_sub.utils.exceptions import StringFormattingException @@ -35,12 +34,6 @@ class ArgumentParser(Enum): MAP_VALUE = "map value" -UNREACHABLE = UnreachableSyntaxException( - "If you see this error, you have discovered a bug in the script parser!\n" - "Please upload your config/subscription file(s) to and make a GitHub issue at " - "https://github.com/jmbannon/ytdl-sub/issues" -) - BRACKET_NOT_CLOSED = InvalidSyntaxException("Bracket not properly closed") NUMERICS_ONLY_ARGS = InvalidSyntaxException( diff --git a/src/ytdl_sub/script/types/function.py b/src/ytdl_sub/script/types/function.py index b6ca5632..dce5e6ee 100644 --- a/src/ytdl_sub/script/types/function.py +++ b/src/ytdl_sub/script/types/function.py @@ -23,6 +23,7 @@ from ytdl_sub.script.types.resolvable import Resolvable from ytdl_sub.script.types.variable import FunctionArgument from ytdl_sub.script.types.variable import Variable from ytdl_sub.script.types.variable_dependency import VariableDependency +from ytdl_sub.script.utils.exceptions import UNREACHABLE from ytdl_sub.script.utils.exceptions import IncompatibleFunctionArguments from ytdl_sub.utils.exceptions import StringFormattingException @@ -113,10 +114,10 @@ class FunctionInputSpec: def is_compatible(self, input_args: List[ArgumentType]) -> bool: if self.args is not None: return self._is_args_compatible(input_args=input_args) - elif self.varargs is not None: + if self.varargs is not None: return self._is_varargs_compatible(input_args=input_args) - else: - assert False, "should never reach here" + + raise UNREACHABLE # TODO: functions with no args def expected_args_str(self) -> str: def to_human_readable_name(python_type: Type[NamedType] | Type[Union[NamedType]]) -> str: @@ -128,8 +129,9 @@ class FunctionInputSpec: if self.args is not None: return f"({', '.join([to_human_readable_name(type_) for type_ in self.args])})" - elif self.varargs is not None: + if self.varargs is not None: return f"({to_human_readable_name(self.varargs.__name__)}, ...)" + return "()" @classmethod def from_function(cls, func: "BuiltInFunction") -> "FunctionInputSpec": @@ -233,7 +235,7 @@ class BuiltInFunction(Function): else: received_type_names.append(arg.type_name()) - received_args_str = f"({', '.join([name for name in received_type_names])})" + received_args_str = f"({', '.join(name for name in received_type_names)})" return f"Expected {self.input_spec.expected_args_str()}\nReceived {received_args_str}" diff --git a/src/ytdl_sub/script/utils/exceptions.py b/src/ytdl_sub/script/utils/exceptions.py index 75522c01..f62b5382 100644 --- a/src/ytdl_sub/script/utils/exceptions.py +++ b/src/ytdl_sub/script/utils/exceptions.py @@ -17,3 +17,10 @@ class IncompatibleFunctionArguments(UserException): class UnreachableSyntaxException(InvalidSyntaxException): """For use in places where code _should_ never reach, but might from bugs""" + + +UNREACHABLE = UnreachableSyntaxException( + "If you see this error, you have discovered a bug in the script parser!\n" + "Please upload your config/subscription file(s) to and make a GitHub issue at " + "https://github.com/jmbannon/ytdl-sub/issues" +) diff --git a/tests/unit/script/test_parser.py b/tests/unit/script/test_parser.py index 2ee26a9e..d0e1d8e1 100644 --- a/tests/unit/script/test_parser.py +++ b/tests/unit/script/test_parser.py @@ -34,29 +34,6 @@ class TestParser: ] ) - # def test_array(self): - # parsed = parse("hello {['elem1', 'elem2']}") - # parsed_empty = parse("hello {[]}") - # parsed_with_var = parse("hello {['elem1', variable_name]}") - # parsed_extend = parse( - # "hi {%at(%flatten_array(%extend(['elem1', 'elem2'], ['elem3'], [['elem4'], ['elem5', 'elem6']], ['elem7'])), 1)}" - # ) - # parsed_extend.resolve({}) - # assert False - # - # def test_map(self): - # parsed = parse("hello {%map(['elem1', 'elem2'])}") - # parsed_empty = parse("hello {%map()}") - # parsed_with_var = parse("hello {%map([variable_name, 'elem2'])}") - # parsed_extend = parse("hi {%map([variable_name, 'elem2'], ['elem3', variable_name])}") - # parse_raw_map = parse("hello {{'key': 'value'}}") - # parsed_extend.resolve({}) - # assert False - # - # def test_function_argument(self): - # parsed = parse("hello {%map([$1, $2])}") - # assert False - def test_conditional(self): parsed = parse("hello {%if(True, 'hi', 3.4)}") assert parsed == SyntaxTree( diff --git a/tests/unit/script/types/test_bool.py b/tests/unit/script/types/test_bool.py index ee6a3fd6..96d9378b 100644 --- a/tests/unit/script/types/test_bool.py +++ b/tests/unit/script/types/test_bool.py @@ -1,19 +1,10 @@ import re -from typing import Tuple import pytest from ytdl_sub.script.parser import BOOLEAN_ONLY_ARGS -from ytdl_sub.script.parser import NUMERICS_INVALID_CHAR -from ytdl_sub.script.parser import NUMERICS_ONLY_ARGS -from ytdl_sub.script.parser import UNEXPECTED_CHAR_ARGUMENT -from ytdl_sub.script.parser import UNEXPECTED_COMMA_ARGUMENT -from ytdl_sub.script.parser import ArgumentParser from ytdl_sub.script.script import Script -from ytdl_sub.script.types.array import ResolvedArray from ytdl_sub.script.types.resolvable import Boolean -from ytdl_sub.script.types.resolvable import Float -from ytdl_sub.script.types.resolvable import Integer from ytdl_sub.script.types.resolvable import String from ytdl_sub.script.utils.exceptions import InvalidSyntaxException diff --git a/tests/unit/script/types/test_float.py b/tests/unit/script/types/test_float.py index 8d2c97bd..763d7453 100644 --- a/tests/unit/script/types/test_float.py +++ b/tests/unit/script/types/test_float.py @@ -1,18 +1,12 @@ import re -from typing import Tuple import pytest from ytdl_sub.script.parser import NUMERICS_INVALID_CHAR from ytdl_sub.script.parser import NUMERICS_ONLY_ARGS -from ytdl_sub.script.parser import UNEXPECTED_CHAR_ARGUMENT -from ytdl_sub.script.parser import UNEXPECTED_COMMA_ARGUMENT -from ytdl_sub.script.parser import ArgumentParser from ytdl_sub.script.script import Script -from ytdl_sub.script.types.array import ResolvedArray from ytdl_sub.script.types.resolvable import Boolean from ytdl_sub.script.types.resolvable import Float -from ytdl_sub.script.types.resolvable import Integer from ytdl_sub.script.types.resolvable import String from ytdl_sub.script.utils.exceptions import InvalidSyntaxException diff --git a/tests/unit/script/types/test_function.py b/tests/unit/script/types/test_function.py index 1fa41238..e8cd5ed1 100644 --- a/tests/unit/script/types/test_function.py +++ b/tests/unit/script/types/test_function.py @@ -1,22 +1,11 @@ import re -from typing import Tuple import pytest -from ytdl_sub.script.parser import NUMERICS_INVALID_CHAR -from ytdl_sub.script.parser import NUMERICS_ONLY_ARGS -from ytdl_sub.script.parser import STRINGS_ONLY_ARGS -from ytdl_sub.script.parser import UNEXPECTED_CHAR_ARGUMENT -from ytdl_sub.script.parser import UNEXPECTED_COMMA_ARGUMENT -from ytdl_sub.script.parser import ArgumentParser from ytdl_sub.script.script import Script -from ytdl_sub.script.types.array import ResolvedArray from ytdl_sub.script.types.resolvable import Boolean -from ytdl_sub.script.types.resolvable import Float -from ytdl_sub.script.types.resolvable import Integer from ytdl_sub.script.types.resolvable import String from ytdl_sub.script.utils.exceptions import IncompatibleFunctionArguments -from ytdl_sub.script.utils.exceptions import InvalidSyntaxException def _incompatible_arguments_match(expected: str, recieved: str) -> str: diff --git a/tests/unit/script/types/test_integer.py b/tests/unit/script/types/test_integer.py index 02ed5055..e696c0eb 100644 --- a/tests/unit/script/types/test_integer.py +++ b/tests/unit/script/types/test_integer.py @@ -1,17 +1,11 @@ import re -from typing import Tuple import pytest from ytdl_sub.script.parser import NUMERICS_INVALID_CHAR from ytdl_sub.script.parser import NUMERICS_ONLY_ARGS -from ytdl_sub.script.parser import UNEXPECTED_CHAR_ARGUMENT -from ytdl_sub.script.parser import UNEXPECTED_COMMA_ARGUMENT -from ytdl_sub.script.parser import ArgumentParser from ytdl_sub.script.script import Script -from ytdl_sub.script.types.array import ResolvedArray from ytdl_sub.script.types.resolvable import Boolean -from ytdl_sub.script.types.resolvable import Float from ytdl_sub.script.types.resolvable import Integer from ytdl_sub.script.types.resolvable import String from ytdl_sub.script.utils.exceptions import InvalidSyntaxException diff --git a/tests/unit/script/types/test_string.py b/tests/unit/script/types/test_string.py index d0aa4df4..3b37fac9 100644 --- a/tests/unit/script/types/test_string.py +++ b/tests/unit/script/types/test_string.py @@ -1,20 +1,11 @@ import re -from typing import Tuple import pytest -from ytdl_sub.script.parser import NUMERICS_INVALID_CHAR -from ytdl_sub.script.parser import NUMERICS_ONLY_ARGS from ytdl_sub.script.parser import STRINGS_NOT_CLOSED from ytdl_sub.script.parser import STRINGS_ONLY_ARGS -from ytdl_sub.script.parser import UNEXPECTED_CHAR_ARGUMENT -from ytdl_sub.script.parser import UNEXPECTED_COMMA_ARGUMENT -from ytdl_sub.script.parser import ArgumentParser from ytdl_sub.script.script import Script -from ytdl_sub.script.types.array import ResolvedArray from ytdl_sub.script.types.resolvable import Boolean -from ytdl_sub.script.types.resolvable import Float -from ytdl_sub.script.types.resolvable import Integer from ytdl_sub.script.types.resolvable import String from ytdl_sub.script.utils.exceptions import InvalidSyntaxException