Skip to content

Conversation

jonathanberthias
Copy link
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.

@@ -211,7 +211,7 @@ def __init__(
path: str,
endpoint: typing.Callable[..., typing.Any],
*,
methods: list[str] | None = None,
methods: typing.Collection[str] | None = None,
Copy link
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
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

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
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
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 encode:master May 29, 2025
7 checks passed
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