Skip to content

Commit

Permalink
Merge pull request #966 from jairhenrique/ruff-pth
Browse files Browse the repository at this point in the history
Enable PTH rule on ruff
  • Loading branch information
vitalik authored Dec 1, 2023
2 parents 667548a + 68cc1b0 commit d09ad93
Show file tree
Hide file tree
Showing 18 changed files with 28 additions and 46 deletions.
3 changes: 2 additions & 1 deletion ninja/management/commands/export_openapi_schema.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
from pathlib import Path
from typing import Any, Optional

from django.core.management.base import BaseCommand, CommandError, CommandParser
Expand Down Expand Up @@ -83,7 +84,7 @@ def handle(self, *args: Any, **options: Any) -> None:
)

if options["output"]:
with open(options["output"], "wb") as f:
with Path(options["output"]).open("wb") as f:
f.write(result.encode())
else:
self.stdout.write(result)
Expand Down
7 changes: 3 additions & 4 deletions ninja/openapi/docs.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import json
import os
from abc import ABC, abstractmethod
from pathlib import Path
from typing import TYPE_CHECKING, Any, Optional
Expand All @@ -16,7 +15,7 @@
# if anyone knows a cleaner way to make mypy happy - welcome
from ninja import NinjaAPI # pragma: no cover

ABS_TPL_PATH = os.path.join(os.path.dirname(__file__), "../templates/")
ABS_TPL_PATH = Path(__file__).parent.parent / "templates/ninja/"


class DocsBase(ABC):
Expand All @@ -32,7 +31,7 @@ def get_openapi_url(self, api: "NinjaAPI", path_params: DictStrAny) -> str:

class Swagger(DocsBase):
template = "ninja/swagger.html"
template_cdn = os.path.join(ABS_TPL_PATH, "ninja/swagger_cdn.html")
template_cdn = str(ABS_TPL_PATH / "swagger_cdn.html")
default_settings = {
"layout": "BaseLayout",
"deepLinking": True,
Expand All @@ -58,7 +57,7 @@ def render_page(

class Redoc(DocsBase):
template = "ninja/redoc.html"
template_cdn = os.path.join(ABS_TPL_PATH, "ninja/redoc_cdn.html")
template_cdn = str(ABS_TPL_PATH / "redoc_cdn.html")
default_settings: DictStrAny = {}

def __init__(self, settings: Optional[DictStrAny] = None):
Expand Down
13 changes: 6 additions & 7 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ test = [
"pytest-django",
"pytest-asyncio",
"psycopg2-binary",
"black",
"mypy==1.7.1",
"ruff==0.1.6",
"django-stubs",
Expand All @@ -70,14 +69,15 @@ dev = ["pre-commit"]
[tool.ruff]
target-version = "py37"
select = [
"B", # flake8-bugbear
"C", # flake8-comprehensions
"E", # pycodestyle errors
"W", # pycodestyle warnings
"F", # pyflakes
"FURB", # refurb
"I", # isort
"C", # flake8-comprehensions
"B", # flake8-bugbear
"PTH", # flake8-use-pathlib
"UP", # pyupgrade
"FURB", # refurb
"W", # pycodestyle warnings
]
ignore = [
"E501", # line too long, handled by black
Expand All @@ -86,10 +86,9 @@ ignore = [
]

[tool.ruff.per-file-ignores]
"__init__.py" = ["F401"]
"ninja/compatibility/datastructures.py" = ["C416"]
"ninja/utils.py" = ["B004"]
"tests/*" = ["C408", "F401"]
"tests/*" = ["C408"]


[tool.coverage.run]
Expand Down
11 changes: 6 additions & 5 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import os
import sys
from pathlib import Path

ROOT = Path(__file__).parent.parent.resolve()

sys.path.insert(0, str(ROOT))
sys.path.insert(0, str(ROOT / "tests/demo_project"))

ROOT = os.path.abspath(os.path.join(os.path.dirname(os.path.dirname(__file__))))
sys.path.insert(0, ROOT)
sys.path.insert(0, os.path.join(ROOT, "tests/demo_project"))
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "demo.settings")

# from django.conf import settings
# settings.configure()
import django # noqa

django.setup()
Expand Down
6 changes: 3 additions & 3 deletions tests/demo_project/demo/settings.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
from pathlib import Path

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
BASE_DIR = Path(__file__).parent.parent


SECRET_KEY = "NOT-SUPER-SECRET-DO-NOT-USE-ME"
Expand Down Expand Up @@ -57,7 +57,7 @@
DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": os.path.join(BASE_DIR, "db.sqlite3"),
"NAME": BASE_DIR / "db.sqlite3",
}
}

Expand Down
2 changes: 0 additions & 2 deletions tests/demo_project/someapp/admin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
from django.contrib import admin

# Register your models here.
2 changes: 0 additions & 2 deletions tests/demo_project/someapp/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
from django.shortcuts import render

# Create your views here.
9 changes: 5 additions & 4 deletions tests/test_app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import contextlib
import os
from pathlib import Path
from tempfile import NamedTemporaryFile

import pytest
Expand Down Expand Up @@ -65,12 +66,12 @@ def html(request):
def file_response(request):
tmp = NamedTemporaryFile(delete=False)
try:
with open(tmp.name, "wb") as f:
f.write(b"this is a file")
return FileResponse(open(tmp.name, "rb"))
p = Path(tmp.name)
p.write_bytes(b"this is a file")
return FileResponse(Path(tmp.name).open("rb"))
finally:
with contextlib.suppress(PermissionError):
os.remove(tmp.name)
Path(tmp.name).unlink()


@pytest.mark.parametrize(
Expand Down
1 change: 0 additions & 1 deletion tests/test_async.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import asyncio

import django
import pytest

from ninja import NinjaAPI
Expand Down
1 change: 0 additions & 1 deletion tests/test_auth_async.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import asyncio

import django
import pytest

from ninja import NinjaAPI
Expand Down
3 changes: 0 additions & 3 deletions tests/test_csrf.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import re
from unittest import mock

import pytest
from django.conf import settings
from django.views.decorators.csrf import csrf_exempt

from ninja import NinjaAPI
from ninja.errors import ConfigError
from ninja.security import APIKeyCookie, APIKeyHeader
from ninja.testing import TestClient as BaseTestClient

Expand Down
5 changes: 0 additions & 5 deletions tests/test_csrf_async.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
from unittest import mock

import django
import pytest
from django.conf import settings

from ninja import NinjaAPI
from ninja.errors import ConfigError
from ninja.security import APIKeyCookie
from ninja.testing import TestAsyncClient as BaseTestAsyncClient


Expand Down
1 change: 0 additions & 1 deletion tests/test_exceptions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import django
import pytest
from django.http import Http404

Expand Down
3 changes: 1 addition & 2 deletions tests/test_export_openapi_schema.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import json
import os
import tempfile
from io import StringIO
from pathlib import Path
Expand Down Expand Up @@ -27,7 +26,7 @@ def test_export_indent():

def test_export_to_file():
with tempfile.TemporaryDirectory() as tmp:
output_file = os.path.join(tmp, "result.json")
output_file = Path(tmp) / "result.json"
call_command(ExportCmd(), output=output_file)
json.loads(Path(output_file).read_text())

Expand Down
1 change: 0 additions & 1 deletion tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import uuid

import pytest
from django.http import Http404
from pydantic import BaseModel

from ninja import NinjaAPI
Expand Down
1 change: 0 additions & 1 deletion tests/test_orm_schemas.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from typing import List
from unittest.mock import Mock

import django
import pytest
from django.contrib.postgres import fields as ps_fields
from django.db import models
Expand Down
1 change: 0 additions & 1 deletion tests/test_query_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from pydantic import Field

from ninja import NinjaAPI, Query, Schema
from ninja.testing import TestClient


class Range(IntEnum):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_with_django/test_multi_param_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ def test_validate_test_data():
if 0: # pragma: nocover
# if test cases or schema generation changes,
# use this block of code to regenerate the fixtures
with open(filename, "w") as f:
with Path(filename).open("w") as f:
json.dump(schema["paths"][path], f, indent=2)
with open(filename) as f:
with Path(filename).open() as f:
data = json.load(f)
assert json.loads(json.dumps(schema["paths"][path])) == data

Expand Down

0 comments on commit d09ad93

Please sign in to comment.