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:
The runner was upgraded to CPython 3.11.9 while the py311 Windows runner is still on 3.11.8.

So this must be an issue with 3.11.9.
https://docs.python.org/release/3.11.9/whatsnew/changelog.html#python-3-11-9

> **gh-116764**: Restore support of None and other false values in [urllib.parse](https://docs.python.org/release/3.11.9/library/urllib.parse.html#module-urllib.parse) functions [parse_qs()](https://docs.python.org/release/3.11.9/library/urllib.parse.html#urllib.parse.parse_qs) and [parse_qsl()](https://docs.python.org/release/3.11.9/library/urllib.parse.html#urllib.parse.parse_qsl). Also, they now raise a TypeError for non-zero integers and non-empty sequences.



Patch:
diff --git a/src/streamlink/plugin/api/validate/_validators.py b/src/streamlink/plugin/api/validate/_validators.py
index 69135b74919..e99d305733f 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 ceff9bc1dde..c328116d27b 100644
--- a/tests/test_api_validate.py
+++ b/tests/test_api_validate.py
@@ -1343,13 +1343,20 @@ def test_success(self):
             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
         """)