Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

web: Type SUPPORTED_METHODS so it can be overridden. #3354

Closed
wants to merge 1 commit into from

Conversation

alexmv
Copy link
Contributor

@alexmv alexmv commented Dec 8, 2023

Its default type is Tuple[str, str, str, str, str, str, str], which can only be overridden by a tuple of the exact same length. Which is not terribly useful for its stated purpose.

Type it as a Collection[str], which should support folks overriding it as any number of reasonable things, as it is only used by way of if request.method not in self.SUPPORTED_METHODS.

Its default type is `Tuple[str, str, str, str, str, str, str]`, which
can only be overridden by a tuple of the exact same length.  Which is
not terribly useful for its stated purpose.

Type it as a `Collection[str]`, which should support folks overriding
it as any number of reasonable things, as it is only used by way of
`if request.method not in self.SUPPORTED_METHODS`.
@andersk
Copy link
Contributor

andersk commented Dec 8, 2023

Mypy reveals some implicit reliances on it being a Tuple:

tornado/web.py:1795: error: Item "None" of "Optional[str]" has no attribute "lower"  [union-attr]
tornado/test/web_test.py:2190: error: Unsupported left operand type for + ("Collection[str]")  [operator]
tornado/test/httpclient_test.py:124: error: Unsupported left operand type for + ("Collection[str]")  [operator]

(The first error is a result of Mypy permitting type narrowing of the left operand of in when the right operand is a Tuple but not when it’s a Collection.)

We could remove these reliances, or keep them and use Tuple[str, ...] (the literal ellipsis makes this a variable-length tuple type).

@alexmv
Copy link
Contributor Author

alexmv commented Dec 8, 2023

It ideally should be a set, since it's primarily used for an in query. However, that would be a non-backwards-compatible change, since subclasses in the wild which are doing RequestHandler.SUPPORTED_METHODS + ("OTHER",) would break.

Maintainers, do you have a preference between Tuple[str, ...] or the backwards-incompatible Set[str]?

@bdarnell
Copy link
Member

bdarnell commented Jun 7, 2024

I prefer to keep this as a tuple. Logically it should have been a set, but whether it's a set or a tuple the interface of modifying SUPPORTED_METHODS is awkward and there should be a different way to declare a method to be HTTP-accessible. (perhaps either a decorator, or an overridable check_http_method(name) which defaults to name in self.SUPPORTED_METHODS). I'd rather keep the tuple in place and eventually move to a better solution than to force a backwards-incompatible change to move from a tuple to a set if that's not the long-term solution.

I'll make the change to Tuple[str, ...].

bdarnell added a commit to bdarnell/tornado that referenced this pull request Jun 7, 2024
Its default type is `Tuple[str, str, str, str, str, str, str]`, which
can only be overridden by a tuple of the exact same length.

This change originated in tornadoweb#3354 (thanks alexmv and andersk).

Closes tornadoweb#3354
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.

None yet

3 participants