Skip to content

Commit

Permalink
Replace black and isort with ruff
Browse files Browse the repository at this point in the history
Closes #277
  • Loading branch information
bcb committed Jul 28, 2024
1 parent 9a1fe9f commit efbf105
Show file tree
Hide file tree
Showing 41 changed files with 103 additions and 50 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/code-quality.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ jobs:
python-version: 3.8
- run: pip install --upgrade pip
- run: pip install types-setuptools "black<23" ruff "mypy<2" "jsonschema<5" pytest "returns<1" "aiohttp<4" "aiozmq<1" "django<5" "fastapi<1" "flask<3" "flask-socketio<5.3.1" "pyzmq" "sanic" "tornado<7" "uvicorn<1" "websockets<11"
- run: black --diff --check $(git ls-files -- '*.py' ':!:docs/*')
- run: ruff $(git ls-files -- '*.py' ':!:docs/*')
- run: ruff check --select I $(git ls-files -- '*.py' ':!:docs/*')
- run: ruff format --check $(git ls-files -- '*.py' ':!:docs/*')
- run: mypy --strict $(git ls-files -- '*.py' ':!:docs/*')
16 changes: 8 additions & 8 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ default_language_version:
exclude: (^docs)
fail_fast: true
repos:
- repo: https://github.com/ambv/black
rev: 22.8.0
hooks:
- id: black
args: [--diff, --check]

- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.0.265
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.4.1
hooks:
- id: ruff
name: lint with ruff
- id: ruff
name: sort imports with ruff
args: [--select, I, --fix]
- id: ruff-format
name: format with ruff

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.2.0
Expand Down
3 changes: 2 additions & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# This file only contains a selection of the most common options. For a full
# list see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html
from typing import List

# -- Path setup --------------------------------------------------------------

Expand Down Expand Up @@ -38,7 +39,7 @@
# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns = []
exclude_patterns: List[str] = []


# -- Options for HTML output -------------------------------------------------
Expand Down
4 changes: 3 additions & 1 deletion examples/aiohttp_server.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""AioHTTP server"""

from aiohttp import web
from jsonrpcserver import async_dispatch, async_method, Ok, Result

from jsonrpcserver import Ok, Result, async_dispatch, async_method


@async_method
Expand Down
4 changes: 3 additions & 1 deletion examples/aiozmq_server.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
"""AioZMQ server"""

import asyncio

import aiozmq # type: ignore
import zmq
from jsonrpcserver import async_dispatch, async_method, Ok, Result

from jsonrpcserver import Ok, Result, async_dispatch, async_method


@async_method
Expand Down
3 changes: 2 additions & 1 deletion examples/asyncio_server.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"""Demonstrates processing a batch of 100 requests asynchronously with asyncio."""

import asyncio
import json

from jsonrpcserver import async_dispatch, async_method, Ok, Result
from jsonrpcserver import Ok, Result, async_dispatch, async_method


@async_method
Expand Down
4 changes: 3 additions & 1 deletion examples/django_server.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
"""Django server"""

from django.http import HttpRequest, HttpResponse # type: ignore
from django.views.decorators.csrf import csrf_exempt # type: ignore
from jsonrpcserver import dispatch, method, Ok, Result

from jsonrpcserver import Ok, Result, dispatch, method


@method
Expand Down
6 changes: 4 additions & 2 deletions examples/fastapi_server.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
"""FastAPI server"""
from fastapi import FastAPI, Request, Response

import uvicorn
from jsonrpcserver import dispatch, method, Ok, Result
from fastapi import FastAPI, Request, Response

from jsonrpcserver import Ok, Result, dispatch, method

app = FastAPI()

Expand Down
4 changes: 3 additions & 1 deletion examples/flask_server.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""Flask server"""

from flask import Flask, Response, request
from jsonrpcserver import dispatch, method, Ok, Result

from jsonrpcserver import Ok, Result, dispatch, method

app = Flask(__name__)

Expand Down
3 changes: 2 additions & 1 deletion examples/http_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
Demonstrates using Python's builtin http.server module to serve JSON-RPC.
"""

from http.server import BaseHTTPRequestHandler, HTTPServer

from jsonrpcserver import dispatch, method, Ok, Result
from jsonrpcserver import Ok, Result, dispatch, method


@method
Expand Down
3 changes: 2 additions & 1 deletion examples/jsonrpcserver_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
Uses jsonrpcserver's built-in "serve" function.
"""
from jsonrpcserver import method, serve, Ok, Result

from jsonrpcserver import Ok, Result, method, serve


@method
Expand Down
4 changes: 3 additions & 1 deletion examples/sanic_server.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
"""Sanic server"""

from sanic import Sanic
from sanic.request import Request
from sanic.response import HTTPResponse, json
from jsonrpcserver import dispatch_to_serializable, method, Ok, Result

from jsonrpcserver import Ok, Result, dispatch_to_serializable, method

app = Sanic("JSON-RPC app")

Expand Down
4 changes: 3 additions & 1 deletion examples/socketio_server.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
"""SocketIO server"""

from flask import Flask, Request
from flask_socketio import SocketIO, send # type: ignore
from jsonrpcserver import dispatch, method, Ok, Result

from jsonrpcserver import Ok, Result, dispatch, method

app = Flask(__name__)
socketio = SocketIO(app)
Expand Down
4 changes: 3 additions & 1 deletion examples/tornado_server.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
"""Tornado server"""

from typing import Awaitable, Optional

from tornado import ioloop, web
from jsonrpcserver import async_dispatch, async_method, Ok, Result

from jsonrpcserver import Ok, Result, async_dispatch, async_method


@async_method
Expand Down
4 changes: 3 additions & 1 deletion examples/websockets_server.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
"""Websockets server"""

import asyncio

from websockets.server import WebSocketServerProtocol, serve
from jsonrpcserver import async_dispatch, async_method, Ok, Result

from jsonrpcserver import Ok, Result, async_dispatch, async_method


@async_method
Expand Down
4 changes: 3 additions & 1 deletion examples/werkzeug_server.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
"""Werkzeug server"""

from werkzeug.serving import run_simple
from werkzeug.wrappers import Request, Response
from jsonrpcserver import method, Result, Ok, dispatch

from jsonrpcserver import Ok, Result, dispatch, method


@method
Expand Down
4 changes: 3 additions & 1 deletion examples/zeromq_server.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""ZeroMQ server"""

import zmq
from jsonrpcserver import dispatch, method, Ok, Result

from jsonrpcserver import Ok, Result, dispatch, method

socket = zmq.Context().socket(zmq.REP)

Expand Down
5 changes: 5 additions & 0 deletions jsonrpcserver/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
"""Jsonrpcserver"""

from returns.result import Result as R

from .async_main import (
dispatch as async_dispatch,
)
from .async_main import (
dispatch_to_response as async_dispatch_to_response,
)
from .async_main import (
dispatch_to_serializable as async_dispatch_to_serializable,
)
from .async_methods import method as async_method
Expand Down
9 changes: 5 additions & 4 deletions jsonrpcserver/async_dispatcher.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
"""Async version of dispatcher.py"""

import asyncio
import logging
from functools import partial
from inspect import signature
from itertools import starmap
from typing import Any, Callable, Iterable, Tuple, Union
import asyncio
import logging

from returns.result import Failure, Result, Success

from .async_methods import Method, Methods
from .dispatcher import (
Deserialized,
create_request,
Expand All @@ -21,16 +23,15 @@
validate_result,
)
from .exceptions import JsonRpcError
from .async_methods import Method, Methods
from .request import Request
from .response import Response, ServerErrorResponse
from .result import (
ErrorResult,
InternalErrorResult,
InvalidParamsResult,
MethodNotFoundResult,
SuccessResult,
)
from .response import Response, ServerErrorResponse
from .utils import make_list

logger = logging.getLogger(__name__)
Expand Down
4 changes: 2 additions & 2 deletions jsonrpcserver/async_main.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
"""Async version of main.py. The public async functions."""

import json
from typing import Any, Callable, Dict, Iterable, List, Optional, Union, cast

from .async_dispatcher import dispatch_to_response_pure
from .async_methods import Methods, global_methods
from .dispatcher import Deserialized
from .main import default_jsonrpc_validator, default_deserializer
from .main import default_deserializer, default_jsonrpc_validator
from .response import Response, to_serializable
from .sentinels import NOCONTEXT
from .utils import identity


# pylint: disable=missing-function-docstring,duplicate-code


Expand Down
1 change: 1 addition & 0 deletions jsonrpcserver/async_methods.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Async methods"""

from typing import Any, Awaitable, Callable, Dict, Optional, cast

from returns.result import Result
Expand Down
7 changes: 4 additions & 3 deletions jsonrpcserver/dispatcher.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
"""Dispatcher - does the hard work of this library: parses, validates and dispatches
requests, providing responses.
"""

# pylint: disable=protected-access
import logging
from functools import partial
from inspect import signature
from itertools import starmap
from typing import Any, Callable, Dict, Iterable, List, Tuple, Union
import logging

from returns.result import Result, Failure, Success
from returns.result import Failure, Result, Success

from .exceptions import JsonRpcError
from .methods import Method, Methods
from .request import Request
from .response import (
Response,
ErrorResponse,
InvalidRequestResponse,
ParseErrorResponse,
Response,
ServerErrorResponse,
SuccessResponse,
)
Expand Down
2 changes: 2 additions & 0 deletions jsonrpcserver/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Exceptions"""

from typing import Any

from .sentinels import NODATA


Expand Down
4 changes: 2 additions & 2 deletions jsonrpcserver/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
- dispatch_to_json/dispatch: Returns a JSON-RPC response string (or an empty string for
notifications).
"""

import json
from importlib.resources import read_text
from typing import Any, Callable, Dict, List, Union, cast
import json

from jsonschema.validators import validator_for # type: ignore

Expand All @@ -26,7 +27,6 @@
from .sentinels import NOCONTEXT
from .utils import identity


default_args_validator = validate_args
default_deserializer = json.loads

Expand Down
1 change: 1 addition & 0 deletions jsonrpcserver/methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
Methods can take either positional or named arguments, but not both. This is a
limitation of JSON-RPC.
"""

from typing import Any, Callable, Dict, Optional, cast

from returns.result import Result
Expand Down
1 change: 1 addition & 0 deletions jsonrpcserver/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
After parsing a request string, we put the (dict) requests into these Request
namedtuples, simply because they're nicer to work with.
"""

from typing import Any, Dict, List, NamedTuple, Union


Expand Down
5 changes: 3 additions & 2 deletions jsonrpcserver/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
https://www.jsonrpc.org/specification#response_object
"""

from typing import Any, Dict, List, NamedTuple, Union

from returns.result import Result, Failure
from returns.result import Failure, Result

from .codes import (
ERROR_INVALID_REQUEST,
Expand Down Expand Up @@ -101,7 +102,7 @@ def to_dict(response: Response) -> Dict[str, Any]:


def to_serializable(
response: Union[Response, List[Response], None]
response: Union[Response, List[Response], None],
) -> Union[Deserialized, None]:
"""Serialize a response object (or list of them), to a dict, or list of them."""
if response is None:
Expand Down
6 changes: 4 additions & 2 deletions jsonrpcserver/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
The public functions are Success, Error and InvalidParams.
"""

from typing import Any, NamedTuple

from returns.result import Failure, Result as R, Success
from returns.result import Failure, Success
from returns.result import Result as R

from .codes import ERROR_INVALID_PARAMS, ERROR_METHOD_NOT_FOUND, ERROR_INTERNAL_ERROR
from .codes import ERROR_INTERNAL_ERROR, ERROR_INVALID_PARAMS, ERROR_METHOD_NOT_FOUND
from .sentinels import NODATA

# pylint: disable=missing-class-docstring,missing-function-docstring,invalid-name
Expand Down
Loading

0 comments on commit efbf105

Please sign in to comment.