From e0d32fb27ae04d60e206cedef28d77439d4f29af Mon Sep 17 00:00:00 2001 From: honglei Date: Fri, 25 Aug 2023 21:02:34 +0800 Subject: [PATCH] try fix py3.8/test_nullable.py --- sqlmodel/main.py | 5 ++++- tests/test_nullable.py | 12 ++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/sqlmodel/main.py b/sqlmodel/main.py index dd41cc9..7d94c40 100644 --- a/sqlmodel/main.py +++ b/sqlmodel/main.py @@ -467,11 +467,14 @@ def get_sqlalchemy_type(field: FieldInfo) -> Any: meta = field.metadata[0] return AutoString(length=meta.max_length) - if get_origin(type_) is Annotated: + org_type = get_origin(type_) + if org_type is Annotated: type2 = get_args(type_)[0] if type2 is pydantic.AnyUrl: meta = get_args(type_)[1] return AutoString(length=meta.max_length) + elif org_type is pydantic.AnyUrl: + return AutoString(type_.__metadata__[0].max_length) # The 3rd is PydanticGeneralMetadata metadata = _get_field_metadata(field) diff --git a/tests/test_nullable.py b/tests/test_nullable.py index 3117812..22e1e1b 100644 --- a/tests/test_nullable.py +++ b/tests/test_nullable.py @@ -57,7 +57,12 @@ def test_nullable_fields(clear_sqlmodel, caplog): str_default_str_nullable: str = Field(default="default", nullable=True) str_default_ellipsis_non_nullable: str = Field(default=..., nullable=False) str_default_ellipsis_nullable: str = Field(default=..., nullable=True) - annotated_any_url: Optional[MoveSharedUrl] = Field(description="") + optional_url: Optional[MoveSharedUrl] = Field(default=None, description="") + url: MoveSharedUrl + annotated_url: Annotated[MoveSharedUrl, Field(description="")] + annotated_optional_url: Annotated[ + Optional[MoveSharedUrl], Field(description="") + ] = None engine = create_engine("sqlite://", echo=True) SQLModel.metadata.create_all(engine) @@ -88,7 +93,10 @@ def test_nullable_fields(clear_sqlmodel, caplog): assert "str_default_str_nullable VARCHAR," in create_table_log assert "str_default_ellipsis_non_nullable VARCHAR NOT NULL," in create_table_log assert "str_default_ellipsis_nullable VARCHAR," in create_table_log - assert "annotated_any_url VARCHAR(512) NOT NULL" in create_table_log + assert "optional_url VARCHAR(512), " in create_table_log + assert "url VARCHAR(512) NOT NULL," in create_table_log + assert "annotated_url VARCHAR(512) NOT NULL," in create_table_log + assert "annotated_optional_url VARCHAR(512)," in create_table_log # Test for regression in https://github.com/tiangolo/sqlmodel/issues/420