plugin: add session typing information#4802
Conversation
64ba278 to
7ae0796
Compare
7ae0796 to
88756ff
Compare
- Add typing information to `Plugin.session` - Implement `http_session` stub file due to the `HTTPSession` subclass of `requests.Session` which adds additional keywords to the `request()` method, including all other HTTP-verb methods - Add `flake8-pyi` and `typing_extensions` to dev-requirements.txt (`typing_extensions` is not a runtime dependency)
88756ff to
289434b
Compare
|
I've changed the return type of the Once this PR is merged, I will open another one with typing information added to the various stream implementations. Adding this here unnecessarily bloats up the diff. |
|
Thanks @bastimeyer. Bummer about the |
Plugin.sessionhttp_sessionstub file due to theHTTPSessionsubclassof
requests.Sessionwhich adds additional keywords to therequest()method, including all other HTTP-verb methodsflake8-pyiandtyping_extensionsto dev-requirements.txt(
typing_extensionsis not a runtime dependency)With the removal of the
Plugin.bind()classmethod in #4768, now that theStreamlinksession instance gets set on eachPlugininstance, typing information can finally be added, as thesessionattribute doesn't have to be defined asNoneanymore initially.Adding typing informations finally allows plugin implementors to see all the stuff that's defined on the
Session, including theHTTPSessionviaself.session.http. However, since Streamlink subclasses requests'sSessionand adds additional keywords to therequest()method and thus also to all HTTP-verb methods likeget(),post(), etc., this introduces typing issues. The most common problem by far is of course the addition of theschemakeyword, which also changes the return type of the various methods fromResponsetoAny.I originally tried to avoid adding a stub file and directly adding the missing HTTP-verb methods to the
HTTPSessionsubclass and adding typing information there, but that got very messy. The repetitive typing information therefore needs to be defined in a stub file. See PEP561.Unfortunately though, it doesn't seem to be possible to import already defined type aliases from the
types-requestspackage, so I had to copy all required definitions for the other args+keywords provided by requests.The
typing_extensionsdependency had to be added because mypy is configured for python 3.7, andTypeAliaswasn't part of thetypingmodule yet in 3.7. Otherwise aerror: Module "typing" has no attribute "TypeAlias"would be raised.The new
Y037linting error also has to be ignored because of an apparent mypy bug (error: Type application has too many types (1 expected)) when union types likea | b | care defined instead ofUnion[a,b,c]. This is all a bit weird, because the copied type alias defintions are using both styles. Maybe it's because of the re-defintions... The union types syntax in stub files is valid on Python 3.7, btw:https://typing.readthedocs.io/en/latest/source/stubs.html#syntax
Opening this as a draft for now.