Skip to content

Commit

Permalink
Merge pull request #6 from eadwinCode/linting_with_ruff
Browse files Browse the repository at this point in the history
code linting with ruff
  • Loading branch information
eadwinCode committed Aug 20, 2023
2 parents d657768 + 3a0fa79 commit aebd890
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 32 deletions.
8 changes: 3 additions & 5 deletions .github/workflows/test_full.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
with:
python-version: ${{ matrix.python-version }}
- name: Install core
run: pip install "Django${{ matrix.django-version }}" pydantic pydantic[email]
run: pip install "Django${{ matrix.django-version }}" "pydantic >=1.6.2,!=1.7,!=1.7.1,!=1.7.2,!=1.7.3,!=1.8,!=1.8.1,<2.0.0" pydantic[email]
- name: Install tests
run: pip install pytest pytest-django
- name: Test
Expand All @@ -41,9 +41,7 @@ jobs:
run: flit install --symlink
- name: Black
run: black --check ninja_schema tests
- name: isort
run: isort --check ninja_schema tests
- name: Flake8
run: flake8 ninja_schema tests
- name: Ruff Linting Check
run: ruff check ninja_schema tests
- name: mypy
run: mypy ninja_schema
5 changes: 2 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@ install: ## Install dependencies
lint: ## Run code linters
make clean
black --check ninja_schema tests
isort --check ninja_schema tests
flake8 ninja_schema tests
ruff check ninja_schema tests
mypy ninja_schema

fmt format: ## Run code formatters
make clean
black ninja_schema tests
isort ninja_schema tests
ruff check --fix ninja_schema tests

test: ## Run tests
make clean
Expand Down
2 changes: 1 addition & 1 deletion mypy.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[mypy]
python_version = 3.6
python_version = 3.8

show_column_numbers = True

Expand Down
21 changes: 11 additions & 10 deletions ninja_schema/orm/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

from ..errors import ConfigError
from ..types import DictStrAny
from .schema_registry import SchemaRegister, registry as schema_registry
from .schema_registry import SchemaRegister
from .schema_registry import registry as schema_registry

if TYPE_CHECKING:
from .model_schema import ModelSchema
Expand Down Expand Up @@ -48,17 +49,17 @@ def create_schema(
if schema:
return schema

model_config_kwargs = dict(
model=model,
include=fields,
exclude=exclude,
skip_registry=skip_registry,
depth=depth,
registry=registry,
)
model_config_kwargs = {
"model": model,
"include": fields,
"exclude": exclude,
"skip_registry": skip_registry,
"depth": depth,
"registry": registry,
}
model_config = cls.get_model_config(**model_config_kwargs) # type: ignore

attrs = dict(Config=model_config)
attrs = {"Config": model_config}

new_schema = type(name, (ModelSchema,), attrs)
new_schema = cast(Type[ModelSchema], new_schema)
Expand Down
9 changes: 4 additions & 5 deletions ninja_schema/orm/model_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@


class PydanticNamespace:
__annotations__: Dict = dict()
__annotations__: Dict = {}
__config__: Optional[Type[BaseConfig]] = None
__fields__: Dict[str, ModelField] = dict()
__fields__: Dict[str, ModelField] = {}
__validators__: ModelValidatorGroup = ModelValidatorGroup({})
__pre_root_validators__: List = []
__post_root_validators__: List = []
Expand Down Expand Up @@ -285,7 +285,7 @@ def process_build_schema_parameters(self) -> None:
model_pk = getattr(
self.model._meta.pk, # type: ignore
"name",
getattr(self.model._meta.pk, "attname"), # type: ignore
self.model._meta.pk.attname, # type: ignore
) # no type:ignore
if (
model_pk not in self.include
Expand Down Expand Up @@ -324,7 +324,7 @@ def __new__(
except AttributeError as exc:
raise ConfigError(
f"{exc} (Is `Config.model` a valid Django model class?)"
)
) from exc

field_values, _seen = {}, set()

Expand Down Expand Up @@ -354,7 +354,6 @@ def __new__(

_seen.add(field_name)
if field_name in annotations and field_name in namespace:

python_type = annotations.pop(field_name)
pydantic_field = namespace[field_name]
if (
Expand Down
3 changes: 2 additions & 1 deletion ninja_schema/orm/utils/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
from ...compat import ArrayField, HStoreField, JSONField, RangeField
from ...types import DictStrAny
from ..factory import SchemaFactory
from ..schema_registry import SchemaRegister, registry as global_registry
from ..schema_registry import SchemaRegister
from ..schema_registry import registry as global_registry

if TYPE_CHECKING:
from ..model_schema import ModelSchema
Expand Down
1 change: 0 additions & 1 deletion ninja_schema/pydanticutils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ def compute_field_annotations(
namespace: "DictStrAny",
**field_definitions: Any,
) -> "DictStrAny":

fields = {}
annotations = {}

Expand Down
32 changes: 27 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ classifiers = [

requires = [
"Django >=2.0",
"pydantic",
"pydantic >=1.6.2,!=1.7,!=1.7.1,!=1.7.2,!=1.7.3,!=1.8,!=1.8.1,<2.0.0",
"pydantic[email]"
]
description-file = "README.md"
Expand All @@ -56,10 +56,32 @@ test = [
"pytest-cov",
"pytest-django",
"pytest-asyncio",
"mypy ==0.971",
"flake8 >=3.8.3,<4.0.0",
"black ==22.12.0",
"isort >=5.0.6,<6.0.0",
"mypy ==1.4.1",
"ruff ==0.0.275",
"black == 23.7.0",
"django-stubs",
]
doc = []



[tool.ruff]
select = [
"E", # pycodestyle errors
"W", # pycodestyle warnings
"F", # pyflakes
"I", # isort
"C", # flake8-comprehensions
"B", # flake8-bugbear
]
ignore = [
"E501", # line too long, handled by black
"B008", # do not perform function calls in argument defaults
"C901", # too complex
]

[tool.ruff.per-file-ignores]
"__init__.py" = ["F401"]

[tool.ruff.isort]
known-third-party = ["pydantic", "Django"]
3 changes: 2 additions & 1 deletion tests/test_converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import pytest
from django.db import models
from django.db.models import Manager
from pydantic import ValidationError

from ninja_schema import ModelSchema
from tests.models import Week
Expand Down Expand Up @@ -238,7 +239,7 @@ class Config:
},
"required": ["jsonfield", "positivebigintegerfield"],
}
with pytest.raises(Exception):
with pytest.raises(ValidationError):
ModelNewFieldsSchema(id=1, jsonfield={"any": "data"}, positivebigintegerfield=1)

obj = ModelNewFieldsSchema(
Expand Down

0 comments on commit aebd890

Please sign in to comment.