From 045f9bcc8aa74c2d888b7ee856eae7006f9311fe Mon Sep 17 00:00:00 2001 From: honglei Date: Wed, 16 Aug 2023 23:18:55 +0800 Subject: [PATCH] add test for pydantic.AnyURL --- sqlmodel/main.py | 5 ++--- tests/test_nullable.py | 5 +++++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/sqlmodel/main.py b/sqlmodel/main.py index a593a53..7e809f3 100644 --- a/sqlmodel/main.py +++ b/sqlmodel/main.py @@ -666,15 +666,14 @@ def _is_field_noneable(field: FieldInfo) -> bool: return field.nullable if not field.is_required(): default = getattr(field, "original_default", field.default) - if default is PydanticUndefined: - return False if field.annotation is None or field.annotation is NoneType: return True if _is_optional_or_union(field.annotation): for base in get_args(field.annotation): if base is NoneType: return True - + if default is PydanticUndefined: + return False return False return False diff --git a/tests/test_nullable.py b/tests/test_nullable.py index 1c8b37b..2509da0 100644 --- a/tests/test_nullable.py +++ b/tests/test_nullable.py @@ -3,6 +3,9 @@ from typing import Optional import pytest from sqlalchemy.exc import IntegrityError from sqlmodel import Field, Session, SQLModel, create_engine +from typing_extensions import Annotated +from pydantic import AnyUrl, UrlConstraints +MoveSharedUrl = Annotated[AnyUrl, UrlConstraints(max_length=512, allowed_schemes=['smb', 'ftp','file'])] def test_nullable_fields(clear_sqlmodel, caplog): @@ -49,6 +52,7 @@ 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: MoveSharedUrl | None = Field(description="") engine = create_engine("sqlite://", echo=True) SQLModel.metadata.create_all(engine) @@ -77,6 +81,7 @@ 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)," in create_table_log # Test for regression in https://github.com/tiangolo/sqlmodel/issues/420