Skip to content

Use sequence instead of list #3119

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

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions tornado/routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,18 @@ def request_callable(request):
from tornado.log import app_log
from tornado.util import basestring_type, import_object, re_unescape, unicode_type

from typing import Any, Union, Optional, Awaitable, List, Dict, Pattern, Tuple, overload
from typing import (
Any,
Sequence,
Union,
Optional,
Awaitable,
List,
Dict,
Pattern,
Tuple,
overload,
)


class Router(httputil.HTTPServerConnectionDelegate):
Expand Down Expand Up @@ -286,10 +297,10 @@ def finish(self) -> None:

# _RuleList can either contain pre-constructed Rules or a sequence of
# arguments to be passed to the Rule constructor.
_RuleList = List[
_RuleList = Sequence[
Union[
"Rule",
List[Any], # Can't do detailed typechecking of lists.
Sequence[Any], # Can't do detailed typechecking of lists.
Copy link
Member

Choose a reason for hiding this comment

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

What happens if we only change the outer List to Sequence? I don't think we need covariance on the inner List[Any], and then we wouldn't need to change the (tuple, list) typecheck below. (Logically, this inner sequence is only supposed to be 2-, 3-, or 4-tuples. For historical reasons we also allow lists of the same lengths, but I don't see any reason to extend this to arbitrary sequences).

Tuple[Union[str, "Matcher"], Any],
Tuple[Union[str, "Matcher"], Any, Dict[str, Any]],
Tuple[Union[str, "Matcher"], Any, Dict[str, Any], str],
Expand Down Expand Up @@ -338,7 +349,7 @@ def add_rules(self, rules: _RuleList) -> None:
passed to Rule constructor).
"""
for rule in rules:
if isinstance(rule, (tuple, list)):
if isinstance(rule, (tuple, list, Sequence)):
assert len(rule) in (2, 3, 4)
if isinstance(rule[0], basestring_type):
rule = Rule(PathMatches(rule[0]), *rule[1:])
Expand Down