Skip to content

Commit f056495

Browse files
authored
Merge pull request #47 from febus982/pydantic_v2_part2
Further pydantic v2 support
2 parents 0c17771 + 7e12e80 commit f056495

File tree

13 files changed

+46
-27
lines changed

13 files changed

+46
-27
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ Example:
7272
bind = sa_manager.get_bind()
7373

7474

75-
class MyModel(bind.model_declarative_base):
75+
class MyModel(bind.declarative_base):
7676
pass
7777

7878

@@ -144,7 +144,7 @@ The `SQLAlchemyRepository` and `SQLAlchemyAsyncRepository` class can be used dir
144144
from sqlalchemy_bind_manager.repository import SQLAlchemyRepository
145145

146146

147-
class MyModel(model_declarative_base):
147+
class MyModel(declarative_base):
148148
pass
149149

150150
# Direct usage

docs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ config = SQLAlchemyConfig(
6161
sa_manager = SQLAlchemyBindManager(config)
6262

6363
# Declare a model
64-
class MyModel(sa_manager.get_bind().model_declarative_base):
64+
class MyModel(sa_manager.get_bind().declarative_base):
6565
id: Mapped[int] = mapped_column(primary_key=True)
6666
name: Mapped[str] = mapped_column(String(30))
6767

docs/manager/alembic/env.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
sa_manager = SQLAlchemyBindManager(config=bind_config)
2929

30-
class BookModel(sa_manager.get_bind().model_declarative_base):
30+
class BookModel(sa_manager.get_bind().declarative_base):
3131
id = Column(Integer)
3232
title = Column(String)
3333
################################################################

docs/manager/config.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Once the bind manager is initialised we can retrieve and use the SQLAlchemyBind
2323
The `SQLAlchemyBind` class has the following attributes:
2424

2525
* `engine`: The initialised SQLALchemy `Engine`
26-
* `model_declarative_base`: A base class that can be used to create [declarative models](https://docs.sqlalchemy.org/en/14/orm/mapping_styles.html#declarative-mapping)
26+
* `declarative_base`: A base class that can be used to create [declarative models](https://docs.sqlalchemy.org/en/14/orm/mapping_styles.html#declarative-mapping)
2727
* `registry_mapper`: The `registry` associated with the `engine`. It can be used with Alembic or to setup [imperative mapping](https://docs.sqlalchemy.org/en/14/orm/mapping_styles.html#imperative-mapping)
2828
* `session_class`: The class built by [sessionmaker()](https://docs.sqlalchemy.org/en/14/orm/session_api.html#sqlalchemy.orm.sessionmaker), either `Session` or `AsyncSession`
2929

docs/manager/models.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ from sqlalchemy import String
1010

1111
bind = sa_manager.get_bind()
1212

13-
class MyModel(bind.model_declarative_base):
13+
class MyModel(bind.declarative_base):
1414
id: Mapped[int] = mapped_column(primary_key=True)
1515
name: Mapped[str] = mapped_column(String(30))
1616
```

docs/repository/usage.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ The `SQLAlchemyRepository` and `SQLAlchemyAsyncRepository` class can be used dir
66
from sqlalchemy_bind_manager.repository import SQLAlchemyRepository
77

88

9-
class MyModel(model_declarative_base):
9+
class MyModel(declarative_base):
1010
pass
1111

1212
# Direct usage

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ build-backend = "poetry_dynamic_versioning.backend"
3939

4040
[tool.poetry.dependencies]
4141
python = ">=3.8,<3.12"
42-
pydantic = ">=1.10.2, <3.0.0"
42+
pydantic = "^2.1.1"
4343
SQLAlchemy = { version = "~2.0.0", extras = ["asyncio", "mypy"] }
4444

4545
[tool.poetry.group.dev]

sqlalchemy_bind_manager/_bind_manager.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import Mapping, MutableMapping, Union
22

3-
from pydantic import BaseModel
3+
from pydantic import BaseModel, ConfigDict
44
from sqlalchemy import MetaData, create_engine
55
from sqlalchemy.engine import Engine
66
from sqlalchemy.ext.asyncio import (
@@ -10,7 +10,7 @@
1010
create_async_engine,
1111
)
1212
from sqlalchemy.orm import Session, sessionmaker
13-
from sqlalchemy.orm.decl_api import registry
13+
from sqlalchemy.orm.decl_api import DeclarativeMeta, registry
1414

1515
from sqlalchemy_bind_manager.exceptions import (
1616
InvalidConfig,
@@ -32,22 +32,20 @@ class SQLAlchemyAsyncConfig(BaseModel):
3232

3333
class SQLAlchemyBind(BaseModel):
3434
engine: Engine
35-
model_declarative_base: type
35+
declarative_base: DeclarativeMeta
3636
registry_mapper: registry
3737
session_class: sessionmaker[Session]
3838

39-
class Config:
40-
arbitrary_types_allowed = True
39+
model_config = ConfigDict(arbitrary_types_allowed=True)
4140

4241

4342
class SQLAlchemyAsyncBind(BaseModel):
4443
engine: AsyncEngine
45-
model_declarative_base: type
44+
declarative_base: DeclarativeMeta
4645
registry_mapper: registry
4746
session_class: async_sessionmaker[AsyncSession]
4847

49-
class Config:
50-
arbitrary_types_allowed = True
48+
model_config = ConfigDict(arbitrary_types_allowed=True)
5149

5250

5351
_SQLAlchemyConfig = Union[
@@ -123,7 +121,7 @@ def __build_sync_bind(
123121
class_=Session,
124122
**session_options,
125123
),
126-
model_declarative_base=registry_mapper.generate_base(),
124+
declarative_base=registry_mapper.generate_base(),
127125
)
128126

129127
def __build_async_bind(
@@ -141,7 +139,7 @@ def __build_async_bind(
141139
bind=engine,
142140
**session_options,
143141
),
144-
model_declarative_base=registry_mapper.generate_base(),
142+
declarative_base=registry_mapper.generate_base(),
145143
)
146144

147145
def get_binds(self) -> Mapping[str, Union[SQLAlchemyBind, SQLAlchemyAsyncBind]]:

sqlalchemy_bind_manager/_repository/common.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from typing import Generic, List, TypeVar, Union
44

55
from pydantic import BaseModel, StrictInt, StrictStr
6-
from pydantic.generics import GenericModel
76
from sqlalchemy import asc, desc
87

98
MODEL = TypeVar("MODEL")
@@ -19,7 +18,7 @@ class PageInfo(BaseModel):
1918
has_previous_page: bool
2019

2120

22-
class PaginatedResult(GenericModel, Generic[MODEL]):
21+
class PaginatedResult(BaseModel, Generic[MODEL]):
2322
items: List[MODEL]
2423
page_info: PageInfo
2524

@@ -38,7 +37,7 @@ class CursorPageInfo(BaseModel):
3837
end_cursor: Union[CursorReference, None] = None
3938

4039

41-
class CursorPaginatedResult(GenericModel, Generic[MODEL]):
40+
class CursorPaginatedResult(BaseModel, Generic[MODEL]):
4241
items: List[MODEL]
4342
page_info: CursorPageInfo
4443

tests/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def sa_bind(request, sa_manager):
102102

103103
@pytest.fixture
104104
async def model_classes(sa_bind) -> Tuple[Type, Type]:
105-
class ParentModel(sa_bind.model_declarative_base):
105+
class ParentModel(sa_bind.declarative_base):
106106
__tablename__ = "parent_model"
107107
# required in order to access columns with server defaults
108108
# or SQL expression defaults, subsequent to a flush, without
@@ -119,7 +119,7 @@ class ParentModel(sa_bind.model_declarative_base):
119119
lazy="selectin",
120120
)
121121

122-
class ChildModel(sa_bind.model_declarative_base):
122+
class ChildModel(sa_bind.declarative_base):
123123
__tablename__ = "child_model"
124124
# required in order to access columns with server defaults
125125
# or SQL expression defaults, subsequent to a flush, without

0 commit comments

Comments
 (0)