[channels] Make URLRouter.routes a Collection
list is invariant, which made this type inconvenient in practice. Each of the routes is either a pattern or another router.
CC @huynguyengl99
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉
I think it makes sense to migrate from list to Collection. If you have time, could you also help check whether other places or functionalities could be updated to use Collection as well?
Good point - this is only iterated over, not indexed, so Collection is enough.
I only found a couple of other places in stubs/channels/ where I was confident in weakening list parameter types to something covariant. One of them could be a Collection, while the other really did need to be a Sequence since it's indexed.
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉
Looks good to me now.
To make it easier for the core developers to track and approve this PR, could you create an issue and link this PR to it, @cjwatson?
And I think you can make the title a bit more generic since the changes are varied.
I only found a couple of other places in
stubs/channels/where I was confident in weakening list parameter types to something covariant. One of them could be aCollection, while the other really did need to be aSequencesince it's indexed.
For reference, we try to cut back on the use of pseudo-protocols like Mapping and Sequence and use protocols (like Collection or Iterable or one of the custom protocols from _typeshed) instead. This particular case would really benefit from having protocol intersections to be able to easily combine the Iterable and SupportsGetItem protocols. But for now the most pragmatic solution is to just use Sequence like you did.