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.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.