Skip to content

Commit

Permalink
Setup pre-commit (#53)
Browse files Browse the repository at this point in the history
* Setup and run `pre-commit` on the project

* Update `README.md` with setup instructions
  • Loading branch information
surenkov authored Mar 25, 2024
1 parent 82a1b89 commit c479dfb
Show file tree
Hide file tree
Showing 19 changed files with 80 additions and 30 deletions.
15 changes: 14 additions & 1 deletion .github/workflows/python-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,24 @@ jobs:
python-version: '3.x'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install -e .[dev,test]
- name: Lint package
run: mypy .

pre-commit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.x'
- name: Install dependencies
run: |
python -m pip install -e .[dev]
- name: Run pre-commit
run: pre-commit run --show-diff-on-failure --color=always --all-files

test:
runs-on: ubuntu-latest
strategy:
Expand Down
21 changes: 21 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
default_language_version:
python: python3.12

repos:
- repo: meta
hooks:
- id: check-hooks-apply
- repo: https://github.com/PyCQA/isort
rev: 5.13.0
hooks:
- id: isort
args: [ "--settings-path", "./pyproject.toml", "--filter-files" ]
files: "^django_pydantic_field/"
exclude: ^.*\b(\.pytest_cache|\.venv|venv|tests)\b.*$
- repo: https://github.com/psf/black
rev: 24.3.0
hooks:
- id: black
args: [ "--config", "./pyproject.toml" ]
files: "^django_pydantic_field/"
exclude: ^.*\b(\.pytest_cache|\.venv|venv|tests)\b.*$
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,14 @@ class FooClassBasedView(views.APIView):
return Response([request.data])
```

## Contributing
To get `django-pydantic-field` up and running in development mode:
1. Clone this repo;
1. Create a virtual environment: `python -m venv .venv`;
1. Activate `.venv`: `. .venv/bin/activate`;
1. Install the project and its dependencies: `pip install -e .[dev,test]`;
1. Setup `pre-commit`: `pre-commit install`.

## Acknowledgement

* [Churkin Oleg](https://gist.github.com/Bahus/98a9848b1f8e2dcd986bf9f05dbf9c65) for his Gist as a source of inspiration;
Expand Down
1 change: 1 addition & 0 deletions django_pydantic_field/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from .fields import SchemaField as SchemaField


def __getattr__(name):
if name == "_migration_serializers":
module = __import__("django_pydantic_field._migration_serializers", fromlist=["*"])
Expand Down
1 change: 1 addition & 0 deletions django_pydantic_field/_migration_serializers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import warnings

from .compat.django import *

DEPRECATION_MSG = (
Expand Down
4 changes: 2 additions & 2 deletions django_pydantic_field/compat/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .django import GenericContainer as GenericContainer
from .django import MigrationWriter as MigrationWriter
from .pydantic import PYDANTIC_V1 as PYDANTIC_V1
from .pydantic import PYDANTIC_V2 as PYDANTIC_V2
from .pydantic import PYDANTIC_VERSION as PYDANTIC_VERSION
from .django import GenericContainer as GenericContainer
from .django import MigrationWriter as MigrationWriter
1 change: 1 addition & 0 deletions django_pydantic_field/compat/django.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
Moreover, `types.UnionType`, introduced in 3.10, do not allow explicit type construction,
only with `X | Y` syntax. Both cases require a dedicated serializer for migration writes.
"""

import sys
import types
import typing as ty
Expand Down
2 changes: 1 addition & 1 deletion django_pydantic_field/fields.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .compat.imports import compat_getattr, compat_dir
from .compat.imports import compat_dir, compat_getattr

__getattr__ = compat_getattr(__name__)
__dir__ = compat_dir(__name__)
10 changes: 8 additions & 2 deletions django_pydantic_field/fields.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@ def SchemaField(
**kwargs: te.Unpack[_SchemaFieldKwargs],
) -> ST: ...
@ty.overload
@te.deprecated("Passing `json.dump` kwargs to `SchemaField` is not supported by Pydantic 2 and will be removed in the future versions.")
@te.deprecated(
"Passing `json.dump` kwargs to `SchemaField` is not supported by "
"Pydantic 2 and will be removed in the future versions."
)
def SchemaField(
schema: ty.Type[ST] | None | ty.ForwardRef = ...,
config: ConfigType = ...,
Expand All @@ -105,7 +108,10 @@ def SchemaField(
**kwargs: te.Unpack[_DeprecatedSchemaFieldKwargs],
) -> ST | None: ...
@ty.overload
@te.deprecated("Passing `json.dump` kwargs to `SchemaField` is not supported by Pydantic 2 and will be removed in the future versions.")
@te.deprecated(
"Passing `json.dump` kwargs to `SchemaField` is not supported by "
"Pydantic 2 and will be removed in the future versions."
)
def SchemaField(
schema: ty.Type[ST] | ty.ForwardRef = ...,
config: ConfigType = ...,
Expand Down
2 changes: 1 addition & 1 deletion django_pydantic_field/forms.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .compat.imports import compat_getattr, compat_dir
from .compat.imports import compat_dir, compat_getattr

__getattr__ = compat_getattr(__name__)
__dir__ = compat_dir(__name__)
11 changes: 6 additions & 5 deletions django_pydantic_field/forms.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ from __future__ import annotations

import json
import typing as ty
import typing_extensions as te

import typing_extensions as te
from django.forms.fields import JSONField
from django.forms.widgets import Widget
from django.utils.functional import _StrOrPromise

from .fields import ST, ConfigType, _ExportKwargs
from .fields import _ExportKwargs, ConfigType, ST

__all__ = ("SchemaField",)

Expand Down Expand Up @@ -38,15 +38,13 @@ class _JSONFieldKwargs(_CharFieldKwargs, total=False):
class _SchemaFieldKwargs(_ExportKwargs, _JSONFieldKwargs, total=False):
allow_null: bool | None


class _DeprecatedSchemaFieldKwargs(_SchemaFieldKwargs, total=False):
allow_nan: ty.Any
indent: ty.Any
separators: ty.Any
skipkeys: ty.Any
sort_keys: ty.Any


class SchemaField(JSONField, ty.Generic[ST]):
@ty.overload
def __init__(
Expand All @@ -57,7 +55,10 @@ class SchemaField(JSONField, ty.Generic[ST]):
**kwargs: te.Unpack[_SchemaFieldKwargs],
) -> None: ...
@ty.overload
@te.deprecated("Passing `json.dump` kwargs to `SchemaField` is not supported by Pydantic 2 and will be removed in the future versions.")
@te.deprecated(
"Passing `json.dump` kwargs to `SchemaField` is not supported by "
"Pydantic 2 and will be removed in the future versions."
)
def __init__(
self,
schema: ty.Type[ST] | ty.ForwardRef | str,
Expand Down
2 changes: 1 addition & 1 deletion django_pydantic_field/rest_framework.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .compat.imports import compat_getattr, compat_dir
from .compat.imports import compat_dir, compat_getattr

__getattr__ = compat_getattr(__name__)
__dir__ = compat_dir(__name__)
3 changes: 1 addition & 2 deletions django_pydantic_field/v1/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,13 @@ def prepare_schema(schema: "ModelType", owner: t.Any = None) -> None:


def extract_export_kwargs(ctx: dict, extractor=dict.get) -> t.Dict[str, t.Any]:
""" Extract ``BaseModel.json()`` kwargs from ctx for field deconstruction/reconstruction."""
"""Extract ``BaseModel.json()`` kwargs from ctx for field deconstruction/reconstruction."""

export_ctx = dict(
exclude_defaults=extractor(ctx, "exclude_defaults", None),
exclude_none=extractor(ctx, "exclude_none", None),
exclude_unset=extractor(ctx, "exclude_unset", None),
by_alias=extractor(ctx, "by_alias", None),

# extract json.dumps(...) kwargs, see: https://docs.pydantic.dev/1.10/usage/exporting_models/#modeljson
skipkeys=extractor(ctx, "skipkeys", None),
indent=extractor(ctx, "indent", None),
Expand Down
8 changes: 3 additions & 5 deletions django_pydantic_field/v1/fields.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import contextlib
import json
import typing as t
import contextlib
from functools import partial

import pydantic
Expand Down Expand Up @@ -188,8 +188,7 @@ def SchemaField(
*args,
null: "t.Literal[True]",
**kwargs,
) -> "t.Optional[base.ST]":
...
) -> "t.Optional[base.ST]": ...


@t.overload
Expand All @@ -200,8 +199,7 @@ def SchemaField(
*args,
null: "t.Literal[False]" = ...,
**kwargs,
) -> "base.ST":
...
) -> "base.ST": ...


def SchemaField(schema=None, config=None, default=None, *args, **kwargs) -> t.Any:
Expand Down
15 changes: 6 additions & 9 deletions django_pydantic_field/v1/rest_framework.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

from django.conf import settings
from pydantic import BaseModel, ValidationError

from rest_framework import exceptions, parsers, renderers, serializers
from rest_framework.schemas import openapi
from rest_framework.schemas.utils import is_list_view

from . import base
from django_pydantic_field.compat.typing import get_args

from . import base

__all__ = (
"SchemaField",
"SchemaRenderer",
Expand All @@ -32,10 +32,7 @@ def get_schema(self, ctx: "RequestResponseContext") -> t.Optional[t.Type[BaseMod
schema = self.get_annotation_schema(ctx)

if self.require_explicit_schema and schema is None:
raise ValueError(
"Schema should be either explicitly set with annotation "
"or passed in the context"
)
raise ValueError("Schema should be either explicitly set with annotation or passed in the context")

return schema

Expand Down Expand Up @@ -189,7 +186,7 @@ def map_renderers(self, path: str, method: str):
return response_types

def get_request_body(self, path: str, method: str):
if method not in ('PUT', 'PATCH', 'POST'):
if method not in ("PUT", "PATCH", "POST"):
return {}

self.request_media_types = self.map_parsers(path, method)
Expand All @@ -201,10 +198,10 @@ def get_request_body(self, path: str, method: str):
media_type, request_schema = request_type
content_schemas[media_type] = {"schema": request_schema}
else:
serializer_ref = self._get_reference(serializer)
serializer_ref = self._get_reference(serializer)
content_schemas[request_type] = {"schema": serializer_ref}

return {'content': content_schemas}
return {"content": content_schemas}

def get_responses(self, path: str, method: str):
if method == "DELETE":
Expand Down
1 change: 1 addition & 0 deletions django_pydantic_field/v2/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from django.utils.translation import gettext_lazy as _

from django_pydantic_field.compat import deprecation

from . import types

__all__ = ("SchemaField",)
Expand Down
3 changes: 2 additions & 1 deletion django_pydantic_field/v2/rest_framework/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
from rest_framework.schemas import utils as drf_schema_utils
from rest_framework.test import APIRequestFactory

from . import fields, parsers, renderers
from django_pydantic_field.v2 import utils

from . import fields, parsers, renderers

if ty.TYPE_CHECKING:
from collections.abc import Iterable

Expand Down
1 change: 1 addition & 0 deletions django_pydantic_field/v2/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from django_pydantic_field.compat.django import GenericContainer
from django_pydantic_field.compat.functools import cached_property

from . import utils

if ty.TYPE_CHECKING:
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ dev = [
"black",
"isort",
"mypy",
"pre-commit",
"pytest~=7.4",
"djangorestframework>=3.11,<4",
"django-stubs[compatible-mypy]~=4.2",
Expand Down

0 comments on commit c479dfb

Please sign in to comment.