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

Open
wants to merge 1 commit into
base: master
Choose a base branch
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]?

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

2 participants