Skip to content

Commit 900d613

Browse files
fsecada01claude
andcommitted
Fix type checking errors across codebase
- Fix invalid parameter defaults: dict = None → dict | None = None - Fix invalid type forms: str or int → str | int for union types - Fix write_row signature: Type[SQLModel] → SQLModel (should accept instances) - Fix logger type mismatch: add Any annotation for optional loguru dependency - Fix dynamic import type error: add type: ignore for SQLAlchemy dialect import - Replace deprecated session.execute() with session.exec() in bulk_upsert - Add type: ignore comments for SQLModel Field.in_() method in tests All type checks now pass with ty check. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 53fe6f9 commit 900d613

5 files changed

Lines changed: 16 additions & 19 deletions

File tree

sqlmodel_crud_utils/a_sync.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
""" """
22

3-
from typing import Type
4-
53
from dateutil.parser import parse as date_parse
64
from dotenv import load_dotenv
75
from sqlalchemy.exc import MultipleResultsFound
@@ -46,7 +44,7 @@ async def get_result_from_query(query: SelectOfScalar, session: AsyncSession):
4644
async def get_one_or_create(
4745
session_inst: AsyncSession,
4846
model: type[SQLModel],
49-
create_method_kwargs: dict = None,
47+
create_method_kwargs: dict | None = None,
5048
selectin: bool = False,
5149
select_in_key: str | None = None,
5250
**kwargs,
@@ -92,12 +90,12 @@ async def _get_entry(sqlmodel, **key_args):
9290
return created, False
9391

9492

95-
async def write_row(data_row: Type[SQLModel], session_inst: AsyncSession):
93+
async def write_row(data_row: SQLModel, session_inst: AsyncSession):
9694
"""
9795
Writes a new instance of an SQLModel ORM model to the database, with an
9896
exception catch that rolls back the session in the event of failure.
9997
100-
:param data_row: Type[SQLModel]
98+
:param data_row: SQLModel
10199
:param session_inst: AsyncSession
102100
:return: Tuple[bool, ScalarResult]
103101
"""
@@ -158,7 +156,7 @@ async def insert_data_rows(data_rows, session_inst: AsyncSession):
158156

159157

160158
async def get_row(
161-
id_str: str or int,
159+
id_str: str | int,
162160
session_inst: AsyncSession,
163161
model: type[SQLModel],
164162
selectin: bool = False,
@@ -441,7 +439,7 @@ async def get_rows_within_id_list(
441439

442440

443441
async def delete_row(
444-
id_str: str or int,
442+
id_str: str | int,
445443
session_inst: AsyncSession,
446444
model: type[SQLModel],
447445
pk_field: str = "id",
@@ -498,7 +496,7 @@ async def bulk_upsert_mappings(
498496
index_elements=[getattr(model, x) for x in pk_fields],
499497
set_={k: getattr(stmnt.excluded, k) for k in payload[0].keys()},
500498
)
501-
await session_inst.execute(stmnt)
499+
await session_inst.exec(stmnt)
502500

503501
results = await session_inst.scalars(
504502
stmnt.returning(model), execution_options={"populate_existing": True}

sqlmodel_crud_utils/sync.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
""" """
22

3-
from typing import Type
4-
53
from dateutil.parser import parse as date_parse
64
from dotenv import load_dotenv
75
from sqlalchemy.exc import MultipleResultsFound
@@ -45,7 +43,7 @@ def get_result_from_query(query: SelectOfScalar, session: Session):
4543
def get_one_or_create(
4644
session_inst: Session,
4745
model: type[SQLModel],
48-
create_method_kwargs: dict = None,
46+
create_method_kwargs: dict | None = None,
4947
selectin: bool = False,
5048
select_in_key: str | None = None,
5149
**kwargs,
@@ -91,12 +89,12 @@ def _get_entry(sqlmodel, **key_args):
9189
return created, False
9290

9391

94-
def write_row(data_row: Type[SQLModel], session_inst: Session):
92+
def write_row(data_row: SQLModel, session_inst: Session):
9593
"""
9694
Writes a new instance of an SQLModel ORM model to the database, with an
9795
exception catch that rolls back the session in the event of failure.
9896
99-
:param data_row: Type[SQLModel]
97+
:param data_row: SQLModel
10098
:param session_inst: Session
10199
:return: Tuple[bool, ScalarResult]
102100
"""
@@ -155,7 +153,7 @@ def insert_data_rows(data_rows, session_inst: Session):
155153

156154

157155
def get_row(
158-
id_str: str or int,
156+
id_str: str | int,
159157
session_inst: Session,
160158
model: type[SQLModel],
161159
selectin: bool = False,
@@ -442,7 +440,7 @@ def get_rows_within_id_list(
442440

443441

444442
def delete_row(
445-
id_str: str or int,
443+
id_str: str | int,
446444
session_inst: Session,
447445
model: type[SQLModel],
448446
pk_field: str = "id",

sqlmodel_crud_utils/utils.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import importlib
22
import logging
33
import os
4+
from typing import Any
45

56
from dateutil.parser import parse as date_parse
67

78
try:
89
from loguru import logger
910
except ImportError:
10-
logger = logging.getLogger("sqlmodel_crud_utils")
11+
logger: Any = logging.getLogger("sqlmodel_crud_utils") # type: ignore[assignment]
1112

1213

1314
def get_val(val: str):
@@ -31,7 +32,7 @@ def get_sql_dialect_import(dialect: str):
3132
3233
:return: func
3334
"""
34-
return importlib.import_module(f"sqlalchemy.dialects" f".{dialect}").insert
35+
return importlib.import_module(f"sqlalchemy.dialects" f".{dialect}").insert # type: ignore[attr-defined]
3536

3637

3738
def is_date(val: str, fuzzy: bool = False):

tests/test_async_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ async def test_async_insert_data_rows_success(async_session: AsyncSession):
275275

276276
# Assert: Verify they exist in the DB
277277
ids = [row.id for row in result]
278-
statement = select(MockModel).where(MockModel.id.in_(ids))
278+
statement = select(MockModel).where(MockModel.id.in_(ids)) # type: ignore[union-attr]
279279
# FIX: Remove redundant .scalars()
280280
fetched_rows_result = await async_session.exec(statement)
281281
fetched_rows = fetched_rows_result.all()

tests/test_sync_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ def test_sync_insert_data_rows_success(sync_session: Session):
220220

221221
# Assert: Verify they exist in the DB
222222
ids = [row.id for row in result]
223-
statement = select(MockModel).where(MockModel.id.in_(ids))
223+
statement = select(MockModel).where(MockModel.id.in_(ids)) # type: ignore[union-attr]
224224
fetched_rows = sync_session.exec(statement).all()
225225
assert len(fetched_rows) == len(data_rows)
226226
names_after = {row.name for row in fetched_rows}

0 commit comments

Comments
 (0)