plugin: add session typing information #4802
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Plugin.session
http_session
stub file due to theHTTPSession
subclassof
requests.Session
which adds additional keywords to therequest()
method, including all other HTTP-verb methodsflake8-pyi
andtyping_extensions
to dev-requirements.txt(
typing_extensions
is not a runtime dependency)With the removal of the
Plugin.bind()
classmethod in #4768, now that theStreamlink
session instance gets set on eachPlugin
instance, typing information can finally be added, as thesession
attribute doesn't have to be defined asNone
anymore initially.Adding typing informations finally allows plugin implementors to see all the stuff that's defined on the
Session
, including theHTTPSession
viaself.session.http
. However, since Streamlink subclasses requests'sSession
and 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 theschema
keyword, which also changes the return type of the various methods fromResponse
toAny
.I originally tried to avoid adding a stub file and directly adding the missing HTTP-verb methods to the
HTTPSession
subclass 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-requests
package, so I had to copy all required definitions for the other args+keywords provided by requests.The
typing_extensions
dependency had to be added because mypy is configured for python 3.7, andTypeAlias
wasn't part of thetyping
module yet in 3.7. Otherwise aerror: Module "typing" has no attribute "TypeAlias"
would be raised.The new
Y037
linting 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 | c
are 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.