Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve typing and imports #644

Merged
merged 2 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ Let's define an example SQLAlchemy model:

```python
from sqlalchemy import Column, Integer, String, create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import declarative_base


Base = declarative_base()
Expand Down
2 changes: 1 addition & 1 deletion docs/authentication.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ admin = Admin(app=..., authentication_backend=authentication_backend، ...)
from sqladmin import Admin, ModelView
from sqladmin.authentication import AuthenticationBackend
from sqlalchemy import Column, Integer, String, create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import declarative_base
from starlette.applications import Starlette
from starlette.requests import Request
from starlette.responses import RedirectResponse
Expand Down
2 changes: 1 addition & 1 deletion docs/configurations.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Let's say you've defined your SQLAlchemy models like this:

```python
from sqlalchemy import Column, Integer, String, create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import declarative_base


Base = declarative_base()
Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ Let's define an example SQLAlchemy model:

```python
from sqlalchemy import Column, Integer, String, create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import declarative_base


Base = declarative_base()
Expand Down
2 changes: 1 addition & 1 deletion sqladmin/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from jinja2 import ChoiceLoader, FileSystemLoader, PackageLoader
from sqlalchemy.engine import Engine
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm.session import Session, sessionmaker
from sqlalchemy.orm import Session, sessionmaker
from starlette.applications import Starlette
from starlette.datastructures import URL, FormData, UploadFile
from starlette.exceptions import HTTPException
Expand Down
3 changes: 1 addition & 2 deletions sqladmin/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
import anyio
from sqlalchemy import Boolean, select
from sqlalchemy import inspect as sqlalchemy_inspect
from sqlalchemy.orm import ColumnProperty, RelationshipProperty
from sqlalchemy.orm.session import sessionmaker
from sqlalchemy.orm import ColumnProperty, RelationshipProperty, sessionmaker
from sqlalchemy.sql.schema import Column
from wtforms import (
BooleanField,
Expand Down
7 changes: 3 additions & 4 deletions sqladmin/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@

from sqlalchemy import Column, inspect
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import RelationshipProperty
from sqlalchemy.orm.session import sessionmaker
from sqlalchemy.orm import RelationshipProperty, sessionmaker

from sqladmin._types import MODEL_PROPERTY

Expand Down Expand Up @@ -180,7 +179,7 @@ def get_primary_key(model: type) -> Column:
return pks[0]


def get_primary_keys(model: type) -> Tuple[Column, ...]:
def get_primary_keys(model: Any) -> Tuple[Column, ...]:
return tuple(inspect(model).mapper.primary_key)


Expand Down Expand Up @@ -227,7 +226,7 @@ def _object_identifier_parts(id_string: str, model: type) -> Tuple[str, ...]:
return tuple(v.replace(r"\;", ";").replace(r"\\", "\\") for v in values)


def object_identifier_values(id_string: str, model: type) -> tuple:
def object_identifier_values(id_string: str, model: Any) -> tuple:
values = []
pks = get_primary_keys(model)
for pk, part in zip(pks, _object_identifier_parts(id_string, model)):
Expand Down
3 changes: 1 addition & 2 deletions sqladmin/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
import anyio
from sqlalchemy import Column, String, asc, cast, desc, func, inspect, or_
from sqlalchemy.exc import NoInspectionAvailable
from sqlalchemy.orm import joinedload
from sqlalchemy.orm.session import sessionmaker
from sqlalchemy.orm import joinedload, sessionmaker
from sqlalchemy.sql.elements import ClauseElement
from sqlalchemy.sql.expression import Select, select
from starlette.datastructures import URL
Expand Down
3 changes: 1 addition & 2 deletions tests/test_ajax.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
from httpx import AsyncClient
from sqlalchemy import Column, ForeignKey, Integer, String, select
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship, selectinload, sessionmaker
from sqlalchemy.orm import declarative_base, relationship, selectinload, sessionmaker
from starlette.applications import Starlette

from sqladmin import Admin, ModelView
Expand Down
2 changes: 1 addition & 1 deletion tests/test_application.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from sqlalchemy import Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import declarative_base
from starlette.applications import Starlette
from starlette.datastructures import MutableHeaders
from starlette.middleware import Middleware
Expand Down
2 changes: 1 addition & 1 deletion tests/test_base_view.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Generator

import pytest
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import declarative_base
from starlette.applications import Starlette
from starlette.requests import Request
from starlette.testclient import TestClient
Expand Down
2 changes: 1 addition & 1 deletion tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import pytest
from sqlalchemy import Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import declarative_base
from wtforms import Form

from sqladmin.fields import (
Expand Down
3 changes: 1 addition & 2 deletions tests/test_file_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
from httpx import AsyncClient
from sqlalchemy import Column, Integer, select
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from sqlalchemy.orm import declarative_base, sessionmaker
from starlette.applications import Starlette

from sqladmin import Admin, ModelView
Expand Down
10 changes: 7 additions & 3 deletions tests/test_forms/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@
)
from sqlalchemy.dialects.postgresql import ARRAY, INET, MACADDR, UUID
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import ColumnProperty, composite, relationship
from sqlalchemy.orm.session import sessionmaker
from sqlalchemy.orm import (
ColumnProperty,
composite,
declarative_base,
relationship,
sessionmaker,
)
from wtforms import BooleanField, Field, Form, IntegerField, StringField, TimeField
from wtforms.fields.core import UnboundField

Expand Down
4 changes: 1 addition & 3 deletions tests/test_forms/test_multi_pk_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
import pytest
from sqlalchemy import Column, DateTime, ForeignKey, Integer, String
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
from sqlalchemy.orm.session import sessionmaker
from sqlalchemy.orm import declarative_base, relationship, sessionmaker

from sqladmin.forms import get_model_form
from tests.common import async_engine as engine
Expand Down
2 changes: 1 addition & 1 deletion tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import pytest
from sqlalchemy import Column, ForeignKey, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import declarative_base

from sqladmin.helpers import (
get_object_identifier,
Expand Down
3 changes: 1 addition & 2 deletions tests/test_integrations/test_sqlalchemy_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
import pytest
from sqlalchemy import Column, Integer
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm.session import sessionmaker
from sqlalchemy.orm import declarative_base, sessionmaker
from sqlalchemy_utils import (
ArrowType,
ChoiceType,
Expand Down
2 changes: 1 addition & 1 deletion tests/test_integrations/test_sqlmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
except ImportError:
pytest.skip("SQLModel support for SQLAlchemy v2.", allow_module_level=True)

from sqlalchemy.orm.session import sessionmaker
from sqlalchemy.orm import sessionmaker

from sqladmin.forms import get_model_form
from tests.common import sync_engine as engine
Expand Down
2 changes: 1 addition & 1 deletion tests/test_menu.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from sqlalchemy import Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import declarative_base
from starlette.requests import Request

from sqladmin import ModelView
Expand Down
3 changes: 1 addition & 2 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
from markupsafe import Markup
from sqlalchemy import Boolean, Column, Enum, ForeignKey, Integer, String
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship, sessionmaker
from sqlalchemy.orm import declarative_base, relationship, sessionmaker
from sqlalchemy.sql.expression import Select
from starlette.applications import Starlette
from starlette.requests import Request
Expand Down
3 changes: 1 addition & 2 deletions tests/test_models_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@

import pytest
from sqlalchemy import Column, Integer
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from sqlalchemy.orm import declarative_base, sessionmaker
from starlette.applications import Starlette
from starlette.requests import Request
from starlette.responses import RedirectResponse, Response
Expand Down
3 changes: 1 addition & 2 deletions tests/test_views/test_multi_pk_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

import pytest
from sqlalchemy import Column, ForeignKey, ForeignKeyConstraint, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship, sessionmaker
from sqlalchemy.orm import declarative_base, relationship, sessionmaker
from starlette.applications import Starlette
from starlette.testclient import TestClient

Expand Down
3 changes: 1 addition & 2 deletions tests/test_views/test_view_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
select,
)
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship, selectinload, sessionmaker
from sqlalchemy.orm import declarative_base, relationship, selectinload, sessionmaker
from starlette.applications import Starlette
from starlette.requests import Request

Expand Down
3 changes: 1 addition & 2 deletions tests/test_views/test_view_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
func,
select,
)
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship, selectinload, sessionmaker
from sqlalchemy.orm import declarative_base, relationship, selectinload, sessionmaker
from starlette.applications import Starlette
from starlette.requests import Request
from starlette.testclient import TestClient
Expand Down