Skip to content

Make annotation for methods more permissive - #2903

Merged
Kludex merged 6 commits into
Kludex:masterfrom
jonathanberthias:patch-1
May 29, 2025
Merged

Kludex merged 6 commits into
Kludex:masterfrom
jonathanberthias:patch-1

Conversation

@jonathanberthias

Copy link
Copy Markdown
Contributor

Summary

The type annotation for methods in the Route constructor restricts the input to lists of strings when it can accept any iterable. The methods on the Route itself are stored as a set.

I had the issue when I was trying to build new routes from other preexisting routes:

new_route = Route(
    route.path.replace("/prefix1", "/prefix2"),
    route.endpoint,
    methods=route.methods,  # type error: Argument "methods" to "Route" has incompatible type "set[str] | None"; expected "list[str] | None"
    name=f"new_{route.name}",
)

I changed the annotation to Collection, though the current implementation would work with any Iterable. Collection allows lists, sets, tuples, and Iterable would also allow generators. Let me know what you prefer.

Checklist

  • I understand that this PR may be closed in case there was no previous discussion. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.

Comment thread starlette/routing.py Outdated
endpoint: typing.Callable[..., typing.Any],
*,
methods: list[str] | None = None,
methods: typing.Collection[str] | None = None,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We typically use typing.Sequence across the project.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, but in this case it makes sense to allow sets in addition to list and tuples

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reminder: https://docs.python.org/3/library/typing.html#deprecated-aliases
Shouldn't we start applying new standards?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I followed the current convention of importing everything from the typing module, but if #2867 gets merged before this PR, then I'll adjust 馃槃

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed to import from collections.abc

@jonathanberthias

This comment was marked as outdated.

@Kludex
Kludex merged commit f099743 into Kludex:master May 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants