<commit_before>import collections
from http import HTTPStatus
from typing import Any, Union, Iterator, Sequence

from werkzeug.wrappers import Request

from rouver.args import ArgumentTemplate, ArgumentDict, parse_args
from rouver.response import \
    respond, respond_with_json, respond_with_html, created_at, see_other
from rouver.types import StartResponse, Header


class RouteHandlerBase(collections.Iterable):

    """Base class for rouver route handlers.

    Sub-classes of RouteHandlerBase can act as route handlers. They provide
    convenient, opaque access to several rouver services.

    Implementations must implement the __iter__() method.

    >>> from rouver.router import Router
    >>> class MyRouteHandler(RouteHandlerBase):
    ...     def __iter__(self):
    ...         return self.respond_with_html("<div>Hello World!</div>")
    >>> class MyRouter(Router):
    ...     def __init__(self):
    ...         super().__init__()
    ...         self.add_routes([
    ...             ("my-route", "GET", MyRouteHandler),
    ...         ])
    """

    def __init__(self, request: Request, path_args: Sequence[Any],
                 start_response: StartResponse) -> None:
        self.request = request
        self.path_args = path_args
        self.start_response = start_response

    def __iter__(self) -> Iterator[bytes]:
        raise NotImplementedError()

    def parse_args(self, argument_template: Sequence[ArgumentTemplate]) \
            -> ArgumentDict:
        return parse_args(self.request.environ, argument_template)

    def respond(self, extra_headers: Sequence[Header] = []) \
            -> Iterator[bytes]:
        return respond(self.start_response, extra_headers=extra_headers)

    def respond_with_json(
            self, json: Union[str, bytes, Any], *,
            status: HTTPStatus = HTTPStatus.OK,
            extra_headers: Sequence[Header] = []) -> Iterator[bytes]:
        return respond_with_json(
            self.start_response, json,
            status=status, extra_headers=extra_headers)

    def respond_with_html(
            self, html: str, *, status: HTTPStatus = HTTPStatus.OK,
            extra_headers: Sequence[Header] = []) \
            -> Iterator[bytes]:
        return respond_with_html(
            self.start_response, html,
            status=status, extra_headers=extra_headers)

    def created_at(self, url_part: str) -> Iterator[bytes]:
        return created_at(self.request, self.start_response, url_part)

    def see_other(self, url_part: str) -> Iterator[bytes]:
        return see_other(self.request, self.start_response, url_part)<commit_msg>[mypy] Fix a type warning<commit_after>import collections
from http import HTTPStatus
from typing import cast, Any, Union, Iterator, Sequence, Dict

from werkzeug.wrappers import Request

from rouver.args import ArgumentTemplate, ArgumentDict, parse_args
from rouver.response import \
    respond, respond_with_json, respond_with_html, created_at, see_other
from rouver.types import StartResponse, Header


class RouteHandlerBase(collections.Iterable):

    """Base class for rouver route handlers.

    Sub-classes of RouteHandlerBase can act as route handlers. They provide
    convenient, opaque access to several rouver services.

    Implementations must implement the __iter__() method.

    >>> from rouver.router import Router
    >>> class MyRouteHandler(RouteHandlerBase):
    ...     def __iter__(self):
    ...         return self.respond_with_html("<div>Hello World!</div>")
    >>> class MyRouter(Router):
    ...     def __init__(self):
    ...         super().__init__()
    ...         self.add_routes([
    ...             ("my-route", "GET", MyRouteHandler),
    ...         ])
    """

    def __init__(self, request: Request, path_args: Sequence[Any],
                 start_response: StartResponse) -> None:
        self.request = request
        self.path_args = path_args
        self.start_response = start_response

    def __iter__(self) -> Iterator[bytes]:
        raise NotImplementedError()

    def parse_args(self, argument_template: Sequence[ArgumentTemplate]) \
            -> ArgumentDict:
        return parse_args(cast(Dict[str, Any], self.request.environ),
                          argument_template)

    def respond(self, extra_headers: Sequence[Header] = []) \
            -> Iterator[bytes]:
        return respond(self.start_response, extra_headers=extra_headers)

    def respond_with_json(
            self, json: Union[str, bytes, Any], *,
            status: HTTPStatus = HTTPStatus.OK,
            extra_headers: Sequence[Header] = []) -> Iterator[bytes]:
        return respond_with_json(
            self.start_response, json,
            status=status, extra_headers=extra_headers)

    def respond_with_html(
            self, html: str, *, status: HTTPStatus = HTTPStatus.OK,
            extra_headers: Sequence[Header] = []) \
            -> Iterator[bytes]:
        return respond_with_html(
            self.start_response, html,
            status=status, extra_headers=extra_headers)

    def created_at(self, url_part: str) -> Iterator[bytes]:
        return created_at(self.request, self.start_response, url_part)

    def see_other(self, url_part: str) -> Iterator[bytes]:
        return see_other(self.request, self.start_response, url_part)
{'fraction_non_alphanumeric': 0.10232383808095952, 'fraction_numerical': 0.0, 'mean_word_length': 2.8203292770221906, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6465416', 'n_tokens_mistral': 1483, 'n_tokens_neox': 1451, 'n_words': 361}