From cf955f3c76e32aca7e054bb3bf80362bc1580b3b Mon Sep 17 00:00:00 2001 From: Ezeudoh Tochukwu Date: Wed, 9 Aug 2023 21:44:25 +0100 Subject: [PATCH 1/5] code linting with ruff --- .github/workflows/test_full.yml | 6 ++---- Makefile | 5 ++--- ninja_schema/orm/factory.py | 21 +++++++++--------- ninja_schema/orm/model_schema.py | 9 ++++---- ninja_schema/orm/utils/converter.py | 3 ++- ninja_schema/pydanticutils/__init__.py | 1 - pyproject.toml | 30 ++++++++++++++++++++++---- tests/test_converters.py | 3 ++- 8 files changed, 49 insertions(+), 29 deletions(-) diff --git a/.github/workflows/test_full.yml b/.github/workflows/test_full.yml index a673680..4abc0c7 100644 --- a/.github/workflows/test_full.yml +++ b/.github/workflows/test_full.yml @@ -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 diff --git a/Makefile b/Makefile index 02c5210..5110255 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/ninja_schema/orm/factory.py b/ninja_schema/orm/factory.py index 34c492d..eac2a6a 100755 --- a/ninja_schema/orm/factory.py +++ b/ninja_schema/orm/factory.py @@ -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 @@ -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) diff --git a/ninja_schema/orm/model_schema.py b/ninja_schema/orm/model_schema.py index 802fbdd..eaab474 100755 --- a/ninja_schema/orm/model_schema.py +++ b/ninja_schema/orm/model_schema.py @@ -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 = [] @@ -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 @@ -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() @@ -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 ( diff --git a/ninja_schema/orm/utils/converter.py b/ninja_schema/orm/utils/converter.py index f3aa619..d8fd1ca 100755 --- a/ninja_schema/orm/utils/converter.py +++ b/ninja_schema/orm/utils/converter.py @@ -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 diff --git a/ninja_schema/pydanticutils/__init__.py b/ninja_schema/pydanticutils/__init__.py index e0bf7c3..ed128c0 100755 --- a/ninja_schema/pydanticutils/__init__.py +++ b/ninja_schema/pydanticutils/__init__.py @@ -17,7 +17,6 @@ def compute_field_annotations( namespace: "DictStrAny", **field_definitions: Any, ) -> "DictStrAny": - fields = {} annotations = {} diff --git a/pyproject.toml b/pyproject.toml index b740986..cb336d0 100755 --- a/pyproject.toml +++ b/pyproject.toml @@ -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"] diff --git a/tests/test_converters.py b/tests/test_converters.py index 00e9adc..5c82aee 100644 --- a/tests/test_converters.py +++ b/tests/test_converters.py @@ -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 @@ -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( From a5a6feb5125b006e185da18a0762a44eee1031dc Mon Sep 17 00:00:00 2001 From: Ezeudoh Tochukwu Date: Wed, 9 Aug 2023 21:47:24 +0100 Subject: [PATCH 2/5] fixing mypy --- mypy.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mypy.ini b/mypy.ini index b33e3e7..4a30986 100644 --- a/mypy.ini +++ b/mypy.ini @@ -1,5 +1,5 @@ [mypy] -python_version = 3.6 +python_version = 3.8 show_column_numbers = True From 0c8354b9228b306bc2c6d9c316ffc3bcca8e0afa Mon Sep 17 00:00:00 2001 From: Ezeudoh Tochukwu Date: Wed, 9 Aug 2023 21:52:39 +0100 Subject: [PATCH 3/5] fixing mypy --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index cb336d0..859bcbf 100755 --- a/pyproject.toml +++ b/pyproject.toml @@ -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" From 2283a6e6d7f010ad89939ee018252ccb49070f96 Mon Sep 17 00:00:00 2001 From: Ezeudoh Tochukwu Date: Wed, 9 Aug 2023 21:58:00 +0100 Subject: [PATCH 4/5] fixing pydantic version in ci --- .github/workflows/test_full.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test_full.yml b/.github/workflows/test_full.yml index 4abc0c7..7851339 100644 --- a/.github/workflows/test_full.yml +++ b/.github/workflows/test_full.yml @@ -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 From 3a0fa79c891ca71d15b324589dd84d7809d716e0 Mon Sep 17 00:00:00 2001 From: Ezeudoh Tochukwu Date: Wed, 9 Aug 2023 22:32:11 +0100 Subject: [PATCH 5/5] fixing pydantic version in ci --- .github/workflows/test_full.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test_full.yml b/.github/workflows/test_full.yml index 7851339..02a48a4 100644 --- a/.github/workflows/test_full.yml +++ b/.github/workflows/test_full.yml @@ -21,7 +21,7 @@ jobs: with: python-version: ${{ matrix.python-version }} - name: Install core - 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] + 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