chore: typing: PEP 563, PEP 585, PEP 604, PEP 613 #6218
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.
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 annotations
This 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.abc
ordeque
fromcollections
orPattern
fromre
, rather than their deprecated aliases fromtyping
.https://docs.python.org/3/library/typing.html#deprecated-aliases
This replaces
typing.Union
/typing.Optional
with union types and theira | b
syntax.a | None
should always come last, as a suffix rather than prefix or somewhere in-between a chain of union types.TypeAlias
annotations (PEP 613), and always import fromtyping_extensions
inTYPE_CHECKING
mode, asTypeAlias
is a py310 feature which requires this compat import. Use annotation strings, to allow modern syntax via postponed evaluation (PEP 563).Mapping
/dict
).scripts
directory 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.