Skip to content

Commit e3fbfe6

Browse files
committed
refactor: update type hints to use union syntax for optional types
1 parent f14f027 commit e3fbfe6

File tree

6 files changed

+11
-13
lines changed

6 files changed

+11
-13
lines changed

src/sqlmodel_graphql/introspection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import inspect
66
from collections.abc import Callable
77
from enum import Enum
8-
from typing import TYPE_CHECKING, Any, get_args, get_origin, get_type_hints
8+
from typing import TYPE_CHECKING, Any, get_type_hints
99

1010
from sqlmodel import SQLModel
1111

src/sqlmodel_graphql/sdl_generator.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from enum import Enum
77
from typing import TYPE_CHECKING, Any, get_args, get_origin, get_type_hints
88

9-
from pydantic import BaseModel
109
from sqlmodel import SQLModel
1110

1211
from sqlmodel_graphql.type_converter import TypeConverter

src/sqlmodel_graphql/utils/type_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from __future__ import annotations
44

55
from collections.abc import Callable
6-
from typing import Any, ParamSpec, get_args, get_origin, get_type_hints
6+
from typing import Any, ParamSpec, get_origin, get_type_hints
77

88
from sqlmodel_graphql.type_converter import TypeConverter
99

tests/test_sdl_generator.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,12 @@ def test_get_core_types_list(self) -> None:
166166

167167
def test_get_core_types_list_of_optional(self) -> None:
168168
"""Test extracting core types from list[Optional[T]]."""
169-
result = get_core_types(list[Optional[int]])
169+
result = get_core_types(list[int | None])
170170
assert result == [int]
171171

172172
def test_get_core_types_nested(self) -> None:
173173
"""Test extracting core types from nested types."""
174-
result = get_core_types(list[Union[int, str, None]])
174+
result = get_core_types(list[int | str | None])
175175
assert set(result) == {int, str}
176176

177177
def test_get_core_types_none_in_union(self) -> None:
@@ -271,7 +271,7 @@ def test_list_optional_int(self) -> None:
271271
Note: Current implementation doesn't unwrap Optional inside list,
272272
so it falls back to String. This is a known edge case.
273273
"""
274-
result = _python_type_to_graphql(list[Optional[int]], self.converter)
274+
result = _python_type_to_graphql(list[int | None], self.converter)
275275
# Current behavior: Optional inside list is not handled, defaults to String
276276
assert result == "[String!]!"
277277

tests/test_type_converter.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ def test_get_list_inner_type_entity(self) -> None:
126126
def test_get_list_inner_type_optional(self) -> None:
127127
"""Test getting inner type from list[Optional[int]]."""
128128
converter = TypeConverter({"UserForConverterTest"})
129-
result = converter.get_list_inner_type(list[Optional[int]])
129+
result = converter.get_list_inner_type(list[int | None])
130130
assert result == int
131131

132132
def test_get_list_inner_type_empty(self) -> None:
@@ -250,7 +250,7 @@ def test_is_relationship_list_entity(self) -> None:
250250
def test_is_relationship_list_optional_entity(self) -> None:
251251
"""Test detecting list of optional entities relationship."""
252252
converter = TypeConverter({"UserForConverterTest"})
253-
assert converter.is_relationship(list[Optional[UserForConverterTest]]) is True
253+
assert converter.is_relationship(list[UserForConverterTest | None]) is True
254254

255255
def test_is_relationship_non_entity(self) -> None:
256256
"""Test non-entity type is not a relationship."""
@@ -278,7 +278,7 @@ def test_unwrap_to_base_type_list(self) -> None:
278278
def test_unwrap_to_base_type_list_optional(self) -> None:
279279
"""Test unwrapping list[Optional[T]] to T."""
280280
converter = TypeConverter({"UserForConverterTest"})
281-
result = converter.unwrap_to_base_type(list[Optional[int]])
281+
result = converter.unwrap_to_base_type(list[int | None])
282282
assert result == int
283283

284284
def test_unwrap_to_base_type_entity(self) -> None:

tests/test_type_utils.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"""Tests for type_utils module."""
22

3-
from typing import Optional
43

54
from sqlmodel import Field, SQLModel
65

@@ -15,7 +14,7 @@ class EntityForUtilsTest(SQLModel):
1514

1615
id: int | None = Field(default=None, primary_key=True)
1716
name: str
18-
optional_field: Optional[str] = None
17+
optional_field: str | None = None
1918

2019

2120
class AnotherEntityForUtilsTest(SQLModel):
@@ -94,7 +93,7 @@ def test_get_return_entity_type_optional_entity(self) -> None:
9493
class TestEntityOptional(SQLModel):
9594
id: int | None
9695

97-
def find(cls, id: int) -> Optional[TestEntityOptional]: # type: ignore
96+
def find(cls, id: int) -> TestEntityOptional | None: # type: ignore
9897
return None
9998

10099
TestEntityOptional.find = classmethod(find) # type: ignore
@@ -157,7 +156,7 @@ def test_get_return_entity_type_list_optional(self) -> None:
157156
class TestEntityListOpt(SQLModel):
158157
id: int | None
159158

160-
def search(cls) -> list[Optional[TestEntityListOpt]]: # type: ignore
159+
def search(cls) -> list[TestEntityListOpt | None]: # type: ignore
161160
return []
162161

163162
TestEntityListOpt.search = classmethod(search) # type: ignore

0 commit comments

Comments
 (0)