Skip to content

Commit 9d63657

Browse files
committed
📝 Update type alias handling for Python 3.12 compatibility
1 parent 105a8e5 commit 9d63657

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

sqlmodel/_compat.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
Optional,
1616
Set,
1717
Type,
18-
TypeAliasType,
1918
TypeVar,
2019
Union,
2120
)
@@ -75,6 +74,9 @@ def partial_init() -> Generator[None, None, None]:
7574

7675

7776
if IS_PYDANTIC_V2:
77+
if sys.version_info >= (3, 12):
78+
from typing import TypeAliasType
79+
7880
from annotated_types import MaxLen
7981
from pydantic import ConfigDict as BaseConfig
8082
from pydantic._internal._fields import PydanticMetadata
@@ -204,7 +206,7 @@ def get_sa_type_from_type_annotation(annotation: Any) -> Any:
204206
# Resolve Optional fields
205207
if annotation is None:
206208
raise ValueError("Missing field type")
207-
if isinstance(annotation, TypeAliasType):
209+
if sys.version_info >= (3, 12) and isinstance(annotation, TypeAliasType):
208210
annotation = annotation.__value__
209211
origin = get_origin(annotation)
210212
if origin is None:

tests/conftest.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,6 @@ def print_mock_fixture() -> Generator[PrintMock, None, None]:
9393
needs_py310 = pytest.mark.skipif(
9494
sys.version_info < (3, 10), reason="requires python3.10+"
9595
)
96+
needs_py312 = pytest.mark.skipif(
97+
sys.version_info < (3, 12), reason="requires python3.12+"
98+
)

tests/test_field_sa_type.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
from sys import version_info
21
from typing import Annotated, TypeAlias
32

4-
import pytest
53
from sqlmodel import Field, SQLModel
64

5+
from tests.conftest import needs_py312
76

8-
@pytest.mark.skipif(version_info[1] < 12, reason="Language feature of Python 3.12+")
7+
8+
@needs_py312
99
def test_sa_type_1() -> None:
1010
Type1 = str
1111

@@ -14,7 +14,7 @@ class Hero1(SQLModel, table=True):
1414
weapon: Type1 = "sword"
1515

1616

17-
@pytest.mark.skipif(version_info[1] < 12, reason="Language feature of Python 3.12+")
17+
@needs_py312
1818
def test_sa_type_2() -> None:
1919
Type2 = Annotated[str, "Just a comment"]
2020

@@ -26,7 +26,7 @@ class Hero(SQLModel, table=True):
2626
Type3: TypeAlias = str
2727

2828

29-
@pytest.mark.skipif(version_info[1] < 12, reason="Language feature of Python 3.12+")
29+
@needs_py312
3030
def test_sa_type_3() -> None:
3131
class Hero(SQLModel, table=True):
3232
pk: int = Field(primary_key=True)
@@ -36,14 +36,14 @@ class Hero(SQLModel, table=True):
3636
Type4: TypeAlias = Annotated[str, "Just a comment"]
3737

3838

39-
@pytest.mark.skipif(version_info[1] < 12, reason="Language feature of Python 3.12+")
39+
@needs_py312
4040
def test_sa_type_4() -> None:
4141
class Hero(SQLModel, table=True):
4242
pk: int = Field(primary_key=True)
4343
weapon: Type4 = "sword"
4444

4545

46-
@pytest.mark.skipif(version_info[1] < 12, reason="Language feature of Python 3.12+")
46+
@needs_py312
4747
def test_sa_type_5() -> None:
4848
type Type5 = str
4949

@@ -52,7 +52,7 @@ class Hero(SQLModel, table=True):
5252
weapon: Type5 = "sword"
5353

5454

55-
@pytest.mark.skipif(version_info[1] < 12, reason="Language feature of Python 3.12+")
55+
@needs_py312
5656
def test_sa_type_6() -> None:
5757
type Type6 = Annotated[str, "Just a comment"]
5858

0 commit comments

Comments
 (0)