Fix type invariance causing too strict typing #115
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.
#111 added a bunch of typing to the codebase. The typing that it added for mutable containers such as lists or dicts were too strict because many type checkers treat these containers as type-invariant, meaning that e.g. a value of type
list[str]orlist[bytes]is not permitted for an arg of typelist[str | bytes]; Only a value of that exact type is.The solution to this is to make use of abstract collections like
MappingandSequencewhich are type covariant and therefore allow the above to work.Note that for
Mapping, only the values are type covariant and the keys are still type invariant and therefore e.g.dict[str | bytes, str | bytes]will need to be modified toMapping[str, str | bytes] | Mapping[bytes, str | bytes]and not justMapping[str | bytes, str | bytes]also modified the
envarg inpystemd.run.run()to copy the dict before mutating it so that theMappingtyping can be used.