Repository: streamlink/streamlink

Issue:
tests: py311: FAILED tests/test_api_validate.py::TestParseQsdValidator::test_failure
This test failure has started to occur yesterday in the scheduled CI test runners, and it's not just a flaky test:
https://github.com/streamlink/streamlink/actions/runs/8608358511/job/23590572703#step:6:289

> ```
> =================================== FAILURES ===================================
> ______________________ TestParseQsdValidator.test_failure ______________________
> 
> self = <tests.test_api_validate.TestParseQsdValidator object at 0x7f1413e807d0>
> 
>     def test_failure(self):
>         with pytest.raises(ValidationError) as cm:
>             validate.validate(validate.parse_qsd(), 123)
> >       assert_validationerror(cm.value, """
>             ValidationError:
>               Unable to parse query string: 'int' object has no attribute 'decode' (123)
>         """)
> 
> tests/test_api_validate.py:1350: 
> _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
> 
> exception = ValidationError("Unable to parse query string: memoryview: a bytes-like object is required, not 'int' (123)")
> expected = "\n            ValidationError:\n              Unable to parse query string: 'int' object has no attribute 'decode' (123)\n        "
> 
>     def assert_validationerror(exception, expected):
> >       assert str(exception) == dedent(expected).strip("\n")
> E       assert "ValidationEr...t 'int' (123)" == "ValidationEr...decode' (123)"
> E         
> E         Skipping 39 identical leading characters in diff, use -v to show
> E         - y string: 'int' object has no attribute 'decode' (123)
> E         + y string: memoryview: a bytes-like object is required, not 'int' (123)
> 
> tests/test_api_validate.py:15: AssertionError
> ```

No idea yet where this is coming from. I can't produce this locally.

Currently blocks #5930 

Since this looks like an issue with the CI runner env, I'm tagging this as a CI issue for now...

Discussion:

Patch:
diff --git a/src/streamlink/plugin/api/validate/_validators.py b/src/streamlink/plugin/api/validate/_validators.py
index 69135b74..e99d3057 100644
--- a/src/streamlink/plugin/api/validate/_validators.py
+++ b/src/streamlink/plugin/api/validate/_validators.py
@@ -651,4 +651,8 @@ def validator_parse_qsd(*args, **kwargs) -> TransformSchema:
     :raise ValidationError: On parsing error
     """
 
-    return TransformSchema(_parse_qsd, *args, **kwargs, exception=ValidationError, schema=None)
+    def parser(*_args, **_kwargs):
+        validate(AnySchema(str, bytes), _args[0])
+        return _parse_qsd(*_args, **_kwargs, exception=ValidationError, schema=None)
+
+    return TransformSchema(parser, *args, **kwargs)


Test patch:
diff --git a/tests/test_api_validate.py b/tests/test_api_validate.py
index ceff9bc1..c328116d 100644
--- a/tests/test_api_validate.py
+++ b/tests/test_api_validate.py
@@ -1343,13 +1343,20 @@ class TestParseQsdValidator:
             validate.parse_qsd(),
             "foo=bar&foo=baz&qux=quux",
         ) == {"foo": "baz", "qux": "quux"}
+        assert validate.validate(
+            validate.parse_qsd(),
+            b"foo=bar&foo=baz&qux=quux",
+        ) == {b"foo": b"baz", b"qux": b"quux"}
 
     def test_failure(self):
         with pytest.raises(ValidationError) as cm:
             validate.validate(validate.parse_qsd(), 123)
         assert_validationerror(cm.value, """
-            ValidationError:
-              Unable to parse query string: 'int' object has no attribute 'decode' (123)
+            ValidationError(AnySchema):
+              ValidationError(type):
+                Type of 123 should be str, but is int
+              ValidationError(type):
+                Type of 123 should be bytes, but is int
         """)