chore: typing: PEP 563, PEP 585, PEP 604, PEP 613#6218
Conversation
083e27d to
57031a4
Compare
This comment was marked as outdated.
This comment was marked as outdated.
|
I now understand the issue. The broken dataclasses in the mentioned plugins were not caused by anything special in the dataclasses themselves. I got something wrong after reading a different and unrelated issue with a similar error message on the cpython issue tracker. This issue is caused by Streamlink's plugin module loader. So, as explained, when applying PEP 563, all of the module's type annotations are turned into strings for postponed evaluation. The dataclasses implementation therefore needs to look up the loaded/cached module of that dataclass-object from
The reason for the error is that Streamlink's plugin loader currently doesn't add the module to Simply looking up the cache and only adding new modules doesn't work, because it breaks the sideloading implementation, so this needs to be fixed by adding an While investigating this, I also ran into issues with the |
c7c4a38 to
093d1b3
Compare
093d1b3 to
aa01d4b
Compare
aa01d4b to
e5683f5
Compare
In preparation for the upcoming py38 removal, this updates all typing annotations to modern syntax.
Before I'm going to merge this, I will have to double and triple check everything first, which I haven't done yet. I will have a look at this on the weekend. The changes were all done manually, and I tried to translate everything 1-to-1, but some minor things were also changed and fixed in the process. Not everything though, but this wasn't the goal here. The goal is to not introduce any new typing bugs.
As a requirement, currently, without dropping py38 just yet, PEP 563 is required with its
from __future__ import annotations, to postpone evaluation of annotations. This could've been done a long time ago already, but never was, because it didn't really matter. Now we have one giant commit that updates everything and will need to be added to.git-blame-ignore-revs.There are three commits. Commit two and three don't belong into one, so they had to be split up.
from __future__ import annotationsThis can be removed again (where possible) when dropping py38 at the end of October. Union types (PEP 604) still require it, as it's a py310 feature and requires dropping py39 support.
This replaces generics of the builtin base classes like
type,list,tuple,set,dict, etc., which previously had to be done by imported their respective aliases fromtyping. This also imports stuff likeCallable,Mapping,Iterable, etc. fromcollections.abcordequefromcollectionsorPatternfromre, rather than their deprecated aliases fromtyping.https://docs.python.org/3/library/typing.html#deprecated-aliases
This replaces
typing.Union/typing.Optionalwith union types and theira | bsyntax.a | Noneshould always come last, as a suffix rather than prefix or somewhere in-between a chain of union types.TypeAliasannotations (PEP 613), and always import fromtyping_extensionsinTYPE_CHECKINGmode, asTypeAliasis a py310 feature which requires this compat import. Use annotation strings, to allow modern syntax via postponed evaluation (PEP 563).Mapping/dict).scriptsdirectory to the mypy config and fix typing issues. Both the GH release script and CDP generator script required some further changes which didn't belong into commit one, as well as the config update.Two exceptions which currently can't be updated yet are
streamlink.plugin.plugin._MCollection(list[MType]), because it's a runtime typing requirement and can't be evaluated on py38, andstreamlink.plugin.plugin.Matches(_MCollection[re.Match | None]), which requires py310 due to the union type.