Skip to content

Commit

Permalink
feat: add hash to config classes (#358)
Browse files Browse the repository at this point in the history
Implement `__hash__` methods for configuration classes
  • Loading branch information
tataraba authored Jan 21, 2025
1 parent 114b0cd commit 1de76cf
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
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

0 comments on commit 1de76cf

Please sign in to comment.