Skip to content

Commit 63daca4

Browse files
committed
fixed typing issue
1 parent 5db461b commit 63daca4

File tree

5 files changed

+21
-29
lines changed

5 files changed

+21
-29
lines changed

README.md

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ class CreateUserSchema(ModelSchema):
6363
## `from_orm(cls, obj: Any)`
6464
You can generate a schema instance from your django model instance
6565
```Python
66-
from typings import Optional
6766
from django.contrib.auth import get_user_model
6867
from ninja_schema import ModelSchema, model_validator
6968

@@ -90,15 +89,14 @@ print(schema.json(indent=2)
9089
}
9190
```
9291

93-
## `apply(self, model_instance, **kwargs)`
92+
## `apply_to_model(self, model_instance, **kwargs)`
9493
You can transfer data from your ModelSchema to Django Model instance using the `apply` function.
95-
The `apply` function uses Pydantic model `.dict` function, `dict` function filtering that can be passed as `kwargs` to the `.apply` function.
94+
The `apply_to_model` function uses Pydantic model `.dict` function, `dict` function filtering that can be passed as `kwargs` to the `.apply` function.
9695

9796
For more info, visit [Pydantic model export](https://pydantic-docs.helpmanual.io/usage/exporting_models/)
9897
```Python
99-
from typings import Optional
10098
from django.contrib.auth import get_user_model
101-
from ninja_schema import ModelSchema, model_validator
99+
from ninja_schema import ModelSchema
102100

103101
UserModel = get_user_model()
104102
new_user = UserModel.objects.create_user(username='eadwin', email='[email protected]', password='password')
@@ -111,7 +109,7 @@ class UpdateUserSchema(ModelSchema):
111109
optional = ['username'] # `username` is now optional
112110

113111
schema = UpdateUserSchema(first_name='Emeka', last_name='Okoro')
114-
schema.apply(new_user, exclude_none=True)
112+
schema.apply_to_model(new_user, exclude_none=True)
115113

116114
assert new_user.first_name == 'Emeka' # True
117115
assert new_user.username == 'eadwin' # True
@@ -269,4 +267,3 @@ print(UserSchema.schema())
269267
}
270268
}
271269
```
272-

ninja_schema/orm/schema_registry.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ def register_model(self, model: Type[Model], schema: Type["ModelSchema"]) -> Non
3131
from ninja_schema.orm.model_schema import ModelSchema
3232

3333
assert is_valid_class(schema) and issubclass(schema, (ModelSchema,)), (
34-
"Only Schema can be" 'registered, received "{}"'.format(schema.__name__)
34+
'Only Schema can beregistered, received "{}"'.format(schema.__name__)
35+
)
36+
assert is_valid_django_model(model), (
37+
"Only Django Models are allowed. {}".format(model.__name__)
3538
)
36-
assert is_valid_django_model(
37-
model
38-
), "Only Django Models are allowed. {}".format(model.__name__)
3939
# TODO: register model as module_name.model_name
4040
self.register_schema(model, schema)
4141

ninja_schema/orm/utils/converter.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@
3939

4040
def assert_valid_name(name: str) -> None:
4141
"""Helper to assert that provided names are valid."""
42-
assert COMPILED_NAME_PATTERN.match(
43-
name
44-
), 'Names must match /{}/ but "{}" does not.'.format(NAME_PATTERN, name)
42+
assert COMPILED_NAME_PATTERN.match(name), (
43+
'Names must match /{}/ but "{}" does not.'.format(NAME_PATTERN, name)
44+
)
4545

4646

4747
def convert_choice_name(name: str) -> str:

tests/test_v1_pydantic/test_custom_fields.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class Config:
3333
"title": "SemesterEnum",
3434
"description": "An enumeration.",
3535
"enum": ["1", "2", "3"],
36-
"type": "string"
36+
"type": "string",
3737
}
3838
},
3939
}

tests/test_v2_pydantic/test_model_schema.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import json
2+
import typing as t
23

34
import pydantic
45
import pytest
6+
from django.db.models import Model as DjangoModel
57

68
from ninja_schema import ModelSchema, SchemaFactory, model_validator
79
from ninja_schema.errors import ConfigError
810
from ninja_schema.pydanticutils import IS_PYDANTIC_V1
911
from tests.models import Event
1012

13+
T = t.TypeVar("T", bound=DjangoModel)
14+
1115

1216
class TestModelSchema:
1317
@pytest.mark.skipif(IS_PYDANTIC_V1, reason="requires pydantic == 2.1.x")
@@ -522,15 +526,8 @@ def test_schema_with_mixin_generic_class(self):
522526
Test that a schema with a generic mixin class works correctly.
523527
"""
524528

525-
import datetime
526-
import typing as t
527-
528-
from django.db.models import Model as DjangoModel
529-
530-
T = t.TypeVar("T", bound=DjangoModel)
531-
532529
class GenericMixin(t.Generic[T]):
533-
def save(self, instance: T | None = None) -> T:
530+
def save(self, instance: t.Optional[T] = None) -> T:
534531
"""
535532
Save the model instance and return it.
536533
"""
@@ -542,19 +539,17 @@ def save(self, instance: T | None = None) -> T:
542539
instance = self.Config.model(**self.dict())
543540
instance.save()
544541
return instance
545-
class BaseModelSchema(ModelSchema, GenericMixin[T]):
546-
...
542+
543+
class BaseModelSchema(ModelSchema, GenericMixin[T]): ...
544+
547545
class EventGenericSchema(BaseModelSchema[Event]):
548546
class Config:
549547
model = Event
550-
include = (
551-
"title",
552-
)
548+
include = ("title",)
553549

554550
event = EventGenericSchema(title="PyConf 2021")
555551
assert event.title == "PyConf 2021"
556552

557553
instance_event = event.save()
558554
assert isinstance(instance_event, Event)
559555
assert instance_event.title == "PyConf 2021"
560-

0 commit comments

Comments
 (0)