diff --git a/examples/request_stream/client.py b/examples/request_stream/client.py index c6c4f73d..b9565634 100644 --- a/examples/request_stream/client.py +++ b/examples/request_stream/client.py @@ -1,5 +1,6 @@ import requests + # Warning: This is a heavy process. data = "" diff --git a/guide/content/en/migrate.py b/guide/content/en/migrate.py index e0510af4..e58e2c91 100644 --- a/guide/content/en/migrate.py +++ b/guide/content/en/migrate.py @@ -1,4 +1,5 @@ import re + from pathlib import Path from textwrap import indent diff --git a/guide/pyproject.toml b/guide/pyproject.toml new file mode 100644 index 00000000..eb5e4b78 --- /dev/null +++ b/guide/pyproject.toml @@ -0,0 +1,7 @@ +[tool.ruff] +extend = "../pyproject.toml" + +[tool.ruff.isort] +known-first-party = ["webapp"] +lines-after-imports = 1 +lines-between-types = 1 diff --git a/guide/webapp/display/base.py b/guide/webapp/display/base.py index 7486d89a..8624493c 100644 --- a/guide/webapp/display/base.py +++ b/guide/webapp/display/base.py @@ -2,7 +2,6 @@ from __future__ import annotations from html5tagger import Builder, Document # type: ignore - class BaseRenderer: def __init__(self, base_title: str): self.base_title = base_title diff --git a/guide/webapp/display/code_style.py b/guide/webapp/display/code_style.py index 2a7403f9..cf7c2b65 100644 --- a/guide/webapp/display/code_style.py +++ b/guide/webapp/display/code_style.py @@ -7,7 +7,6 @@ from pygments.token import ( # Error,; Generic,; Number,; Operator, Token, ) - class SanicCodeStyle(Style): styles = { Token: "#777", diff --git a/guide/webapp/display/layouts/base.py b/guide/webapp/display/layouts/base.py index 8ffec4f0..fd5c2733 100644 --- a/guide/webapp/display/layouts/base.py +++ b/guide/webapp/display/layouts/base.py @@ -6,7 +6,6 @@ from typing import Generator from html5tagger import Builder from sanic import Request - class BaseLayout: def __init__(self, builder: Builder): self.builder = builder diff --git a/guide/webapp/display/layouts/elements/footer.py b/guide/webapp/display/layouts/elements/footer.py index 4ef2051a..c7a54d42 100644 --- a/guide/webapp/display/layouts/elements/footer.py +++ b/guide/webapp/display/layouts/elements/footer.py @@ -3,7 +3,6 @@ from datetime import datetime from html5tagger import Builder, E # type: ignore from sanic import Request - def do_footer(builder: Builder, request: Request) -> None: builder.footer( _pagination(request), diff --git a/guide/webapp/display/layouts/elements/navbar.py b/guide/webapp/display/layouts/elements/navbar.py index fb26fcdd..b50e728e 100644 --- a/guide/webapp/display/layouts/elements/navbar.py +++ b/guide/webapp/display/layouts/elements/navbar.py @@ -1,8 +1,7 @@ -from webapp.display.layouts.models import MenuItem - from html5tagger import Builder, E # type: ignore from sanic import Request +from webapp.display.layouts.models import MenuItem def do_navbar(builder: Builder, request: Request) -> None: navbar_items = [ diff --git a/guide/webapp/display/layouts/elements/sidebar.py b/guide/webapp/display/layouts/elements/sidebar.py index 530f6237..fac6a737 100644 --- a/guide/webapp/display/layouts/elements/sidebar.py +++ b/guide/webapp/display/layouts/elements/sidebar.py @@ -1,9 +1,8 @@ -from webapp.display.layouts.models import MenuItem -from webapp.display.text import slugify - from html5tagger import Builder, E # type: ignore from sanic import Request +from webapp.display.layouts.models import MenuItem +from webapp.display.text import slugify def do_sidebar(builder: Builder, request: Request) -> None: builder.a(class_="burger")(E.span().span().span().span()) diff --git a/guide/webapp/display/layouts/home.py b/guide/webapp/display/layouts/home.py index 242f4b70..b4c31712 100644 --- a/guide/webapp/display/layouts/home.py +++ b/guide/webapp/display/layouts/home.py @@ -8,7 +8,6 @@ from sanic import Request from .base import BaseLayout - class HomeLayout(BaseLayout): @contextmanager def layout( diff --git a/guide/webapp/display/layouts/main.py b/guide/webapp/display/layouts/main.py index dbbaeac1..870e6424 100644 --- a/guide/webapp/display/layouts/main.py +++ b/guide/webapp/display/layouts/main.py @@ -1,15 +1,14 @@ from contextlib import contextmanager from typing import Generator +from sanic import Request + from webapp.display.layouts.elements.footer import do_footer from webapp.display.layouts.elements.navbar import do_navbar from webapp.display.layouts.elements.sidebar import do_sidebar -from sanic import Request - from .base import BaseLayout - class MainLayout(BaseLayout): @contextmanager def layout( diff --git a/guide/webapp/display/layouts/models.py b/guide/webapp/display/layouts/models.py index d3d8105a..c2f1b616 100644 --- a/guide/webapp/display/layouts/models.py +++ b/guide/webapp/display/layouts/models.py @@ -2,7 +2,6 @@ from __future__ import annotations from msgspec import Struct, field - class MenuItem(Struct, kw_only=False, omit_defaults=True): label: str path: str | None = None diff --git a/guide/webapp/display/markdown.py b/guide/webapp/display/markdown.py index 58d33bf5..614bf9ec 100644 --- a/guide/webapp/display/markdown.py +++ b/guide/webapp/display/markdown.py @@ -1,6 +1,8 @@ import re + from textwrap import dedent +from html5tagger import HTML, Builder, E # type: ignore from mistune import HTMLRenderer, create_markdown, escape from mistune.directives import RSTDirective, TableOfContents from mistune.util import safe_entity @@ -8,8 +10,6 @@ from pygments import highlight from pygments.formatters import html from pygments.lexers import get_lexer_by_name -from html5tagger import HTML, Builder, E # type: ignore - from .code_style import SanicCodeStyle from .plugins.attrs import Attributes from .plugins.columns import Column @@ -20,7 +20,6 @@ from .plugins.span import span from .plugins.tabs import Tabs from .text import slugify - class DocsRenderer(HTMLRenderer): def block_code(self, code: str, info: str | None = None): builder = Builder("Block") diff --git a/guide/webapp/display/page/docobject.py b/guide/webapp/display/page/docobject.py index b812b440..89343c3f 100644 --- a/guide/webapp/display/page/docobject.py +++ b/guide/webapp/display/page/docobject.py @@ -3,6 +3,7 @@ from __future__ import annotations import importlib import inspect import pkgutil + from collections import defaultdict from dataclasses import dataclass, field from html import escape @@ -10,12 +11,10 @@ from html import escape from docstring_parser import Docstring, DocstringParam, DocstringRaises from docstring_parser import parse as parse_docstring from docstring_parser.common import DocstringExample - from html5tagger import HTML, Builder, E # type: ignore from ..markdown import render_markdown, slugify - @dataclass class DocObject: name: str diff --git a/guide/webapp/display/page/renderer.py b/guide/webapp/display/page/renderer.py index 3040ba9f..4fc8f761 100644 --- a/guide/webapp/display/page/renderer.py +++ b/guide/webapp/display/page/renderer.py @@ -3,15 +3,14 @@ from __future__ import annotations from contextlib import contextmanager from typing import Type -from webapp.display.base import BaseRenderer - from html5tagger import HTML, Builder # type: ignore from sanic import Request +from webapp.display.base import BaseRenderer + from ..layouts.base import BaseLayout from .page import Page - class PageRenderer(BaseRenderer): def render(self, request: Request, language: str, path: str) -> Builder: builder = self.get_builder( diff --git a/guide/webapp/display/plugins/attrs.py b/guide/webapp/display/plugins/attrs.py index 7117bd0f..5355f647 100644 --- a/guide/webapp/display/plugins/attrs.py +++ b/guide/webapp/display/plugins/attrs.py @@ -2,13 +2,11 @@ from re import Match from textwrap import dedent from typing import Any +from html5tagger import HTML, E from mistune.block_parser import BlockParser from mistune.core import BlockState from mistune.directives import DirectivePlugin -from html5tagger import HTML, E - - class Attributes(DirectivePlugin): def __call__(self, directive, md): directive.register("attrs", self.parse) diff --git a/guide/webapp/display/plugins/columns.py b/guide/webapp/display/plugins/columns.py index 4ab7feab..e0ba1cca 100644 --- a/guide/webapp/display/plugins/columns.py +++ b/guide/webapp/display/plugins/columns.py @@ -8,7 +8,6 @@ from mistune.core import BlockState from mistune.directives import DirectivePlugin, RSTDirective from mistune.markdown import Markdown - class Column(DirectivePlugin): def parse( self, block: BlockParser, m: Match, state: BlockState diff --git a/guide/webapp/display/plugins/hook.py b/guide/webapp/display/plugins/hook.py index db1b647a..6e9773bc 100644 --- a/guide/webapp/display/plugins/hook.py +++ b/guide/webapp/display/plugins/hook.py @@ -2,7 +2,6 @@ from mistune.core import BlockState from mistune.directives import DirectivePlugin, RSTDirective from mistune.markdown import Markdown - class Hook(DirectivePlugin): def __call__( # type: ignore self, directive: RSTDirective, md: Markdown diff --git a/guide/webapp/display/plugins/mermaid.py b/guide/webapp/display/plugins/mermaid.py index 83fa9189..2eb4ab61 100644 --- a/guide/webapp/display/plugins/mermaid.py +++ b/guide/webapp/display/plugins/mermaid.py @@ -3,15 +3,13 @@ from re import Match from textwrap import dedent from typing import Any +from html5tagger import HTML, E from mistune import HTMLRenderer from mistune.block_parser import BlockParser from mistune.core import BlockState from mistune.directives import DirectivePlugin, RSTDirective from mistune.markdown import Markdown -from html5tagger import HTML, E - - class Mermaid(DirectivePlugin): def parse( self, block: BlockParser, m: Match, state: BlockState diff --git a/guide/webapp/display/plugins/notification.py b/guide/webapp/display/plugins/notification.py index 911b4c31..f264212c 100644 --- a/guide/webapp/display/plugins/notification.py +++ b/guide/webapp/display/plugins/notification.py @@ -1,7 +1,5 @@ -from mistune.directives import Admonition - from html5tagger import HTML, E - +from mistune.directives import Admonition class Notification(Admonition): SUPPORTED_NAMES = { diff --git a/guide/webapp/display/plugins/span.py b/guide/webapp/display/plugins/span.py index 0ea5a9cd..500e73f6 100644 --- a/guide/webapp/display/plugins/span.py +++ b/guide/webapp/display/plugins/span.py @@ -2,7 +2,6 @@ import re from mistune.markdown import Markdown - def parse_inline_span(inline, m: re.Match, state): state.append_token( { diff --git a/guide/webapp/display/plugins/tabs.py b/guide/webapp/display/plugins/tabs.py index b99e70db..ca67c074 100644 --- a/guide/webapp/display/plugins/tabs.py +++ b/guide/webapp/display/plugins/tabs.py @@ -8,7 +8,6 @@ from mistune.core import BlockState from mistune.directives import DirectivePlugin, RSTDirective from mistune.markdown import Markdown - class Tabs(DirectivePlugin): def parse( self, block: BlockParser, m: Match, state: BlockState diff --git a/guide/webapp/display/search/renderer.py b/guide/webapp/display/search/renderer.py index fb06aff1..c04b08f6 100644 --- a/guide/webapp/display/search/renderer.py +++ b/guide/webapp/display/search/renderer.py @@ -1,15 +1,14 @@ from contextlib import contextmanager from urllib.parse import unquote -from webapp.display.search.search import Searcher - from html5tagger import Builder, E # type: ignore from sanic import Request +from webapp.display.search.search import Searcher + from ..base import BaseRenderer from ..layouts.main import MainLayout - class SearchRenderer(BaseRenderer): def render( self, request: Request, language: str, searcher: Searcher, full: bool diff --git a/guide/webapp/display/search/search.py b/guide/webapp/display/search/search.py index cede028e..1d4d1085 100644 --- a/guide/webapp/display/search/search.py +++ b/guide/webapp/display/search/search.py @@ -5,8 +5,8 @@ from pathlib import Path from typing import ClassVar from msgspec import Struct -from webapp.display.page import Page +from webapp.display.page import Page class Stemmer: STOP_WORDS: ClassVar[set[str]] = set( diff --git a/guide/webapp/endpoint/search.py b/guide/webapp/endpoint/search.py index 1b962d45..030ed24d 100644 --- a/guide/webapp/endpoint/search.py +++ b/guide/webapp/endpoint/search.py @@ -1,11 +1,11 @@ # from urllib.parse import unquote +from sanic import Blueprint, Request, Sanic, html + from webapp.display.page import Page from webapp.display.search.renderer import SearchRenderer from webapp.display.search.search import Document, Searcher, Stemmer -from sanic import Blueprint, Request, Sanic, html - bp = Blueprint("search", url_prefix="//search") diff --git a/guide/webapp/worker/config.py b/guide/webapp/worker/config.py index b6166094..fea7b0cd 100644 --- a/guide/webapp/worker/config.py +++ b/guide/webapp/worker/config.py @@ -1,8 +1,8 @@ from pathlib import Path from msgspec import yaml -from webapp.display.layouts.models import GeneralConfig, MenuItem +from webapp.display.layouts.models import GeneralConfig, MenuItem def load_menu(path: Path) -> list[MenuItem]: loaded = yaml.decode(path.read_bytes(), type=dict[str, list[MenuItem]]) diff --git a/guide/webapp/worker/factory.py b/guide/webapp/worker/factory.py index dc983f09..9703b04e 100644 --- a/guide/webapp/worker/factory.py +++ b/guide/webapp/worker/factory.py @@ -1,5 +1,7 @@ from pathlib import Path +from sanic import Request, Sanic, html, redirect + from webapp.display.layouts.models import MenuItem from webapp.display.page import Page, PageRenderer from webapp.endpoint.view import bp @@ -7,9 +9,6 @@ from webapp.worker.config import load_config, load_menu from webapp.worker.reload import setup_livereload from webapp.worker.style import setup_style -from sanic import Request, Sanic, html, redirect - - def _compile_sidebar_order(items: list[MenuItem]) -> list[str]: order = [] for item in items: diff --git a/guide/webapp/worker/reload.py b/guide/webapp/worker/reload.py index 3269a12d..2188151b 100644 --- a/guide/webapp/worker/reload.py +++ b/guide/webapp/worker/reload.py @@ -8,7 +8,6 @@ import ujson from sanic import Request, Sanic, Websocket - def setup_livereload(app: Sanic) -> None: @app.main_process_start async def main_process_start(app: Sanic): diff --git a/guide/webapp/worker/style.py b/guide/webapp/worker/style.py index 5b1f3fff..042b2413 100644 --- a/guide/webapp/worker/style.py +++ b/guide/webapp/worker/style.py @@ -1,11 +1,10 @@ # from scss.compiler import compile_string from pygments.formatters import html -from sass import compile as compile_scss -from webapp.display.code_style import SanicCodeStyle - from sanic import Sanic +from sass import compile as compile_scss +from webapp.display.code_style import SanicCodeStyle def setup_style(app: Sanic) -> None: index = app.config.STYLE_DIR / "index.scss" diff --git a/pyproject.toml b/pyproject.toml index 41a036d6..3bdbb374 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,6 +4,13 @@ build-backend = "setuptools.build_meta" [tool.ruff] line-length = 79 +extend-select = ["I"] + +[tool.ruff.isort] +known-first-party = ["sanic"] +known-third-party = ["pytest"] +lines-after-imports = 2 +lines-between-types = 1 [tool.black] line-length = 79 diff --git a/sanic/worker/state.py b/sanic/worker/state.py index c233c19a..1155745e 100644 --- a/sanic/worker/state.py +++ b/sanic/worker/state.py @@ -1,7 +1,6 @@ from collections.abc import Mapping -from typing import Any, Dict, ItemsView, Iterator, KeysView, List +from typing import Any, Dict, ItemsView, Iterator, KeysView, List, ValuesView from typing import Mapping as MappingType -from typing import ValuesView dict diff --git a/scripts/changelog.py b/scripts/changelog.py index 8848c426..091fb38f 100755 --- a/scripts/changelog.py +++ b/scripts/changelog.py @@ -1,12 +1,14 @@ #!/usr/bin/env python -from os import path import sys +from os import path + + if __name__ == "__main__": try: - import towncrier import click + import towncrier except ImportError: print( "Please make sure you have a installed towncrier and click before using this tool" diff --git a/scripts/release.py b/scripts/release.py index e2b9b887..b9996912 100755 --- a/scripts/release.py +++ b/scripts/release.py @@ -1,18 +1,21 @@ #!/usr/bin/env python +import sys + from argparse import ArgumentParser, Namespace from collections import OrderedDict from configparser import RawConfigParser from datetime import datetime from json import dumps -from os import path, chdir -from subprocess import Popen, PIPE +from os import chdir, path +from subprocess import PIPE, Popen -from jinja2 import Environment, BaseLoader -from requests import patch -import sys import towncrier +from jinja2 import BaseLoader, Environment +from requests import patch + + GIT_COMMANDS = { "get_tag": ["git describe --tags --abbrev=0"], "commit_version_change": [ diff --git a/tests/performance/sanic/http_response.py b/tests/performance/sanic/http_response.py index 2e32b620..72525cb9 100644 --- a/tests/performance/sanic/http_response.py +++ b/tests/performance/sanic/http_response.py @@ -3,7 +3,6 @@ import os import sys import timeit - from sanic.response import json diff --git a/tests/test_cancellederror.py b/tests/test_cancellederror.py index 552747ff..32d689a2 100644 --- a/tests/test_cancellederror.py +++ b/tests/test_cancellederror.py @@ -1,6 +1,5 @@ from asyncio import CancelledError - from sanic import Request, Sanic, json diff --git a/tests/test_ext_integration.py b/tests/test_ext_integration.py index abec0f59..4cd1a7b9 100644 --- a/tests/test_ext_integration.py +++ b/tests/test_ext_integration.py @@ -1,6 +1,5 @@ import sys - import pytest from sanic import Sanic