From 358b5c2e5258b184b2f60613e8e5afb7e33e76a9 Mon Sep 17 00:00:00 2001 From: collerek Date: Sun, 1 Nov 2020 10:11:25 +0100 Subject: [PATCH] restore typing in tests and docs, remove unused metaclass code --- README.md | 10 ++--- docs/index.md | 15 +++++--- docs/models.md | 2 +- docs/queries.md | 46 +++++++++++------------ docs_src/__init__.py | 0 docs_src/fastapi/__init__.py | 0 docs_src/fastapi/docs001.py | 10 ++--- docs_src/fields/__init__.py | 0 docs_src/fields/docs001.py | 4 +- docs_src/fields/docs002.py | 16 ++++---- docs_src/fields/docs003.py | 12 +++--- docs_src/fields/docs004.py | 10 ++--- docs_src/models/__init__.py | 0 docs_src/models/docs001.py | 6 +-- docs_src/models/docs002.py | 6 +-- docs_src/models/docs003.py | 6 +-- docs_src/models/docs004.py | 8 ++-- docs_src/models/docs005.py | 13 ++++--- docs_src/models/docs006.py | 8 ++-- docs_src/models/docs007.py | 11 +++--- docs_src/models/docs008.py | 8 ++-- docs_src/models/docs009.py | 18 +++++++-- docs_src/models/docs010.py | 24 +++++++++--- docs_src/queries/__init__.py | 0 docs_src/queries/docs001.py | 12 +++--- docs_src/relations/__init__.py | 0 docs_src/relations/docs001.py | 24 ++++++------ docs_src/relations/docs002.py | 20 +++++----- ormar/__init__.py | 2 + ormar/fields/model_fields.py | 20 +++++++--- ormar/models/metaclass.py | 39 +++++++------------ tests/test_aliases.py | 26 +++++++------ tests/test_columns.py | 16 ++++---- tests/test_docs/__init__.py | 0 tests/test_fastapi_docs.py | 10 ++--- tests/test_fastapi_usage.py | 10 ++--- tests/test_foreign_keys.py | 22 +++++------ tests/test_many_to_many.py | 18 ++++----- tests/test_model_definition.py | 26 ++++++------- tests/test_models.py | 32 ++++++++-------- tests/test_more_reallife_fastapi.py | 10 ++--- tests/test_more_same_table_joins.py | 30 +++++++-------- tests/test_new_annotation_style.py | 34 ++++++++--------- tests/test_non_integer_pkey.py | 4 +- tests/test_queryset_level_methods.py | 24 ++++++------ tests/test_same_table_joins.py | 30 +++++++-------- tests/test_selecting_subset_of_columns.py | 20 +++++----- tests/test_server_default.py | 10 ++--- tests/test_unique_constraints.py | 6 +-- 49 files changed, 354 insertions(+), 324 deletions(-) create mode 100644 docs_src/__init__.py create mode 100644 docs_src/fastapi/__init__.py create mode 100644 docs_src/fields/__init__.py create mode 100644 docs_src/models/__init__.py create mode 100644 docs_src/queries/__init__.py create mode 100644 docs_src/relations/__init__.py create mode 100644 tests/test_docs/__init__.py diff --git a/README.md b/README.md index 2a5988b..c2b0daa 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/docs/index.md b/docs/index.md index 3d0f507..0b691dd 100644 --- a/docs/index.md +++ b/docs/index.md @@ -74,9 +74,12 @@ class Album(ormar.Model): tablename = "album" 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. diff --git a/docs/models.md b/docs/models.md index cb063fb..495b839 100644 --- a/docs/models.md +++ b/docs/models.md @@ -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 diff --git a/docs/queries.md b/docs/queries.md index dd6e249..18c6479 100644 --- a/docs/queries.md +++ b/docs/queries.md @@ -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) diff --git a/docs_src/__init__.py b/docs_src/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/docs_src/fastapi/__init__.py b/docs_src/fastapi/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/docs_src/fastapi/docs001.py b/docs_src/fastapi/docs001.py index efcc959..4d2087c 100644 --- a/docs_src/fastapi/docs001.py +++ b/docs_src/fastapi/docs001.py @@ -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]) diff --git a/docs_src/fields/__init__.py b/docs_src/fields/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/docs_src/fields/docs001.py b/docs_src/fields/docs001.py index a757525..dd79791 100644 --- a/docs_src/fields/docs001.py +++ b/docs_src/fields/docs001.py @@ -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: diff --git a/docs_src/fields/docs002.py b/docs_src/fields/docs002.py index 13e4544..2432856 100644 --- a/docs_src/fields/docs002.py +++ b/docs_src/fields/docs002.py @@ -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: diff --git a/docs_src/fields/docs003.py b/docs_src/fields/docs003.py index 7a7f8b9..5066f60 100644 --- a/docs_src/fields/docs003.py +++ b/docs_src/fields/docs003.py @@ -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) diff --git a/docs_src/fields/docs004.py b/docs_src/fields/docs004.py index 793dc93..8a4e1ab 100644 --- a/docs_src/fields/docs004.py +++ b/docs_src/fields/docs004.py @@ -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()) diff --git a/docs_src/models/__init__.py b/docs_src/models/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/docs_src/models/docs001.py b/docs_src/models/docs001.py index 78e2238..36d7fd9 100644 --- a/docs_src/models/docs001.py +++ b/docs_src/models/docs001.py @@ -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) diff --git a/docs_src/models/docs002.py b/docs_src/models/docs002.py index b8f206e..3450dae 100644 --- a/docs_src/models/docs002.py +++ b/docs_src/models/docs002.py @@ -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) diff --git a/docs_src/models/docs003.py b/docs_src/models/docs003.py index 92a7d49..e79bfba 100644 --- a/docs_src/models/docs003.py +++ b/docs_src/models/docs003.py @@ -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__) diff --git a/docs_src/models/docs004.py b/docs_src/models/docs004.py index f40c1f1..cc8bce8 100644 --- a/docs_src/models/docs004.py +++ b/docs_src/models/docs004.py @@ -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) diff --git a/docs_src/models/docs005.py b/docs_src/models/docs005.py index 50e3515..cd0fa9d 100644 --- a/docs_src/models/docs005.py +++ b/docs_src/models/docs005.py @@ -8,15 +8,16 @@ 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()}) + +print({x: v.__dict__ for x, v in Course.Meta.model_fields.items()}) """ Will produce: {'completed': mappingproxy({'autoincrement': False, @@ -63,4 +64,4 @@ Will produce: 'server_default': None, 'strip_whitespace': False, 'unique': False})} -""" \ No newline at end of file +""" diff --git a/docs_src/models/docs006.py b/docs_src/models/docs006.py index 7926cfb..7649c2c 100644 --- a/docs_src/models/docs006.py +++ b/docs_src/models/docs006.py @@ -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) diff --git a/docs_src/models/docs007.py b/docs_src/models/docs007.py index 751cace..2f4b7b5 100644 --- a/docs_src/models/docs007.py +++ b/docs_src/models/docs007.py @@ -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 diff --git a/docs_src/models/docs008.py b/docs_src/models/docs008.py index b7ab424..da2444c 100644 --- a/docs_src/models/docs008.py +++ b/docs_src/models/docs008.py @@ -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) diff --git a/docs_src/models/docs009.py b/docs_src/models/docs009.py index 71b1bb3..747f612 100644 --- a/docs_src/models/docs009.py +++ b/docs_src/models/docs009.py @@ -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") diff --git a/docs_src/models/docs010.py b/docs_src/models/docs010.py index dd52267..af2c9dc 100644 --- a/docs_src/models/docs010.py +++ b/docs_src/models/docs010.py @@ -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 + ) diff --git a/docs_src/queries/__init__.py b/docs_src/queries/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/docs_src/queries/docs001.py b/docs_src/queries/docs001.py index 6edaa53..850030e 100644 --- a/docs_src/queries/docs001.py +++ b/docs_src/queries/docs001.py @@ -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() \ No newline at end of file + id: int = ormar.Integer(primary_key=True) + album: Optional[Album] = ormar.ForeignKey(Album) + title: str = ormar.String(max_length=100) + position: int = ormar.Integer() diff --git a/docs_src/relations/__init__.py b/docs_src/relations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/docs_src/relations/docs001.py b/docs_src/relations/docs001.py index 6425f39..48307d2 100644 --- a/docs_src/relations/docs001.py +++ b/docs_src/relations/docs001.py @@ -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) diff --git a/docs_src/relations/docs002.py b/docs_src/relations/docs002.py index 470d9b1..8dd0566 100644 --- a/docs_src/relations/docs002.py +++ b/docs_src/relations/docs002.py @@ -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) diff --git a/ormar/__init__.py b/ormar/__init__.py index 17324ad..b121bb1 100644 --- a/ormar/__init__.py +++ b/ormar/__init__.py @@ -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", ] diff --git a/ormar/fields/model_fields.py b/ormar/fields/model_fields.py index e766aa7..f32b5d4 100644 --- a/ormar/fields/model_fields.py +++ b/ormar/fields/model_fields.py @@ -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,12 +178,20 @@ class Float(ModelFieldFactory, float): return sqlalchemy.Float() -class Boolean(ModelFieldFactory, int): - _type = bool +if TYPE_CHECKING: # pragma: nocover - @classmethod - def get_column_type(cls, **kwargs: Any) -> Any: - return sqlalchemy.Boolean() + def Boolean(**kwargs: Any) -> bool: + pass + + +else: + + class Boolean(ModelFieldFactory, int): + _type = bool + + @classmethod + def get_column_type(cls, **kwargs: Any) -> Any: + return sqlalchemy.Boolean() class DateTime(ModelFieldFactory, datetime.datetime): diff --git a/ormar/models/metaclass.py b/ormar/models/metaclass.py index 4e09af5..0190a6b 100644 --- a/ormar/models/metaclass.py +++ b/ormar/models/metaclass.py @@ -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 diff --git a/tests/test_aliases.py b/tests/test_aliases.py index 1d84bff..581b59c 100644 --- a/tests/test_aliases.py +++ b/tests/test_aliases.py @@ -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") diff --git a/tests/test_columns.py b/tests/test_columns.py index d0eb326..73cf84a 100644 --- a/tests/test_columns.py +++ b/tests/test_columns.py @@ -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") diff --git a/tests/test_docs/__init__.py b/tests/test_docs/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_fastapi_docs.py b/tests/test_fastapi_docs.py index c66cbb4..9fa503f 100644 --- a/tests/test_fastapi_docs.py +++ b/tests/test_fastapi_docs.py @@ -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) diff --git a/tests/test_fastapi_usage.py b/tests/test_fastapi_usage.py index ecf1a5f..503e582 100644 --- a/tests/test_fastapi_usage.py +++ b/tests/test_fastapi_usage.py @@ -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) diff --git a/tests/test_foreign_keys.py b/tests/test_foreign_keys.py index fc3f3e5..e7bf4e5 100644 --- a/tests/test_foreign_keys.py +++ b/tests/test_foreign_keys.py @@ -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") diff --git a/tests/test_many_to_many.py b/tests/test_many_to_many.py index bd20b1d..a414cae 100644 --- a/tests/test_many_to_many.py +++ b/tests/test_many_to_many.py @@ -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") diff --git a/tests/test_model_definition.py b/tests/test_model_definition.py index 66ced8a..5f6d360 100644 --- a/tests/test_model_definition.py +++ b/tests/test_model_definition.py @@ -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 diff --git a/tests/test_models.py b/tests/test_models.py index 84eed89..2bac6d7 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -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 diff --git a/tests/test_more_reallife_fastapi.py b/tests/test_more_reallife_fastapi.py index 1ad7425..fc3b1f7 100644 --- a/tests/test_more_reallife_fastapi.py +++ b/tests/test_more_reallife_fastapi.py @@ -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") diff --git a/tests/test_more_same_table_joins.py b/tests/test_more_same_table_joins.py index a0f712f..3fe22fe 100644 --- a/tests/test_more_same_table_joins.py +++ b/tests/test_more_same_table_joins.py @@ -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") diff --git a/tests/test_new_annotation_style.py b/tests/test_new_annotation_style.py index 6f141dc..28665a5 100644 --- a/tests/test_new_annotation_style.py +++ b/tests/test_new_annotation_style.py @@ -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") diff --git a/tests/test_non_integer_pkey.py b/tests/test_non_integer_pkey.py index 0b3d8d1..78fb179 100644 --- a/tests/test_non_integer_pkey.py +++ b/tests/test_non_integer_pkey.py @@ -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") diff --git a/tests/test_queryset_level_methods.py b/tests/test_queryset_level_methods.py index 86e6a1e..5ea5443 100644 --- a/tests/test_queryset_level_methods.py +++ b/tests/test_queryset_level_methods.py @@ -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") diff --git a/tests/test_same_table_joins.py b/tests/test_same_table_joins.py index 9ca0654..2375613 100644 --- a/tests/test_same_table_joins.py +++ b/tests/test_same_table_joins.py @@ -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") diff --git a/tests/test_selecting_subset_of_columns.py b/tests/test_selecting_subset_of_columns.py index 7cf0173..389648b 100644 --- a/tests/test_selecting_subset_of_columns.py +++ b/tests/test_selecting_subset_of_columns.py @@ -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") diff --git a/tests/test_server_default.py b/tests/test_server_default.py index f88a062..d01fc40 100644 --- a/tests/test_server_default.py +++ b/tests/test_server_default.py @@ -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") diff --git a/tests/test_unique_constraints.py b/tests/test_unique_constraints.py index e39cbbc..3126d2a 100644 --- a/tests/test_unique_constraints.py +++ b/tests/test_unique_constraints.py @@ -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")