diff --git a/Makefile b/Makefile index e79645c80..87af2cf9e 100644 --- a/Makefile +++ b/Makefile @@ -10,14 +10,14 @@ install: ## Install dependencies .PHONY: lint lint: ## Run code linters - ruff format --check ninja tests - ruff ninja tests + ruff format --preview --check ninja tests + ruff --preview ninja tests mypy ninja .PHONY: fmt format fmt format: ## Run code formatters - ruff format ninja tests - ruff ninja tests --fix + ruff format --preview ninja tests + ruff --preview --fix ninja tests .PHONY: test test: ## Run tests diff --git a/ninja/openapi/docs.py b/ninja/openapi/docs.py index 596885d7a..6e730bd70 100644 --- a/ninja/openapi/docs.py +++ b/ninja/openapi/docs.py @@ -1,6 +1,7 @@ import json import os from abc import ABC, abstractmethod +from pathlib import Path from typing import TYPE_CHECKING, Any, Optional from django.conf import settings @@ -97,8 +98,7 @@ def _render_cdn_template( "this is helper to find and render html template when ninja is not in INSTALLED_APPS" from django.template import RequestContext, Template - with open(template_path) as f: - tpl = Template(f.read()) + tpl = Template(Path(template_path).read_text()) html = tpl.render(RequestContext(request, context)) return HttpResponse(html) diff --git a/ninja/params/models.py b/ninja/params/models.py index 2f02f5ccd..0f36e6e11 100644 --- a/ninja/params/models.py +++ b/ninja/params/models.py @@ -179,7 +179,7 @@ def get_request_data( for name, annotation in cls.__ninja_body_params__.items(): if name in request.POST: data = request.POST[name] - if annotation == str and data[0] != '"' and data[-1] != '"': + if annotation is str and data[0] != '"' and data[-1] != '"': data = f'"{data}"' req.body = data.encode() results[name] = get_request_data(req, api, path_params) diff --git a/ninja/schema.py b/ninja/schema.py index c6b3538a0..056722afa 100644 --- a/ninja/schema.py +++ b/ninja/schema.py @@ -17,6 +17,7 @@ def resolve_name(obj): return f"{obj.first_name} {obj.last_name}" """ + import warnings from typing import Any, Callable, Dict, Type, TypeVar, Union, no_type_check diff --git a/pyproject.toml b/pyproject.toml index 0bc87505c..cdde2fb64 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -70,13 +70,14 @@ dev = ["pre-commit"] [tool.ruff] target-version = "py37" select = [ - "E", # pycodestyle errors - "W", # pycodestyle warnings - "F", # pyflakes - "I", # isort - "C", # flake8-comprehensions - "B", # flake8-bugbear - "UP", # pyupgrade + "E", # pycodestyle errors + "W", # pycodestyle warnings + "F", # pyflakes + "I", # isort + "C", # flake8-comprehensions + "B", # flake8-bugbear + "UP", # pyupgrade + "FURB", # refurb ] ignore = [ "E501", # line too long, handled by black diff --git a/tests/demo_project/manage.py b/tests/demo_project/manage.py index 8121d888a..70e7cc2a5 100755 --- a/tests/demo_project/manage.py +++ b/tests/demo_project/manage.py @@ -1,5 +1,6 @@ #!/usr/bin/env python """Django's command-line utility for administrative tasks.""" + import os import sys diff --git a/tests/demo_project/multi_param/manage.py b/tests/demo_project/multi_param/manage.py index 07bb6a528..ff4410fe7 100644 --- a/tests/demo_project/multi_param/manage.py +++ b/tests/demo_project/multi_param/manage.py @@ -1,5 +1,6 @@ #!/usr/bin/env python """Django's command-line utility for administrative tasks.""" + import os import sys diff --git a/tests/test_app.py b/tests/test_app.py index 59331b46b..0d817dae4 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -1,3 +1,4 @@ +import contextlib import os from tempfile import NamedTemporaryFile @@ -68,30 +69,26 @@ def file_response(request): f.write(b"this is a file") return FileResponse(open(tmp.name, "rb")) finally: - try: + with contextlib.suppress(PermissionError): os.remove(tmp.name) - except PermissionError: - pass -# fmt: off @pytest.mark.parametrize( "method,path,expected_status,expected_data,expected_streaming", [ - ("get", "/", 200, "/", False), - ("get", "/get", 200, "this is GET", False), - ("post", "/post", 200, "this is POST", False), - ("put", "/put", 200, "this is PUT", False), - ("patch", "/patch", 200, "this is PATCH", False), + ("get", "/", 200, "/", False), + ("get", "/get", 200, "this is GET", False), + ("post", "/post", 200, "this is POST", False), + ("put", "/put", 200, "this is PUT", False), + ("patch", "/patch", 200, "this is PATCH", False), ("delete", "/delete", 200, "this is DELETE", False), - ("get", "/multi", 200, "this is GET", False), - ("post", "/multi", 200, "this is POST", False), - ("patch", "/multi", 405, b"Method not allowed", False), - ("get", "/html", 200, b"html", False), - ("get", "/file", 200, b"this is a file", True), + ("get", "/multi", 200, "this is GET", False), + ("post", "/multi", 200, "this is POST", False), + ("patch", "/multi", 405, b"Method not allowed", False), + ("get", "/html", 200, b"html", False), + ("get", "/file", 200, b"this is a file", True), ], ) -# fmt: on def test_method(method, path, expected_status, expected_data, expected_streaming): func = getattr(client, method) response = func(path) diff --git a/tests/test_export_openapi_schema.py b/tests/test_export_openapi_schema.py index 023382699..fbc209b34 100644 --- a/tests/test_export_openapi_schema.py +++ b/tests/test_export_openapi_schema.py @@ -2,6 +2,7 @@ import os import tempfile from io import StringIO +from pathlib import Path from unittest.mock import patch import pytest @@ -28,8 +29,7 @@ def test_export_to_file(): with tempfile.TemporaryDirectory() as tmp: output_file = os.path.join(tmp, "result.json") call_command(ExportCmd(), output=output_file) - with open(output_file) as f: - json.loads(f.read()) + json.loads(Path(output_file).read_text()) def test_export_custom(): diff --git a/tests/test_pagination.py b/tests/test_pagination.py index 4325e2dea..c132528fc 100644 --- a/tests/test_pagination.py +++ b/tests/test_pagination.py @@ -83,7 +83,7 @@ def paginate_queryset(self, items, pagination: Input, request, **params): prev_skip = 0 return { "items": items[skip : skip + 5], - "next": request.build_absolute_uri(f"?skip={skip+5}"), + "next": request.build_absolute_uri(f"?skip={skip + 5}"), "prev": request.build_absolute_uri(f"?skip={prev_skip}"), } diff --git a/tests/test_path.py b/tests/test_path.py index ffad18c81..2b325f585 100644 --- a/tests/test_path.py +++ b/tests/test_path.py @@ -305,7 +305,7 @@ def test_get_path(path, expected_status, expected_response): ], ) def test_get_path_django(path, expected_status, expected_response): - if expected_response == Exception: + if expected_response is Exception: with pytest.raises(Exception, match=expected_status): client.get(path) else: