typeshed icon indicating copy to clipboard operation
typeshed copied to clipboard

[channels] Make URLRouter.routes a Collection

Open cjwatson opened this issue 1 month ago • 9 comments

list is invariant, which made this type inconvenient in practice. Each of the routes is either a pattern or another router.

cjwatson avatar Nov 24 '25 22:11 cjwatson

CC @huynguyengl99

cjwatson avatar Nov 24 '25 22:11 cjwatson

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

github-actions[bot] avatar Nov 24 '25 22:11 github-actions[bot]

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?

huynguyengl99 avatar Nov 25 '25 04:11 huynguyengl99

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.

cjwatson avatar Nov 25 '25 10:11 cjwatson

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

github-actions[bot] avatar Nov 25 '25 10:11 github-actions[bot]

Looks good to me now.

huynguyengl99 avatar Nov 25 '25 17:11 huynguyengl99

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?

huynguyengl99 avatar Nov 25 '25 17:11 huynguyengl99

And I think you can make the title a bit more generic since the changes are varied.

huynguyengl99 avatar Nov 25 '25 17:11 huynguyengl99

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.

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.

srittau avatar Nov 26 '25 13:11 srittau