Skip to content

Commit 405d13d

Browse files
authored
♻️ Replace the dataclass_transform shim with typing_extensions (#2097)
1 parent 648d757 commit 405d13d

4 files changed

Lines changed: 29 additions & 15 deletions

File tree

‎.pre-commit-config.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ repos:
3737
- id: local-ty
3838
name: ty check
3939
entry: >-
40-
uv run ty check sqlmodel tests/test_field_sa_type.py
40+
uv run ty check sqlmodel tests/test_dataclass_transform.py tests/test_field_sa_type.py
4141
tests/test_select_typing.py
4242
require_serial: true
4343
language: unsupported

‎scripts/lint.sh‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
set -e
44
set -x
55

6-
ty check sqlmodel tests/test_field_sa_type.py tests/test_select_typing.py
6+
ty check sqlmodel tests/test_dataclass_transform.py tests/test_field_sa_type.py tests/test_select_typing.py
77
ruff check sqlmodel tests docs_src scripts
88
ruff format sqlmodel tests docs_src scripts --check

‎sqlmodel/main.py‎

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
from sqlalchemy.sql.schema import MetaData
5353
from sqlalchemy.sql.sqltypes import LargeBinary, Time, Uuid
5454
from sqlalchemy.types import TypeEngine
55-
from typing_extensions import deprecated
55+
from typing_extensions import dataclass_transform, deprecated
5656

5757
from ._compat import (
5858
PYDANTIC_MINOR_VERSION,
@@ -82,7 +82,6 @@
8282
from pydantic_core import PydanticUndefined as Undefined
8383
from pydantic_core import PydanticUndefinedType as UndefinedType
8484

85-
_T = TypeVar("_T")
8685
NoArgAnyCallable = Callable[[], Any]
8786
IncEx: TypeAlias = (
8887
set[int]
@@ -101,16 +100,6 @@
101100
)
102101

103102

104-
def __dataclass_transform__(
105-
*,
106-
eq_default: bool = True,
107-
order_default: bool = False,
108-
kw_only_default: bool = False,
109-
field_descriptors: tuple[type | Callable[..., Any], ...] = (()),
110-
) -> Callable[[_T], _T]:
111-
return lambda a: a
112-
113-
114103
class FieldInfo(PydanticFieldInfo): # ty: ignore[subclass-of-final-class]
115104
# mypy - ignore that PydanticFieldInfo is @final
116105
def __init__(self, default: Any = Undefined, **kwargs: Any) -> None:
@@ -565,7 +554,7 @@ def Relationship(
565554
return relationship_info
566555

567556

568-
@__dataclass_transform__(kw_only_default=True, field_descriptors=(Field, FieldInfo))
557+
@dataclass_transform(kw_only_default=True, field_specifiers=(Field, FieldInfo))
569558
class SQLModelMetaclass(ModelMetaclass, DeclarativeMeta):
570559
__sqlmodel_relationships__: dict[str, RelationshipInfo]
571560
model_config: SQLModelConfig

‎tests/test_dataclass_transform.py‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from typing import TYPE_CHECKING
2+
3+
from sqlmodel import Field, SQLModel
4+
5+
6+
def test_field_parameters() -> None:
7+
class Hero(SQLModel):
8+
name: str = Field()
9+
secret_name: str = Field(alias="secretName")
10+
age: int = Field(default=42)
11+
tags: list[str] = Field(default_factory=list)
12+
13+
hero = Hero(name="Deadpond", secretName="Dive Wilson")
14+
other = Hero(name="Spider-Boy", secretName="Pedro Parqueador")
15+
16+
assert hero.name == "Deadpond"
17+
assert hero.secret_name == "Dive Wilson"
18+
assert hero.age == 42
19+
assert hero.tags == []
20+
assert hero.tags is not other.tags
21+
22+
if TYPE_CHECKING:
23+
Hero(secretName="Dive Wilson") # ty: ignore[missing-argument]
24+
Hero("Deadpond", "Dive Wilson") # ty: ignore[too-many-positional-arguments, missing-argument]
25+
Hero(name=123, secretName="Dive Wilson") # ty: ignore[invalid-argument-type]

0 commit comments

Comments
 (0)