Skip to content

Commit

Permalink
feat: patch FieldInfo for comment support
Browse files Browse the repository at this point in the history
- Introduce a patch to SQLAlchemy's FieldInfo to support comments, ensuring `sa_column_kwargs` is initialized when not provided.
- Enable previously disabled test cases related to column comments by applying the new patch.

Generated-by: aiautocommit
  • Loading branch information
iloveitaly committed Feb 7, 2025
1 parent 3fd67e6 commit e85d154
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
2 changes: 2 additions & 0 deletions activemodel/field_comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
from pydantic_core import PydanticUndefined
from sqlmodel.main import SQLModelMetaclass

# this line is very important: it patches FieldInfo to support comments
from . import field_info_patch # noqa: F401
from .utils import logger


Expand Down
29 changes: 29 additions & 0 deletions activemodel/field_info_patch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from sqlmodel.main import FieldInfo


def hash_function_code(func):
"get sha of a function to easily assert that it hasn't changed"

import hashlib
import inspect

source = inspect.getsource(func)
return hashlib.sha256(source.encode()).hexdigest()


assert (
hash_function_code(FieldInfo.__init__)
== "0d947d2aace56b61b7f7bcbea079488b5fb9a1eb9671536c57b06013799e19b0"
)

original_init = FieldInfo.__init__


def patched_init(self, *args, **kwargs):
if "sa_column" not in kwargs and "sa_column_kwargs" not in kwargs:
kwargs["sa_column_kwargs"] = {}

original_init(self, *args, **kwargs)


FieldInfo.__init__ = patched_init
5 changes: 2 additions & 3 deletions test/comments_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,8 @@ def get_column_comment(table_name, column_name):

def test_column_comments(create_and_wipe_database):
fields_to_check = [
# TODO we need to patch FieldInfo to get these to work
# "a_string_field_without_field",
# "a_string_field_with_field",
"a_string_field_without_field",
"a_string_field_with_field",
"field_with_sa_column",
"field_with_sa_column_args",
"field_with_empty_sa_column_args",
Expand Down

0 comments on commit e85d154

Please sign in to comment.