33from typing import Iterable
44from typing import Optional
55from typing import Set
6+ from typing import Type
7+ from typing import TypeVar
68
79from ytdl_sub .entries .entry import Entry
810from ytdl_sub .entries .script .variable_definitions import VARIABLES
2022from ytdl_sub .utils .exceptions import ValidationException
2123from ytdl_sub .utils .script import ScriptUtils
2224from ytdl_sub .utils .scriptable import Scriptable
23- from ytdl_sub .validators .string_formatter_validators import OverridesStringFormatterValidator
2425from ytdl_sub .validators .string_formatter_validators import StringFormatterValidator
2526from ytdl_sub .validators .string_formatter_validators import UnstructuredDictFormatterValidator
2627
28+ ExpectedT = TypeVar ("ExpectedT" )
29+
2730
2831class Overrides (UnstructuredDictFormatterValidator , Scriptable ):
2932 """
@@ -207,7 +210,8 @@ def apply_formatter(
207210 formatter : StringFormatterValidator ,
208211 entry : Optional [Entry ] = None ,
209212 function_overrides : Optional [Dict [str , str ]] = None ,
210- ) -> str :
213+ expected_type : Type [ExpectedT ] = str ,
214+ ) -> ExpectedT :
211215 """
212216 Parameters
213217 ----------
@@ -217,6 +221,8 @@ def apply_formatter(
217221 Optional. Entry to add source variables to the formatter
218222 function_overrides
219223 Optional. Explicit values to override the overrides themselves and source variables
224+ expected_type
225+ The expected type that should return. Defaults to string.
220226
221227 Returns
222228 -------
@@ -227,42 +233,15 @@ def apply_formatter(
227233 StringFormattingException
228234 If the formatter that is trying to be resolved cannot
229235 """
230- return formatter .post_process (
231- str (
232- self ._apply_to_resolvable (
233- formatter = formatter , entry = entry , function_overrides = function_overrides
234- )
235- )
236- )
237-
238- def apply_overrides_formatter_to_native (
239- self ,
240- formatter : OverridesStringFormatterValidator ,
241- function_overrides : Optional [Dict [str , str ]] = None ,
242- ) -> Any :
243- """
244- Parameters
245- ----------
246- formatter
247- Overrides formatter to apply
248- function_overrides
249- Optional. Explicit values to override the overrides themselves and source variables
250-
251- Returns
252- -------
253- The native python form of the resolved variable
254- """
255- return formatter .post_process_native (
236+ out = formatter .post_process (
256237 self ._apply_to_resolvable (
257- formatter = formatter , entry = None , function_overrides = function_overrides
238+ formatter = formatter , entry = entry , function_overrides = function_overrides
258239 ).native
259240 )
260241
261- def evaluate_boolean (
262- self , formatter : StringFormatterValidator , entry : Optional [Entry ] = None
263- ) -> bool :
264- """
265- Apply a formatter, and evaluate it to a boolean
266- """
267- output = self .apply_formatter (formatter = formatter , entry = entry )
268- return ScriptUtils .bool_formatter_output (output )
242+ if not isinstance (out , expected_type ):
243+ raise StringFormattingException (
244+ f"Expected type { expected_type .__name__ } , but received '{ out .__class__ .__name__ } '"
245+ )
246+
247+ return out
0 commit comments