Make annotation for methods more permissive#2903
Merged
Kludex merged 6 commits intoKludex:masterfrom May 29, 2025
Merged
Conversation
starlette/routing.py
Outdated
| endpoint: typing.Callable[..., typing.Any], | ||
| *, | ||
| methods: list[str] | None = None, | ||
| methods: typing.Collection[str] | None = None, |
Contributor
There was a problem hiding this comment.
We typically use typing.Sequence across the project.
Contributor
Author
There was a problem hiding this comment.
Indeed, but in this case it makes sense to allow sets in addition to list and tuples
There was a problem hiding this comment.
Reminder: https://docs.python.org/3/library/typing.html#deprecated-aliases
Shouldn't we start applying new standards?
Contributor
Author
There was a problem hiding this comment.
I followed the current convention of importing everything from the typing module, but if #2867 gets merged before this PR, then I'll adjust 😄
Contributor
Author
There was a problem hiding this comment.
I changed to import from collections.abc
This comment was marked as outdated.
This comment was marked as outdated.
Kludex
approved these changes
May 29, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
The type annotation for
methodsin theRouteconstructor restricts the input to lists of strings when it can accept any iterable. Themethodson theRouteitself are stored as a set.I had the issue when I was trying to build new routes from other preexisting routes:
I changed the annotation to
Collection, though the current implementation would work with anyIterable.Collectionallows lists, sets, tuples, andIterablewould also allow generators. Let me know what you prefer.Checklist