From fe095b151c9cba257de6a85773b0580848d62d08 Mon Sep 17 00:00:00 2001 From: Jesse Bannon Date: Mon, 17 Jul 2023 23:22:45 -0700 Subject: [PATCH] simplify types --- src/ytdl_sub/script/functions.py | 6 ++++++ src/ytdl_sub/script/parser.py | 10 ++++------ src/ytdl_sub/script/types.py | 8 +------- tests/unit/script/test_parser.py | 9 ++++----- 4 files changed, 15 insertions(+), 18 deletions(-) diff --git a/src/ytdl_sub/script/functions.py b/src/ytdl_sub/script/functions.py index 7c058a98..c0c080de 100644 --- a/src/ytdl_sub/script/functions.py +++ b/src/ytdl_sub/script/functions.py @@ -4,6 +4,7 @@ from typing import Generic from typing import TypeVar T = TypeVar("T") +NumericT = TypeVar("NumericT", bound=int | float) @dataclass(frozen=True) @@ -14,6 +15,11 @@ class Resolvable(ABC, Generic[T]): return str(self.value) +@dataclass(frozen=True) +class Numeric(Resolvable[NumericT], ABC, Generic[NumericT]): + pass + + @dataclass(frozen=True) class Integer(Resolvable[int]): pass diff --git a/src/ytdl_sub/script/parser.py b/src/ytdl_sub/script/parser.py index 7d13005c..beeb536a 100644 --- a/src/ytdl_sub/script/parser.py +++ b/src/ytdl_sub/script/parser.py @@ -7,8 +7,6 @@ from ytdl_sub.script.functions import Integer from ytdl_sub.script.functions import String from ytdl_sub.script.types import ArgumentType from ytdl_sub.script.types import Function -from ytdl_sub.script.types import LiteralString -from ytdl_sub.script.types import NumericType from ytdl_sub.script.types import SyntaxTree from ytdl_sub.script.types import Variable from ytdl_sub.utils.exceptions import StringFormattingException @@ -21,7 +19,7 @@ class _Parser: def __init__(self, text: str): self._text = text self._pos = 0 - self._ast: List[LiteralString | Variable | Function] = [] + self._ast: List[String | Variable | Function] = [] self._syntax_tree = self._parse() @@ -69,7 +67,7 @@ class _Parser: assert is_valid_source_variable_name(var_name, raise_exception=False) return Variable(var_name) - def _parse_numeric(self) -> NumericType: + def _parse_numeric(self) -> Integer | Float: numeric_string = "" while ch := self._read(increment_pos=False): if not (ch.isnumeric() or ch == "."): @@ -178,7 +176,7 @@ class _Parser: if ch == "{": bracket_counter += 1 if literal_str: - self._ast.append(LiteralString(literal_str)) + self._ast.append(String(value=literal_str)) literal_str = "" # Allow whitespace after bracket opening @@ -204,7 +202,7 @@ class _Parser: raise StringFormattingException("Bracket count mismatch") if literal_str: - self._ast.append(LiteralString(literal_str)) + self._ast.append(String(value=literal_str)) return SyntaxTree(ast=self._ast) diff --git a/src/ytdl_sub/script/types.py b/src/ytdl_sub/script/types.py index 0e031c81..7f3dd466 100644 --- a/src/ytdl_sub/script/types.py +++ b/src/ytdl_sub/script/types.py @@ -18,7 +18,6 @@ class Variable: name: str -NumericType = Union[Integer, Float] ArgumentType = Union[Integer, Float, String, Boolean, Variable, "Function"] @@ -50,14 +49,9 @@ class Function: return variables -@dataclass(frozen=True) -class LiteralString: - value: str - - @dataclass(frozen=True) class SyntaxTree: - ast: List[LiteralString | Variable | Function] + ast: List[String | Variable | Function] @property def variables(self) -> Set[Variable]: diff --git a/tests/unit/script/test_parser.py b/tests/unit/script/test_parser.py index 26b6ec78..12d638da 100644 --- a/tests/unit/script/test_parser.py +++ b/tests/unit/script/test_parser.py @@ -6,7 +6,6 @@ from ytdl_sub.script.functions import Integer from ytdl_sub.script.functions import String from ytdl_sub.script.parser import parse from ytdl_sub.script.types import Function -from ytdl_sub.script.types import LiteralString from ytdl_sub.script.types import SyntaxTree from ytdl_sub.script.types import Variable from ytdl_sub.utils.exceptions import StringFormattingException @@ -15,14 +14,14 @@ from ytdl_sub.utils.exceptions import StringFormattingException class TestParser: def test_simple(self): parsed = parse("hello world") - assert parsed == SyntaxTree([LiteralString(value="hello world")]) + assert parsed == SyntaxTree([String(value="hello world")]) assert parsed.variables == set() def test_single_function_one_arg(self): parsed = parse("hello {%capitalize('hi mom')}") assert parsed == SyntaxTree( [ - LiteralString("hello "), + String("hello "), Function(name="capitalize", args=[String(value="hi mom")]), ] ) @@ -36,7 +35,7 @@ class TestParser: ) assert parsed == SyntaxTree( [ - LiteralString(value=f"hello{s}"), + String(value=f"hello{s}"), Function( name="concat", args=[ @@ -49,7 +48,7 @@ class TestParser: ], ), ] - + ([LiteralString(value=s)] if s else []) + + ([String(value=s)] if s else []) ) assert parsed.variables == {Variable(name="variable_name")}