simplify types
This commit is contained in:
parent
b16f6262ec
commit
fe095b151c
4 changed files with 15 additions and 18 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
||||
|
|
|
|||
|
|
@ -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]:
|
||||
|
|
|
|||
|
|
@ -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")}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue