-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
117 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,4 @@ select = | |
W, # flake8-pycodestyle | ||
ignore= | ||
# conflict with black formatter | ||
W503,E203, | ||
W503,E203,E704, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
"""UUID type.""" | ||
|
||
from __future__ import annotations | ||
|
||
from typing import Any, Callable, TypeVar | ||
from uuid import UUID as PyUUID | ||
|
||
from sqlalchemy import types as sqltypes | ||
from sqlalchemy.engine import Dialect | ||
|
||
_RET = TypeVar("_RET", str, PyUUID) | ||
|
||
|
||
class Uuid(sqltypes.Uuid[_RET]): | ||
|
||
def __init__( | ||
self, | ||
as_uuid: bool = True, | ||
native_uuid: bool = True, | ||
as_varbinary: bool = False, | ||
) -> None: | ||
super().__init__(as_uuid, native_uuid) # type:ignore | ||
self.as_varbinary = as_varbinary | ||
|
||
def bind_processor(self, dialect: Dialect) -> Callable[[Any | None], Any | None]: | ||
if not self.as_varbinary: | ||
return super().bind_processor(dialect) | ||
|
||
def process(value: Any | None) -> Any | None: | ||
if value is None: | ||
return value | ||
uuid = value if isinstance(value, PyUUID) else PyUUID(value) | ||
return uuid.bytes | ||
|
||
return process | ||
|
||
def result_processor( | ||
self, dialect: Dialect, coltype: Any | ||
) -> Callable[[Any | None], Any | None]: | ||
if not self.as_varbinary: | ||
return super().result_processor(dialect, coltype) | ||
|
||
def process(value: Any | None) -> Any | None: | ||
if value is None: | ||
return value | ||
if self.as_uuid: | ||
return PyUUID(bytes=value.tobytes()) | ||
return str(PyUUID(bytes=value.tobytes())) | ||
|
||
return process |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters