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

feat: add hash to config classes #358

Merged
merged 2 commits into from
Jan 21, 2025
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
6 changes: 6 additions & 0 deletions advanced_alchemy/config/asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ class SQLAlchemyAsyncConfig(GenericSQLAlchemyConfig[AsyncEngine, AsyncSession, a
The configuration options are documented in the Alembic documentation.
"""

def __hash__(self) -> int:
return super().__hash__()

def __eq__(self, other: object) -> bool:
return super().__eq__(other)

@asynccontextmanager
async def get_session(
self,
Expand Down
9 changes: 9 additions & 0 deletions advanced_alchemy/config/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from dataclasses import dataclass, field
from pathlib import Path
from typing import TYPE_CHECKING, Callable, ClassVar, Generic, Union, cast
from uuid import NAMESPACE_DNS, uuid3

from typing_extensions import TypeVar

Expand Down Expand Up @@ -202,6 +203,14 @@ def __post_init__(self) -> None:

event.listen(Session, "before_flush", touch_updated_timestamp)

def __hash__(self) -> int:
return hash((uuid3(NAMESPACE_DNS, str(self)), self.__class__.__name__, self.metadata, self.bind_key))

def __eq__(self, other: object) -> bool:
if not isinstance(other, type(self)):
return False
return self.__hash__() == other.__hash__()

@property
def engine_config_dict(self) -> dict[str, Any]:
"""Return the engine configuration as a dict.
Expand Down
6 changes: 6 additions & 0 deletions advanced_alchemy/config/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ class SQLAlchemySyncConfig(GenericSQLAlchemyConfig[Engine, Session, sessionmaker
The configuration options are documented in the Alembic documentation.
"""

def __hash__(self) -> int:
return super().__hash__()

def __eq__(self, other: object) -> bool:
return super().__eq__(other)

@contextmanager
def get_session(self) -> Generator[Session, None, None]:
"""Get a session context manager.
Expand Down
Loading