Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable FURB rule on ruff #958

Merged
merged 1 commit into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions ninja/openapi/docs.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion ninja/params/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions ninja/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
15 changes: 8 additions & 7 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/demo_project/manage.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""

import os
import sys

Expand Down
1 change: 1 addition & 0 deletions tests/demo_project/multi_param/manage.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""

import os
import sys

Expand Down
27 changes: 12 additions & 15 deletions tests/test_app.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import contextlib
import os
from tempfile import NamedTemporaryFile

Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_export_openapi_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import tempfile
from io import StringIO
from pathlib import Path
from unittest.mock import patch

import pytest
Expand All @@ -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():
Expand Down
2 changes: 1 addition & 1 deletion tests/test_pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"),
}

Expand Down
2 changes: 1 addition & 1 deletion tests/test_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down