Skip to content

Commit ffe8374

Browse files
authored
Replace black and isort with ruff (#278)
Closes #277
1 parent 9a1fe9f commit ffe8374

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+103
-50
lines changed

.github/workflows/code-quality.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ jobs:
1111
python-version: 3.8
1212
- run: pip install --upgrade pip
1313
- 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"
14-
- run: black --diff --check $(git ls-files -- '*.py' ':!:docs/*')
15-
- run: ruff $(git ls-files -- '*.py' ':!:docs/*')
14+
- run: ruff check --select I $(git ls-files -- '*.py' ':!:docs/*')
15+
- run: ruff format --check $(git ls-files -- '*.py' ':!:docs/*')
1616
- run: mypy --strict $(git ls-files -- '*.py' ':!:docs/*')

.pre-commit-config.yaml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ default_language_version:
33
exclude: (^docs)
44
fail_fast: true
55
repos:
6-
- repo: https://github.com/ambv/black
7-
rev: 22.8.0
8-
hooks:
9-
- id: black
10-
args: [--diff, --check]
11-
12-
- repo: https://github.com/charliermarsh/ruff-pre-commit
13-
rev: v0.0.265
6+
- repo: https://github.com/astral-sh/ruff-pre-commit
7+
rev: v0.4.1
148
hooks:
159
- id: ruff
10+
name: lint with ruff
11+
- id: ruff
12+
name: sort imports with ruff
13+
args: [--select, I, --fix]
14+
- id: ruff-format
15+
name: format with ruff
1616

1717
- repo: https://github.com/pre-commit/mirrors-mypy
1818
rev: v1.2.0

docs/conf.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# This file only contains a selection of the most common options. For a full
44
# list see the documentation:
55
# https://www.sphinx-doc.org/en/master/usage/configuration.html
6+
from typing import List
67

78
# -- Path setup --------------------------------------------------------------
89

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

4344

4445
# -- Options for HTML output -------------------------------------------------

examples/aiohttp_server.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"""AioHTTP server"""
2+
23
from aiohttp import web
3-
from jsonrpcserver import async_dispatch, async_method, Ok, Result
4+
5+
from jsonrpcserver import Ok, Result, async_dispatch, async_method
46

57

68
@async_method

examples/aiozmq_server.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
"""AioZMQ server"""
2+
23
import asyncio
34

45
import aiozmq # type: ignore
56
import zmq
6-
from jsonrpcserver import async_dispatch, async_method, Ok, Result
7+
8+
from jsonrpcserver import Ok, Result, async_dispatch, async_method
79

810

911
@async_method

examples/asyncio_server.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
"""Demonstrates processing a batch of 100 requests asynchronously with asyncio."""
2+
23
import asyncio
34
import json
45

5-
from jsonrpcserver import async_dispatch, async_method, Ok, Result
6+
from jsonrpcserver import Ok, Result, async_dispatch, async_method
67

78

89
@async_method

examples/django_server.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
"""Django server"""
2+
23
from django.http import HttpRequest, HttpResponse # type: ignore
34
from django.views.decorators.csrf import csrf_exempt # type: ignore
4-
from jsonrpcserver import dispatch, method, Ok, Result
5+
6+
from jsonrpcserver import Ok, Result, dispatch, method
57

68

79
@method

examples/fastapi_server.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
"""FastAPI server"""
2-
from fastapi import FastAPI, Request, Response
2+
33
import uvicorn
4-
from jsonrpcserver import dispatch, method, Ok, Result
4+
from fastapi import FastAPI, Request, Response
5+
6+
from jsonrpcserver import Ok, Result, dispatch, method
57

68
app = FastAPI()
79

examples/flask_server.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"""Flask server"""
2+
23
from flask import Flask, Response, request
3-
from jsonrpcserver import dispatch, method, Ok, Result
4+
5+
from jsonrpcserver import Ok, Result, dispatch, method
46

57
app = Flask(__name__)
68

examples/http_server.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
33
Demonstrates using Python's builtin http.server module to serve JSON-RPC.
44
"""
5+
56
from http.server import BaseHTTPRequestHandler, HTTPServer
67

7-
from jsonrpcserver import dispatch, method, Ok, Result
8+
from jsonrpcserver import Ok, Result, dispatch, method
89

910

1011
@method

examples/jsonrpcserver_server.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
33
Uses jsonrpcserver's built-in "serve" function.
44
"""
5-
from jsonrpcserver import method, serve, Ok, Result
5+
6+
from jsonrpcserver import Ok, Result, method, serve
67

78

89
@method

examples/sanic_server.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
"""Sanic server"""
2+
23
from sanic import Sanic
34
from sanic.request import Request
45
from sanic.response import HTTPResponse, json
5-
from jsonrpcserver import dispatch_to_serializable, method, Ok, Result
6+
7+
from jsonrpcserver import Ok, Result, dispatch_to_serializable, method
68

79
app = Sanic("JSON-RPC app")
810

examples/socketio_server.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
"""SocketIO server"""
2+
23
from flask import Flask, Request
34
from flask_socketio import SocketIO, send # type: ignore
4-
from jsonrpcserver import dispatch, method, Ok, Result
5+
6+
from jsonrpcserver import Ok, Result, dispatch, method
57

68
app = Flask(__name__)
79
socketio = SocketIO(app)

examples/tornado_server.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
"""Tornado server"""
2+
23
from typing import Awaitable, Optional
34

45
from tornado import ioloop, web
5-
from jsonrpcserver import async_dispatch, async_method, Ok, Result
6+
7+
from jsonrpcserver import Ok, Result, async_dispatch, async_method
68

79

810
@async_method

examples/websockets_server.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
"""Websockets server"""
2+
23
import asyncio
34

45
from websockets.server import WebSocketServerProtocol, serve
5-
from jsonrpcserver import async_dispatch, async_method, Ok, Result
6+
7+
from jsonrpcserver import Ok, Result, async_dispatch, async_method
68

79

810
@async_method

examples/werkzeug_server.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
"""Werkzeug server"""
2+
23
from werkzeug.serving import run_simple
34
from werkzeug.wrappers import Request, Response
4-
from jsonrpcserver import method, Result, Ok, dispatch
5+
6+
from jsonrpcserver import Ok, Result, dispatch, method
57

68

79
@method

examples/zeromq_server.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"""ZeroMQ server"""
2+
23
import zmq
3-
from jsonrpcserver import dispatch, method, Ok, Result
4+
5+
from jsonrpcserver import Ok, Result, dispatch, method
46

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

jsonrpcserver/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
"""Jsonrpcserver"""
2+
23
from returns.result import Result as R
34

45
from .async_main import (
56
dispatch as async_dispatch,
7+
)
8+
from .async_main import (
69
dispatch_to_response as async_dispatch_to_response,
10+
)
11+
from .async_main import (
712
dispatch_to_serializable as async_dispatch_to_serializable,
813
)
914
from .async_methods import method as async_method

jsonrpcserver/async_dispatcher.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
"""Async version of dispatcher.py"""
2+
3+
import asyncio
4+
import logging
25
from functools import partial
36
from inspect import signature
47
from itertools import starmap
58
from typing import Any, Callable, Iterable, Tuple, Union
6-
import asyncio
7-
import logging
89

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

12+
from .async_methods import Method, Methods
1113
from .dispatcher import (
1214
Deserialized,
1315
create_request,
@@ -21,16 +23,15 @@
2123
validate_result,
2224
)
2325
from .exceptions import JsonRpcError
24-
from .async_methods import Method, Methods
2526
from .request import Request
27+
from .response import Response, ServerErrorResponse
2628
from .result import (
2729
ErrorResult,
2830
InternalErrorResult,
2931
InvalidParamsResult,
3032
MethodNotFoundResult,
3133
SuccessResult,
3234
)
33-
from .response import Response, ServerErrorResponse
3435
from .utils import make_list
3536

3637
logger = logging.getLogger(__name__)

jsonrpcserver/async_main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
"""Async version of main.py. The public async functions."""
2+
23
import json
34
from typing import Any, Callable, Dict, Iterable, List, Optional, Union, cast
45

56
from .async_dispatcher import dispatch_to_response_pure
67
from .async_methods import Methods, global_methods
78
from .dispatcher import Deserialized
8-
from .main import default_jsonrpc_validator, default_deserializer
9+
from .main import default_deserializer, default_jsonrpc_validator
910
from .response import Response, to_serializable
1011
from .sentinels import NOCONTEXT
1112
from .utils import identity
1213

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

1616

jsonrpcserver/async_methods.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Async methods"""
2+
23
from typing import Any, Awaitable, Callable, Dict, Optional, cast
34

45
from returns.result import Result

jsonrpcserver/dispatcher.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
"""Dispatcher - does the hard work of this library: parses, validates and dispatches
22
requests, providing responses.
33
"""
4+
45
# pylint: disable=protected-access
6+
import logging
57
from functools import partial
68
from inspect import signature
79
from itertools import starmap
810
from typing import Any, Callable, Dict, Iterable, List, Tuple, Union
9-
import logging
1011

11-
from returns.result import Result, Failure, Success
12+
from returns.result import Failure, Result, Success
1213

1314
from .exceptions import JsonRpcError
1415
from .methods import Method, Methods
1516
from .request import Request
1617
from .response import (
17-
Response,
1818
ErrorResponse,
1919
InvalidRequestResponse,
2020
ParseErrorResponse,
21+
Response,
2122
ServerErrorResponse,
2223
SuccessResponse,
2324
)

jsonrpcserver/exceptions.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""Exceptions"""
2+
23
from typing import Any
4+
35
from .sentinels import NODATA
46

57

jsonrpcserver/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99
- dispatch_to_json/dispatch: Returns a JSON-RPC response string (or an empty string for
1010
notifications).
1111
"""
12+
13+
import json
1214
from importlib.resources import read_text
1315
from typing import Any, Callable, Dict, List, Union, cast
14-
import json
1516

1617
from jsonschema.validators import validator_for # type: ignore
1718

@@ -26,7 +27,6 @@
2627
from .sentinels import NOCONTEXT
2728
from .utils import identity
2829

29-
3030
default_args_validator = validate_args
3131
default_deserializer = json.loads
3232

jsonrpcserver/methods.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
Methods can take either positional or named arguments, but not both. This is a
1212
limitation of JSON-RPC.
1313
"""
14+
1415
from typing import Any, Callable, Dict, Optional, cast
1516

1617
from returns.result import Result

jsonrpcserver/request.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
After parsing a request string, we put the (dict) requests into these Request
44
namedtuples, simply because they're nicer to work with.
55
"""
6+
67
from typing import Any, Dict, List, NamedTuple, Union
78

89

jsonrpcserver/response.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
33
https://www.jsonrpc.org/specification#response_object
44
"""
5+
56
from typing import Any, Dict, List, NamedTuple, Union
67

7-
from returns.result import Result, Failure
8+
from returns.result import Failure, Result
89

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

102103

103104
def to_serializable(
104-
response: Union[Response, List[Response], None]
105+
response: Union[Response, List[Response], None],
105106
) -> Union[Deserialized, None]:
106107
"""Serialize a response object (or list of them), to a dict, or list of them."""
107108
if response is None:

jsonrpcserver/result.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66
77
The public functions are Success, Error and InvalidParams.
88
"""
9+
910
from typing import Any, NamedTuple
1011

11-
from returns.result import Failure, Result as R, Success
12+
from returns.result import Failure, Success
13+
from returns.result import Result as R
1214

13-
from .codes import ERROR_INVALID_PARAMS, ERROR_METHOD_NOT_FOUND, ERROR_INTERNAL_ERROR
15+
from .codes import ERROR_INTERNAL_ERROR, ERROR_INVALID_PARAMS, ERROR_METHOD_NOT_FOUND
1416
from .sentinels import NODATA
1517

1618
# pylint: disable=missing-class-docstring,missing-function-docstring,invalid-name

0 commit comments

Comments
 (0)