restore typing in tests and docs, remove unused metaclass code

This commit is contained in:
collerek
2020-11-01 10:11:25 +01:00
parent be35c80412
commit 358b5c2e52
49 changed files with 354 additions and 324 deletions

View File

@ -75,8 +75,8 @@ class Album(ormar.Model):
metadata = metadata
database = database
id = ormar.Integer(primary_key=True)
name = ormar.String(length=100)
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(length=100)
class Track(ormar.Model):
@ -85,10 +85,10 @@ class Track(ormar.Model):
metadata = metadata
database = database
id = ormar.Integer(primary_key=True)
id: int = ormar.Integer(primary_key=True)
album= ormar.ForeignKey(Album)
title = ormar.String(length=100)
position = ormar.Integer()
title: str = ormar.String(length=100)
position: int = ormar.Integer()
# Create some records to work with.

View File

@ -75,8 +75,11 @@ class Album(ormar.Model):
metadata = metadata
database = database
id = ormar.Integer(primary_key=True)
name = ormar.String(length=100)
# note that type hints are optional so
# id = ormar.Integer(primary_key=True)
# is also valid
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(length=100)
class Track(ormar.Model):
@ -85,10 +88,10 @@ class Track(ormar.Model):
metadata = metadata
database = database
id = ormar.Integer(primary_key=True)
id: int = ormar.Integer(primary_key=True)
album: Optional[Album] =ormar.ForeignKey(Album)
title = ormar.String(length=100)
position = ormar.Integer()
title: str = ormar.String(length=100)
position: int = ormar.Integer()
# Create some records to work with.

View File

@ -34,7 +34,7 @@ By default if you assign primary key to `Integer` field, the `autoincrement` opt
You can disable by passing `autoincremant=False`.
```Python
id = ormar.Integer(primary_key=True, autoincrement=False)
id: int = ormar.Integer(primary_key=True, autoincrement=False)
```
### Fields names vs Column names

View File

@ -109,10 +109,10 @@ class Book(ormar.Model):
metadata = metadata
database = database
id = ormar.Integer(primary_key=True)
title = ormar.String(max_length=200)
author = ormar.String(max_length=100)
genre = ormar.String(max_length=100, default='Fiction', choices=['Fiction', 'Adventure', 'Historic', 'Fantasy'])
id: int = ormar.Integer(primary_key=True)
title: str = ormar.String(max_length=200)
author: str = ormar.String(max_length=100)
genre: str = ormar.String(max_length=100, default='Fiction', choices=['Fiction', 'Adventure', 'Historic', 'Fantasy'])
await Book.objects.create(title='Tom Sawyer', author="Twain, Mark", genre='Adventure')
await Book.objects.create(title='War and Peace', author="Tolstoy, Leo", genre='Fiction')
@ -146,10 +146,10 @@ class Book(ormar.Model):
metadata = metadata
database = database
id = ormar.Integer(primary_key=True)
title = ormar.String(max_length=200)
author = ormar.String(max_length=100)
genre = ormar.String(max_length=100, default='Fiction', choices=['Fiction', 'Adventure', 'Historic', 'Fantasy'])
id: int = ormar.Integer(primary_key=True)
title: str = ormar.String(max_length=200)
author: str = ormar.String(max_length=100)
genre: str = ormar.String(max_length=100, default='Fiction', choices=['Fiction', 'Adventure', 'Historic', 'Fantasy'])
await Book.objects.create(title='Tom Sawyer', author="Twain, Mark", genre='Adventure')
await Book.objects.create(title='War and Peace', author="Tolstoy, Leo", genre='Fiction')
@ -192,8 +192,8 @@ class ToDo(ormar.Model):
metadata = metadata
database = database
id = ormar.Integer(primary_key=True)
text = ormar.String(max_length=500)
id: int = ormar.Integer(primary_key=True)
text: str = ormar.String(max_length=500)
completed= ormar.Boolean(default=False)
# create multiple instances at once with bulk_create
@ -259,10 +259,10 @@ class Book(ormar.Model):
metadata = metadata
database = database
id = ormar.Integer(primary_key=True)
title = ormar.String(max_length=200)
author = ormar.String(max_length=100)
genre = ormar.String(max_length=100, default='Fiction', choices=['Fiction', 'Adventure', 'Historic', 'Fantasy'])
id: int = ormar.Integer(primary_key=True)
title: str = ormar.String(max_length=200)
author: str = ormar.String(max_length=100)
genre: str = ormar.String(max_length=100, default='Fiction', choices=['Fiction', 'Adventure', 'Historic', 'Fantasy'])
await Book.objects.create(title='Tom Sawyer', author="Twain, Mark", genre='Adventure')
await Book.objects.create(title='War and Peace in Space', author="Tolstoy, Leo", genre='Fantasy')
@ -470,9 +470,9 @@ class Company(ormar.Model):
metadata = metadata
database = database
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)
founded = ormar.Integer(nullable=True)
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100)
founded: int = ormar.Integer(nullable=True)
class Car(ormar.Model):
@ -481,13 +481,13 @@ class Car(ormar.Model):
metadata = metadata
database = database
id = ormar.Integer(primary_key=True)
id: int = ormar.Integer(primary_key=True)
manufacturer= ormar.ForeignKey(Company)
name = ormar.String(max_length=100)
year = ormar.Integer(nullable=True)
gearbox_type = ormar.String(max_length=20, nullable=True)
gears = ormar.Integer(nullable=True)
aircon_type = ormar.String(max_length=20, nullable=True)
name: str = ormar.String(max_length=100)
year: int = ormar.Integer(nullable=True)
gearbox_type: str = ormar.String(max_length=20, nullable=True)
gears: int = ormar.Integer(nullable=True)
aircon_type: str = ormar.String(max_length=20, nullable=True)

0
docs_src/__init__.py Normal file
View File

View File

View File

@ -32,8 +32,8 @@ class Category(ormar.Model):
metadata = metadata
database = database
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100)
class Item(ormar.Model):
@ -42,9 +42,9 @@ class Item(ormar.Model):
metadata = metadata
database = database
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)
category = ormar.ForeignKey(Category, nullable=True)
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100)
category: Optional[Category] = ormar.ForeignKey(Category, nullable=True)
@app.get("/items/", response_model=List[Item])

View File

View File

@ -29,8 +29,8 @@ class Course(ormar.Model):
department: Optional[Department] = ormar.ForeignKey(Department)
department = Department(name='Science')
course = Course(name='Math', completed=False, department=department)
department = Department(name="Science")
course = Course(name="Math", completed=False, department=department)
print(department.courses[0])
# Will produce:

View File

@ -14,8 +14,8 @@ class Department(ormar.Model):
database = database
metadata = metadata
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100)
class Course(ormar.Model):
@ -23,14 +23,14 @@ class Course(ormar.Model):
database = database
metadata = metadata
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)
completed= ormar.Boolean(default=False)
department= ormar.ForeignKey(Department, related_name="my_courses")
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100)
completed: bool = ormar.Boolean(default=False)
department: Optional[Department] = ormar.ForeignKey(Department, related_name="my_courses")
department = Department(name='Science')
course = Course(name='Math', completed=False, department=department)
department = Department(name="Science")
course = Course(name="Math", completed=False, department=department)
print(department.my_courses[0])
# Will produce:

View File

@ -14,8 +14,8 @@ class Department(ormar.Model):
database = database
metadata = metadata
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100)
class Course(ormar.Model):
@ -23,7 +23,7 @@ class Course(ormar.Model):
database = database
metadata = metadata
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)
completed= ormar.Boolean(default=False)
department= ormar.ForeignKey(Department)
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100)
completed: bool = ormar.Boolean(default=False)
department: Optional[Department] = ormar.ForeignKey(Department)

View File

@ -16,8 +16,8 @@ class Product(ormar.Model):
metadata = metadata
database = database
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)
company = ormar.String(max_length=200, server_default='Acme')
sort_order = ormar.Integer(server_default=text("10"))
created= ormar.DateTime(server_default=func.now())
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100)
company: str = ormar.String(max_length=200, server_default="Acme")
sort_order: int = ormar.Integer(server_default=text("10"))
created: datetime = ormar.DateTime(server_default=func.now())

View File

View File

@ -12,6 +12,6 @@ class Course(ormar.Model):
database = database
metadata = metadata
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)
completed= ormar.Boolean(default=False)
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100)
completed: bool = ormar.Boolean(default=False)

View File

@ -15,6 +15,6 @@ class Course(ormar.Model):
database = database
metadata = metadata
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)
completed= ormar.Boolean(default=False)
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100)
completed: bool = ormar.Boolean(default=False)

View File

@ -12,9 +12,9 @@ class Course(ormar.Model):
database = database
metadata = metadata
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)
completed= ormar.Boolean(default=False)
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100)
completed: bool = ormar.Boolean(default=False)
print(Course.__fields__)

View File

@ -8,13 +8,13 @@ metadata = sqlalchemy.MetaData()
class Course(ormar.Model):
class Meta:
class Meta(ormar.ModelMeta): # note you don't have to subclass - but it's recommended for ide completion and mypy
database = database
metadata = metadata
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)
completed= ormar.Boolean(default=False)
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100)
completed: bool = ormar.Boolean(default=False)
print(Course.Meta.table.columns)

View File

@ -8,13 +8,14 @@ metadata = sqlalchemy.MetaData()
class Course(ormar.Model):
class Meta:
class Meta(ormar.ModelMeta):
database = database
metadata = metadata
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)
completed= ormar.Boolean(default=False)
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100)
completed: bool = ormar.Boolean(default=False)
print({x: v.__dict__ for x, v in Course.Meta.model_fields.items()})
"""

View File

@ -14,8 +14,8 @@ class Course(ormar.Model):
# define your constraints in Meta class of the model
# it's a list that can contain multiple constraints
# hera a combination of name and column will have to be unique in db
constraints = [ormar.UniqueColumns('name', 'completed')]
constraints = [ormar.UniqueColumns("name", "completed")]
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)
completed = ormar.Boolean(default=False)
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100)
completed: bool = ormar.Boolean(default=False)

View File

@ -12,12 +12,11 @@ class Course(ormar.Model):
database = database
metadata = metadata
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)
completed = ormar.Boolean(default=False)
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100)
completed: bool = ormar.Boolean(default=False)
course = Course(name="Painting for dummies", completed=False)
await course.save()
await Course.objects.create(name="Painting for dummies", completed=False)
await course.save() # type: ignore
await Course.objects.create(name="Painting for dummies", completed=False) # type: ignore

View File

@ -13,7 +13,7 @@ class Child(ormar.Model):
metadata = metadata
database = database
id = ormar.Integer(name='child_id', primary_key=True)
first_name = ormar.String(name='fname', max_length=100)
last_name = ormar.String(name='lname', max_length=100)
born_year = ormar.Integer(name='year_born', nullable=True)
id: int = ormar.Integer(name="child_id", primary_key=True)
first_name: str = ormar.String(name="fname", max_length=100)
last_name: str = ormar.String(name="lname", max_length=100)
born_year: int = ormar.Integer(name="year_born", nullable=True)

View File

@ -1,9 +1,21 @@
from typing import Optional
import databases
import sqlalchemy
import ormar
from .docs010 import Artist # previous example
database = databases.Database("sqlite:///test.db", force_rollback=True)
metadata = sqlalchemy.MetaData()
class Album(ormar.Model):
class Meta:
tablename = "music_albums"
metadata = metadata
database = database
id = ormar.Integer(name='album_id', primary_key=True)
name = ormar.String(name='album_name', max_length=100)
artist= ormar.ForeignKey(Artist, name='artist_id')
id: int = ormar.Integer(name="album_id", primary_key=True)
name: str = ormar.String(name="album_name", max_length=100)
artist: Optional[Artist] = ormar.ForeignKey(Artist, name="artist_id")

View File

@ -1,3 +1,15 @@
from typing import Optional, Union, List
import databases
import sqlalchemy
import ormar
from .docs008 import Child
database = databases.Database("sqlite:///test.db", force_rollback=True)
metadata = sqlalchemy.MetaData()
class ArtistChildren(ormar.Model):
class Meta:
tablename = "children_x_artists"
@ -11,8 +23,10 @@ class Artist(ormar.Model):
metadata = metadata
database = database
id = ormar.Integer(name='artist_id', primary_key=True)
first_name = ormar.String(name='fname', max_length=100)
last_name = ormar.String(name='lname', max_length=100)
born_year = ormar.Integer(name='year')
children: Optional[Union[Child, List[Child]]] = ormar.ManyToMany(Child, through=ArtistChildren)
id: int = ormar.Integer(name="artist_id", primary_key=True)
first_name: str = ormar.String(name="fname", max_length=100)
last_name: str = ormar.String(name="lname", max_length=100)
born_year: int = ormar.Integer(name="year")
children: Optional[Union[Child, List[Child]]] = ormar.ManyToMany(
Child, through=ArtistChildren
)

View File

View File

@ -14,8 +14,8 @@ class Album(ormar.Model):
metadata = metadata
database = database
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100)
class Track(ormar.Model):
@ -24,7 +24,7 @@ class Track(ormar.Model):
metadata = metadata
database = database
id = ormar.Integer(primary_key=True)
album= ormar.ForeignKey(Album)
title = ormar.String(max_length=100)
position = ormar.Integer()
id: int = ormar.Integer(primary_key=True)
album: Optional[Album] = ormar.ForeignKey(Album)
title: str = ormar.String(max_length=100)
position: int = ormar.Integer()

View File

View File

@ -1,4 +1,4 @@
from typing import Optional
from typing import Optional, Dict, Union
import databases
import sqlalchemy
@ -14,8 +14,8 @@ class Department(ormar.Model):
database = database
metadata = metadata
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100)
class Course(ormar.Model):
@ -23,22 +23,22 @@ class Course(ormar.Model):
database = database
metadata = metadata
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)
completed= ormar.Boolean(default=False)
department= ormar.ForeignKey(Department)
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100)
completed: bool = ormar.Boolean(default=False)
department: Optional[Union[Department, Dict]] = ormar.ForeignKey(Department)
department = Department(name='Science')
department = Department(name="Science")
# set up a relation with actual Model instance
course = Course(name='Math', completed=False, department=department)
course = Course(name="Math", completed=False, department=department)
# set up relation with only related model pk value
course2 = Course(name='Math II', completed=False, department=department.pk)
course2 = Course(name="Math II", completed=False, department=department.pk)
# set up a relation with dictionary corresponding to related model
course3 = Course(name='Math III', completed=False, department=department.dict())
course3 = Course(name="Math III", completed=False, department=department.dict())
# explicitly set up None
course4 = Course(name='Math III', completed=False, department=None)
course4 = Course(name="Math III", completed=False, department=None)

View File

@ -14,9 +14,9 @@ class Author(ormar.Model):
database = database
metadata = metadata
id = ormar.Integer(primary_key=True)
first_name = ormar.String(max_length=80)
last_name = ormar.String(max_length=80)
id: int = ormar.Integer(primary_key=True)
first_name: str = ormar.String(max_length=80)
last_name: str = ormar.String(max_length=80)
class Category(ormar.Model):
@ -25,8 +25,8 @@ class Category(ormar.Model):
database = database
metadata = metadata
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=40)
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=40)
class PostCategory(ormar.Model):
@ -44,7 +44,9 @@ class Post(ormar.Model):
database = database
metadata = metadata
id = ormar.Integer(primary_key=True)
title = ormar.String(max_length=200)
categories: Optional[Union[Category, List[Category]]] = ormar.ManyToMany(Category, through=PostCategory)
author= ormar.ForeignKey(Author)
id: int = ormar.Integer(primary_key=True)
title: str = ormar.String(max_length=200)
categories: Optional[Union[Category, List[Category]]] = ormar.ManyToMany(
Category, through=PostCategory
)
author: Optional[Author] = ormar.ForeignKey(Author)

View File

@ -18,6 +18,7 @@ from ormar.fields import ( # noqa: I100
UniqueColumns,
)
from ormar.models import Model
from ormar.models.metaclass import ModelMeta
from ormar.queryset import QuerySet
from ormar.relations import RelationType
@ -56,4 +57,5 @@ __all__ = [
"UniqueColumns",
"QuerySetProtocol",
"RelationProtocol",
"ModelMeta",
]

View File

@ -1,7 +1,7 @@
import datetime
import decimal
import uuid
from typing import Any, Optional, Type
from typing import Any, Optional, TYPE_CHECKING, Type
import pydantic
import sqlalchemy
@ -178,6 +178,14 @@ class Float(ModelFieldFactory, float):
return sqlalchemy.Float()
if TYPE_CHECKING: # pragma: nocover
def Boolean(**kwargs: Any) -> bool:
pass
else:
class Boolean(ModelFieldFactory, int):
_type = bool

View File

@ -1,4 +1,5 @@
import logging
import warnings
from typing import Any, Dict, List, Optional, TYPE_CHECKING, Tuple, Type, Union
import databases
@ -192,20 +193,6 @@ def populate_default_pydantic_field_value(
return attrs
def check_if_field_annotation_or_value_is_ormar(
field: Any, field_name: str, attrs: Dict
) -> bool:
return lenient_issubclass(field, BaseField) or issubclass(
attrs.get(field_name, type), BaseField
)
def extract_field_from_annotation_or_value(
field: Any, field_name: str, attrs: Dict
) -> Type[ormar.fields.BaseField]:
return field if lenient_issubclass(field, BaseField) else attrs.get(field_name)
def populate_pydantic_default_values(attrs: Dict) -> Tuple[Dict, Dict]:
model_fields = {}
potential_fields = {
@ -213,22 +200,22 @@ def populate_pydantic_default_values(attrs: Dict) -> Tuple[Dict, Dict]:
for k, v in attrs["__annotations__"].items()
if lenient_issubclass(v, BaseField)
}
if potential_fields:
warnings.warn(
"Using ormar.Fields as type Model annotation has been deprecated,"
" check documentation of current version",
DeprecationWarning,
)
potential_fields.update(
{k: v for k, v in attrs.items() if lenient_issubclass(v, BaseField)}
)
for field_name, field in potential_fields.items():
# ormar fields can be used as annotation or as default value
if check_if_field_annotation_or_value_is_ormar(field, field_name, attrs):
ormar_field = extract_field_from_annotation_or_value(
field, field_name, attrs
)
if ormar_field.name is None:
ormar_field.name = field_name
attrs = populate_default_pydantic_field_value(
ormar_field, field_name, attrs
)
model_fields[field_name] = ormar_field
attrs["__annotations__"][field_name] = ormar_field.__type__
if field.name is None:
field.name = field_name
attrs = populate_default_pydantic_field_value(field, field_name, attrs)
model_fields[field_name] = field
attrs["__annotations__"][field_name] = field.__type__
return attrs, model_fields

View File

@ -1,3 +1,5 @@
from typing import Optional, Union, List
import databases
import pytest
import sqlalchemy
@ -15,10 +17,10 @@ class Child(ormar.Model):
metadata = metadata
database = database
id = ormar.Integer(name="child_id", primary_key=True)
first_name = ormar.String(name="fname", max_length=100)
last_name = ormar.String(name="lname", max_length=100)
born_year = ormar.Integer(name="year_born", nullable=True)
id: int = ormar.Integer(name="child_id", primary_key=True)
first_name: str = ormar.String(name="fname", max_length=100)
last_name: str = ormar.String(name="lname", max_length=100)
born_year: int = ormar.Integer(name="year_born", nullable=True)
class ArtistChildren(ormar.Model):
@ -34,11 +36,11 @@ class Artist(ormar.Model):
metadata = metadata
database = database
id = ormar.Integer(name="artist_id", primary_key=True)
first_name = ormar.String(name="fname", max_length=100)
last_name = ormar.String(name="lname", max_length=100)
born_year = ormar.Integer(name="year")
children = ormar.ManyToMany(Child, through=ArtistChildren)
id: int = ormar.Integer(name="artist_id", primary_key=True)
first_name: str = ormar.String(name="fname", max_length=100)
last_name: str = ormar.String(name="lname", max_length=100)
born_year: int = ormar.Integer(name="year")
children: Optional[Union[Child, List[Child]]] = ormar.ManyToMany(Child, through=ArtistChildren)
class Album(ormar.Model):
@ -47,9 +49,9 @@ class Album(ormar.Model):
metadata = metadata
database = database
id = ormar.Integer(name="album_id", primary_key=True)
name = ormar.String(name="album_name", max_length=100)
artist = ormar.ForeignKey(Artist, name="artist_id")
id: int = ormar.Integer(name="album_id", primary_key=True)
name: str = ormar.String(name="album_name", max_length=100)
artist: Optional[Artist] = ormar.ForeignKey(Artist, name="artist_id")
@pytest.fixture(autouse=True, scope="module")

View File

@ -23,14 +23,14 @@ class Example(ormar.Model):
metadata = metadata
database = database
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=200, default="aaa")
created = ormar.DateTime(default=datetime.datetime.now)
created_day = ormar.Date(default=datetime.date.today)
created_time = ormar.Time(default=time)
description = ormar.Text(nullable=True)
value = ormar.Float(nullable=True)
data = ormar.JSON(default={})
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=200, default="aaa")
created: datetime.datetime = ormar.DateTime(default=datetime.datetime.now)
created_day: datetime.date = ormar.Date(default=datetime.date.today)
created_time: datetime.time = ormar.Time(default=time)
description: str = ormar.Text(nullable=True)
value: float = ormar.Float(nullable=True)
data: pydantic.Json = ormar.JSON(default={})
@pytest.fixture(autouse=True, scope="module")

View File

View File

@ -1,4 +1,4 @@
from typing import List
from typing import List, Union, Optional
import databases
import pytest
@ -38,8 +38,8 @@ class Category(ormar.Model):
class Meta(LocalMeta):
tablename = "categories"
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100)
class ItemsXCategories(ormar.Model):
@ -51,8 +51,8 @@ class Item(ormar.Model):
class Meta(LocalMeta):
pass
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100)
categories = ormar.ManyToMany(Category, through=ItemsXCategories)

View File

@ -20,8 +20,8 @@ class Category(ormar.Model):
metadata = metadata
database = database
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100)
class Item(ormar.Model):
@ -30,9 +30,9 @@ class Item(ormar.Model):
metadata = metadata
database = database
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)
category = ormar.ForeignKey(Category, nullable=True)
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100)
category: Optional[Category] = ormar.ForeignKey(Category, nullable=True)
@app.post("/items/", response_model=Item)

View File

@ -40,9 +40,9 @@ class Cover(ormar.Model):
metadata = metadata
database = database
id = ormar.Integer(primary_key=True)
album = ormar.ForeignKey(Album, related_name="cover_pictures")
title = ormar.String(max_length=100)
id: int = ormar.Integer(primary_key=True)
album: Optional[Album] = ormar.ForeignKey(Album, related_name="cover_pictures")
title: str = ormar.String(max_length=100)
class Organisation(ormar.Model):
@ -51,8 +51,8 @@ class Organisation(ormar.Model):
metadata = metadata
database = database
id = ormar.Integer(primary_key=True)
ident = ormar.String(max_length=100, choices=["ACME Ltd", "Other ltd"])
id: int = ormar.Integer(primary_key=True)
ident: str = ormar.String(max_length=100, choices=["ACME Ltd", "Other ltd"])
class Organization(object):
@ -65,9 +65,9 @@ class Team(ormar.Model):
metadata = metadata
database = database
id = ormar.Integer(primary_key=True)
org = ormar.ForeignKey(Organisation)
name = ormar.String(max_length=100)
id: int = ormar.Integer(primary_key=True)
org: Optional[Organisation] = ormar.ForeignKey(Organisation)
name: str = ormar.String(max_length=100)
class Member(ormar.Model):
@ -76,9 +76,9 @@ class Member(ormar.Model):
metadata = metadata
database = database
id = ormar.Integer(primary_key=True)
team = ormar.ForeignKey(Team)
email = ormar.String(max_length=100)
id: int = ormar.Integer(primary_key=True)
team: Optional[Team] = ormar.ForeignKey(Team)
email: str = ormar.String(max_length=100)
@pytest.fixture(autouse=True, scope="module")

View File

@ -19,9 +19,9 @@ class Author(ormar.Model):
database = database
metadata = metadata
id = ormar.Integer(primary_key=True)
first_name = ormar.String(max_length=80)
last_name = ormar.String(max_length=80)
id: int = ormar.Integer(primary_key=True)
first_name: str = ormar.String(max_length=80)
last_name: str = ormar.String(max_length=80)
class Category(ormar.Model):
@ -30,8 +30,8 @@ class Category(ormar.Model):
database = database
metadata = metadata
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=40)
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=40)
class PostCategory(ormar.Model):
@ -47,10 +47,10 @@ class Post(ormar.Model):
database = database
metadata = metadata
id = ormar.Integer(primary_key=True)
title = ormar.String(max_length=200)
categories = ormar.ManyToMany(Category, through=PostCategory)
author = ormar.ForeignKey(Author)
id: int = ormar.Integer(primary_key=True)
title: str = ormar.String(max_length=200)
categories: Optional[Union[Category, List[Category]]] = ormar.ManyToMany(Category, through=PostCategory)
author: Optional[Author] = ormar.ForeignKey(Author)
@pytest.fixture(scope="module")

View File

@ -21,16 +21,16 @@ class ExampleModel(Model):
tablename = "example"
metadata = metadata
test = ormar.Integer(primary_key=True)
test_string = ormar.String(max_length=250)
test_text = ormar.Text(default="")
test_bool = ormar.Boolean(nullable=False)
test: int = ormar.Integer(primary_key=True)
test_string: str = ormar.String(max_length=250)
test_text: str = ormar.Text(default="")
test_bool: bool = ormar.Boolean(nullable=False)
test_float: ormar.Float() = None # type: ignore
test_datetime = ormar.DateTime(default=datetime.datetime.now)
test_date = ormar.Date(default=datetime.date.today)
test_time = ormar.Time(default=datetime.time)
test_json = ormar.JSON(default={})
test_bigint = ormar.BigInteger(default=0)
test_bigint: int = ormar.BigInteger(default=0)
test_decimal = ormar.Decimal(scale=2, precision=10)
test_decimal2 = ormar.Decimal(max_digits=10, decimal_places=2)
@ -53,8 +53,8 @@ class ExampleModel2(Model):
tablename = "examples"
metadata = metadata
test = ormar.Integer(primary_key=True)
test_string = ormar.String(max_length=250)
test: int = ormar.Integer(primary_key=True)
test_string: str = ormar.String(max_length=250)
@pytest.fixture(scope="module")
@ -145,7 +145,7 @@ def test_no_pk_in_model_definition(): # type: ignore
tablename = "example2"
metadata = metadata
test_string = ormar.String(max_length=250) # type: ignore
test_string: str = ormar.String(max_length=250) # type: ignore
@typing.no_type_check
@ -158,8 +158,8 @@ def test_two_pks_in_model_definition():
tablename = "example3"
metadata = metadata
id = ormar.Integer(primary_key=True)
test_string = ormar.String(max_length=250, primary_key=True)
id: int = ormar.Integer(primary_key=True)
test_string: str = ormar.String(max_length=250, primary_key=True)
@typing.no_type_check
@ -171,7 +171,7 @@ def test_setting_pk_column_as_pydantic_only_in_model_definition():
tablename = "example4"
metadata = metadata
test = ormar.Integer(primary_key=True, pydantic_only=True)
test: int = ormar.Integer(primary_key=True, pydantic_only=True)
@typing.no_type_check
@ -183,7 +183,7 @@ def test_decimal_error_in_model_definition():
tablename = "example5"
metadata = metadata
test = ormar.Decimal(primary_key=True)
test: decimal.Decimal = ormar.Decimal(primary_key=True)
@typing.no_type_check
@ -195,7 +195,7 @@ def test_string_error_in_model_definition():
tablename = "example6"
metadata = metadata
test = ormar.String(primary_key=True)
test: str = ormar.String(primary_key=True)
@typing.no_type_check

View File

@ -1,6 +1,6 @@
import asyncio
import uuid
from datetime import datetime
import datetime
from typing import List
import databases
@ -22,7 +22,7 @@ class JsonSample(ormar.Model):
metadata = metadata
database = database
id = ormar.Integer(primary_key=True)
id: int = ormar.Integer(primary_key=True)
test_json = ormar.JSON(nullable=True)
@ -32,8 +32,8 @@ class UUIDSample(ormar.Model):
metadata = metadata
database = database
id = ormar.UUID(primary_key=True, default=uuid.uuid4)
test_text = ormar.Text()
id: uuid.UUID = ormar.UUID(primary_key=True, default=uuid.uuid4)
test_text: str = ormar.Text()
class User(ormar.Model):
@ -42,8 +42,8 @@ class User(ormar.Model):
metadata = metadata
database = database
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100, default="")
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100, default="")
class Product(ormar.Model):
@ -52,11 +52,11 @@ class Product(ormar.Model):
metadata = metadata
database = database
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)
rating = ormar.Integer(minimum=1, maximum=5)
in_stock = ormar.Boolean(default=False)
last_delivery = ormar.Date(default=datetime.now)
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100)
rating: int = ormar.Integer(minimum=1, maximum=5)
in_stock: bool = ormar.Boolean(default=False)
last_delivery: datetime.date = ormar.Date(default=datetime.datetime.now)
country_name_choices = ("Canada", "Algeria", "United States")
@ -70,10 +70,10 @@ class Country(ormar.Model):
metadata = metadata
database = database
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=9, choices=country_name_choices, default="Canada",)
taxed = ormar.Boolean(choices=country_taxed_choices, default=True)
country_code = ormar.Integer(
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=9, choices=country_name_choices, default="Canada",)
taxed: bool = ormar.Boolean(choices=country_taxed_choices, default=True)
country_code: int = ormar.Integer(
minimum=0, maximum=1000, choices=country_country_code_choices, default=1
)
@ -213,7 +213,7 @@ async def test_model_filter():
assert product.pk is not None
assert product.name == "T-Shirt"
assert product.rating == 5
assert product.last_delivery == datetime.now().date()
assert product.last_delivery == datetime.datetime.now().date()
products = await Product.objects.all(rating__gte=2, in_stock=True)
assert len(products) == 2

View File

@ -35,8 +35,8 @@ class Category(ormar.Model):
metadata = metadata
database = database
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100)
class Item(ormar.Model):
@ -45,9 +45,9 @@ class Item(ormar.Model):
metadata = metadata
database = database
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)
category = ormar.ForeignKey(Category, nullable=True)
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100)
category: Optional[Category] = ormar.ForeignKey(Category, nullable=True)
@pytest.fixture(autouse=True, scope="module")

View File

@ -18,8 +18,8 @@ class Department(ormar.Model):
metadata = metadata
database = database
id = ormar.Integer(primary_key=True, autoincrement=False)
name = ormar.String(max_length=100)
id: int = ormar.Integer(primary_key=True, autoincrement=False)
name: str = ormar.String(max_length=100)
class SchoolClass(ormar.Model):
@ -28,8 +28,8 @@ class SchoolClass(ormar.Model):
metadata = metadata
database = database
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100)
class Category(ormar.Model):
@ -38,9 +38,9 @@ class Category(ormar.Model):
metadata = metadata
database = database
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)
department = ormar.ForeignKey(Department, nullable=False)
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100)
department: Optional[Department] = ormar.ForeignKey(Department, nullable=False)
class Student(ormar.Model):
@ -49,10 +49,10 @@ class Student(ormar.Model):
metadata = metadata
database = database
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)
schoolclass = ormar.ForeignKey(SchoolClass)
category = ormar.ForeignKey(Category, nullable=True)
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100)
schoolclass: Optional[SchoolClass] = ormar.ForeignKey(SchoolClass)
category: Optional[Category] = ormar.ForeignKey(Category, nullable=True)
class Teacher(ormar.Model):
@ -61,10 +61,10 @@ class Teacher(ormar.Model):
metadata = metadata
database = database
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)
schoolclass = ormar.ForeignKey(SchoolClass)
category = ormar.ForeignKey(Category, nullable=True)
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100)
schoolclass: Optional[SchoolClass] = ormar.ForeignKey(SchoolClass)
category: Optional[Category] = ormar.ForeignKey(Category, nullable=True)
@pytest.fixture(scope="module")

View File

@ -18,8 +18,8 @@ class Album(ormar.Model):
metadata = metadata
database = database
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100)
class Track(ormar.Model):
@ -28,10 +28,10 @@ class Track(ormar.Model):
metadata = metadata
database = database
id = ormar.Integer(primary_key=True)
album = ormar.ForeignKey(Album)
title = ormar.String(max_length=100)
position = ormar.Integer()
id: int = ormar.Integer(primary_key=True)
album: Optional[Album] = ormar.ForeignKey(Album)
title: str = ormar.String(max_length=100)
position: int = ormar.Integer()
class Cover(ormar.Model):
@ -40,9 +40,9 @@ class Cover(ormar.Model):
metadata = metadata
database = database
id = ormar.Integer(primary_key=True)
album = ormar.ForeignKey(Album, related_name="cover_pictures")
title = ormar.String(max_length=100)
id: int = ormar.Integer(primary_key=True)
album: Optional[Album] = ormar.ForeignKey(Album, related_name="cover_pictures")
title: str = ormar.String(max_length=100)
class Organisation(ormar.Model):
@ -51,8 +51,8 @@ class Organisation(ormar.Model):
metadata = metadata
database = database
id = ormar.Integer(primary_key=True)
ident = ormar.String(max_length=100, choices=["ACME Ltd", "Other ltd"])
id: int = ormar.Integer(primary_key=True)
ident: str = ormar.String(max_length=100, choices=["ACME Ltd", "Other ltd"])
class Team(ormar.Model):
@ -61,9 +61,9 @@ class Team(ormar.Model):
metadata = metadata
database = database
id = ormar.Integer(primary_key=True)
org = ormar.ForeignKey(Organisation)
name = ormar.String(max_length=100)
id: int = ormar.Integer(primary_key=True)
org: Optional[Organisation] = ormar.ForeignKey(Organisation)
name: str = ormar.String(max_length=100)
class Member(ormar.Model):
@ -72,9 +72,9 @@ class Member(ormar.Model):
metadata = metadata
database = database
id = ormar.Integer(primary_key=True)
team = ormar.ForeignKey(Team)
email = ormar.String(max_length=100)
id: int = ormar.Integer(primary_key=True)
team: Optional[Team] = ormar.ForeignKey(Team)
email: str = ormar.String(max_length=100)
@pytest.fixture(autouse=True, scope="module")

View File

@ -21,8 +21,8 @@ class Model(ormar.Model):
metadata = metadata
database = database
id = ormar.String(primary_key=True, default=key, max_length=8)
name = ormar.String(max_length=32)
id: str = ormar.String(primary_key=True, default=key, max_length=8)
name: str = ormar.String(max_length=32)
@pytest.fixture(autouse=True, scope="function")

View File

@ -18,10 +18,10 @@ class Book(ormar.Model):
metadata = metadata
database = database
id = ormar.Integer(primary_key=True)
title = ormar.String(max_length=200)
author = ormar.String(max_length=100)
genre = ormar.String(
id: int = ormar.Integer(primary_key=True)
title: str = ormar.String(max_length=200)
author: str = ormar.String(max_length=100)
genre: str = ormar.String(
max_length=100,
default="Fiction",
choices=["Fiction", "Adventure", "Historic", "Fantasy"],
@ -34,9 +34,9 @@ class ToDo(ormar.Model):
metadata = metadata
database = database
id = ormar.Integer(primary_key=True)
text = ormar.String(max_length=500)
completed = ormar.Boolean(default=False)
id: int = ormar.Integer(primary_key=True)
text: str = ormar.String(max_length=500)
completed: bool = ormar.Boolean(default=False)
class Category(ormar.Model):
@ -45,8 +45,8 @@ class Category(ormar.Model):
metadata = metadata
database = database
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=500)
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=500)
class Note(ormar.Model):
@ -55,9 +55,9 @@ class Note(ormar.Model):
metadata = metadata
database = database
id = ormar.Integer(primary_key=True)
text = ormar.String(max_length=500)
category = ormar.ForeignKey(Category)
id: int = ormar.Integer(primary_key=True)
text: str = ormar.String(max_length=500)
category: Optional[Category] = ormar.ForeignKey(Category)
@pytest.fixture(autouse=True, scope="module")

View File

@ -18,8 +18,8 @@ class Department(ormar.Model):
metadata = metadata
database = database
id = ormar.Integer(primary_key=True, autoincrement=False)
name = ormar.String(max_length=100)
id: int = ormar.Integer(primary_key=True, autoincrement=False)
name: str = ormar.String(max_length=100)
class SchoolClass(ormar.Model):
@ -28,9 +28,9 @@ class SchoolClass(ormar.Model):
metadata = metadata
database = database
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)
department = ormar.ForeignKey(Department, nullable=False)
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100)
department: Optional[Department] = ormar.ForeignKey(Department, nullable=False)
class Category(ormar.Model):
@ -39,8 +39,8 @@ class Category(ormar.Model):
metadata = metadata
database = database
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100)
class Student(ormar.Model):
@ -49,10 +49,10 @@ class Student(ormar.Model):
metadata = metadata
database = database
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)
schoolclass = ormar.ForeignKey(SchoolClass)
category = ormar.ForeignKey(Category, nullable=True)
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100)
schoolclass: Optional[SchoolClass] = ormar.ForeignKey(SchoolClass)
category: Optional[Category] = ormar.ForeignKey(Category, nullable=True)
class Teacher(ormar.Model):
@ -61,10 +61,10 @@ class Teacher(ormar.Model):
metadata = metadata
database = database
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)
schoolclass = ormar.ForeignKey(SchoolClass)
category = ormar.ForeignKey(Category, nullable=True)
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100)
schoolclass: Optional[SchoolClass] = ormar.ForeignKey(SchoolClass)
category: Optional[Category] = ormar.ForeignKey(Category, nullable=True)
@pytest.fixture(scope="module")

View File

@ -18,9 +18,9 @@ class Company(ormar.Model):
metadata = metadata
database = database
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100, nullable=False)
founded = ormar.Integer(nullable=True)
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100, nullable=False)
founded: int = ormar.Integer(nullable=True)
class Car(ormar.Model):
@ -29,13 +29,13 @@ class Car(ormar.Model):
metadata = metadata
database = database
id = ormar.Integer(primary_key=True)
manufacturer = ormar.ForeignKey(Company)
name = ormar.String(max_length=100)
year = ormar.Integer(nullable=True)
gearbox_type = ormar.String(max_length=20, nullable=True)
gears = ormar.Integer(nullable=True)
aircon_type = ormar.String(max_length=20, nullable=True)
id: int = ormar.Integer(primary_key=True)
manufacturer: Optional[Company] = ormar.ForeignKey(Company)
name: str = ormar.String(max_length=100)
year: int = ormar.Integer(nullable=True)
gearbox_type: str = ormar.String(max_length=20, nullable=True)
gears: int = ormar.Integer(nullable=True)
aircon_type: str = ormar.String(max_length=20, nullable=True)
@pytest.fixture(autouse=True, scope="module")

View File

@ -20,11 +20,11 @@ class Product(ormar.Model):
metadata = metadata
database = database
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)
company = ormar.String(max_length=200, server_default="Acme")
sort_order = ormar.Integer(server_default=text("10"))
created = ormar.DateTime(server_default=func.now())
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100)
company: str = ormar.String(max_length=200, server_default="Acme")
sort_order: int = ormar.Integer(server_default=text("10"))
created: datetime = ormar.DateTime(server_default=func.now())
@pytest.fixture(scope="module")

View File

@ -21,9 +21,9 @@ class Product(ormar.Model):
database = database
constraints = [ormar.UniqueColumns("name", "company")]
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)
company = ormar.String(max_length=200)
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100)
company: str = ormar.String(max_length=200)
@pytest.fixture(scope="module")