Skip to content

Commit 537bf3e

Browse files
committed
[PATCH] plugin.api.validate: refactor callable (streamlink#4514)
Remove callable check from the base validate singledispatch function and register the abc.Callable type instead. Also fix type validation in the transform schema.
1 parent 8bd9bb6 commit 537bf3e

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

src/streamlink/plugin/api/validate.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
1616
"""
1717

18+
try:
19+
from collections.abc import Callable
20+
except ImportError:
21+
from typing import Callable
1822
from copy import copy, deepcopy
1923
from typing import Any, Match, Tuple, Union
2024

@@ -349,17 +353,20 @@ def parse_qsd(*args, **kwargs):
349353

350354
@singledispatch
351355
def validate(schema, value):
352-
if callable(schema):
353-
if schema(value):
354-
return value
355-
else:
356-
raise ValueError("{0}({1!r}) is not true".format(schema.__name__, value))
357-
358-
if schema == value:
359-
return value
360-
else:
356+
if schema != value:
361357
raise ValueError("{0!r} does not equal {1!r}".format(value, schema))
362358

359+
return value
360+
361+
362+
@validate.register(Callable)
363+
def validate_callable(schema, value):
364+
# type: (Callable)
365+
if not schema(value):
366+
raise ValueError("{0}({1!r}) is not true".format(schema.__name__, value))
367+
368+
return value
369+
363370

364371
@validate.register(any)
365372
def validate_any(schema, value):
@@ -410,7 +417,7 @@ def validate_getitem(schema, value):
410417
@validate.register(transform)
411418
def validate_transform(schema, value):
412419
# type: (transform)
413-
validate(callable, schema.func)
420+
validate(Callable, schema.func)
414421
return schema.func(value, *schema.args, **schema.kwargs)
415422

416423

0 commit comments

Comments
 (0)