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

Improve type signature for _RuleList #3455

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

mhils
Copy link

@mhils mhils commented Jan 17, 2025

This PR improves the type signature for _RuleList to be more forgiving. Minimal repro:

from tornado.web import Application, RequestHandler

class SampleHandler(RequestHandler):
    def get(self):
        self.write("Hello world")

handlers = [
    (r"/", SampleHandler),
]

application = Application(handlers)

Before Patch:

$ mypy repro.py
repro.py:11: error: Argument 1 to "Application" has incompatible type "list[tuple[str, type[SampleHandler]]]"; expected "list[Rule | list[Any] | tuple[str | Matcher, Any] | tuple[str | Matcher, Any, dict[str, Any]] | tuple[str | Matcher, Any, dict[str, Any], str]] | None"  [arg-type]
repro.py:11: note: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
repro.py:11: note: Consider using "Sequence" instead, which is covariant
Found 1 error in 1 file (checked 1 source file)

After Patch:

$ mypy repro.py
Success: no issues found in 1 source file

Thank you for maintaining tornado, we (@mitmproxy) are super happy users for over a decade! 😃✨🍰

`List` is invariant, `Sequence` is covariant
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.

1 participant